diff --git a/.github/workflows/build-cloudberry.yml b/.github/workflows/build-cloudberry.yml index 83536c413f7..cd314a2a4d2 100644 --- a/.github/workflows/build-cloudberry.yml +++ b/.github/workflows/build-cloudberry.yml @@ -243,6 +243,12 @@ jobs: "make_configs":["src/test/regress:installcheck-good"], "pg_settings":{"optimizer":"on"} }, + {"test":"ic-anser", + "make_configs":["gpcontrib/anser:installcheck-with-install"] + }, + {"test":"ic-rpt", + "make_configs":["gpcontrib/robust_predicate_transfer:installcheck-with-install"] + }, {"test":"pax-ic-good-opt-off", "make_configs":[ "contrib/pax_storage/:pax-test", diff --git a/LICENSE b/LICENSE index a42d93295ee..2bc7349dd78 100644 --- a/LICENSE +++ b/LICENSE @@ -394,6 +394,12 @@ The PostgreSQL software includes: contrib/pgcrypto/imath.c see licenses/LICENSE-imath.txt + RPT+ transfer-graph code adapted from dynamic-predicate-transfer: + gpcontrib/robust_predicate_transfer/include/rpt.h + gpcontrib/robust_predicate_transfer/src/rptgraph.cpp + gpcontrib/robust_predicate_transfer/src/rptplan.cpp + see licenses/LICENSE-mit-duckdb-rpt.txt + ---------------------------- Apache License v2.0 with LLVM Exceptions diff --git a/NOTICE b/NOTICE index 0226ded13da..e078e7b7f4f 100644 --- a/NOTICE +++ b/NOTICE @@ -10,3 +10,12 @@ This product includes software originally developed by VMware. Greenplum Database Copyright (c) 2004-2020 VMware, Inc. or its affiliates. All Rights Reserved. + +--------------------------------------------------------------------------- + +This product includes RPT+ transfer-graph code in +gpcontrib/robust_predicate_transfer adapted from the +dynamic-predicate-transfer artifact: +https://github.com/embryo-labs/dynamic-predicate-transfer + +Copyright 2018-2025 Stichting DuckDB Foundation diff --git a/configure b/configure index ce7322eb553..686a235d336 100755 --- a/configure +++ b/configure @@ -818,6 +818,8 @@ GCOV enable_debug enable_orafce enable_faultinjector +with_rpt +with_anser enable_debug_extensions enable_gpfdist enable_strong_random @@ -889,6 +891,8 @@ enable_atomics enable_strong_random enable_gpfdist enable_debug_extensions +with_anser +with_rpt enable_faultinjector enable_orafce enable_debug @@ -1657,6 +1661,9 @@ Optional Packages: --with-libraries=DIRS look for additional libraries in DIRS --with-libs=DIRS alternative spelling of --with-libraries --with-pgport=PORTNUM set default port number [5432] + --with-anser build the Anser runtime-filter extension + --with-rpt build the RPT+ transfer-plan extension (requires + --with-anser) --with-blocksize=BLOCKSIZE set table block size in kB [8] --with-segsize=SEGSIZE set table segment size in GB [1] @@ -3597,6 +3604,67 @@ fi +# +# Anser adaptive information sharing (runtime filters) +# + + + +# Check whether --with-anser was given. +if test "${with_anser+set}" = set; then : + withval=$with_anser; + case $withval in + yes) + : + ;; + no) + : + ;; + *) + as_fn_error $? "no argument expected for --with-anser option" "$LINENO" 5 + ;; + esac + +else + with_anser=no + +fi + + + + +# +# Robust predicate transfer (RPT+ over Anser) +# + + + +# Check whether --with-rpt was given. +if test "${with_rpt+set}" = set; then : + withval=$with_rpt; + case $withval in + yes) + : + ;; + no) + : + ;; + *) + as_fn_error $? "no argument expected for --with-rpt option" "$LINENO" 5 + ;; + esac + +else + with_rpt=no + +fi + + + +if test "$with_rpt" = yes && test "$with_anser" != yes; then + as_fn_error $? "--with-rpt requires --with-anser" "$LINENO" 5 +fi + # # Enable fault injector # diff --git a/configure.ac b/configure.ac index 8c8e3fa54be..9a1414fd1e6 100644 --- a/configure.ac +++ b/configure.ac @@ -234,6 +234,23 @@ PGAC_ARG_BOOL(enable, debug-extensions, yes, [exclude debug extensions in gpcontrib]) AC_SUBST(enable_debug_extensions) +# +# Anser adaptive information sharing (runtime filters) +# +PGAC_ARG_BOOL(with, anser, no, + [build the Anser runtime-filter extension]) +AC_SUBST(with_anser) + +# +# Robust predicate transfer (RPT+ over Anser) +# +PGAC_ARG_BOOL(with, rpt, no, + [build the RPT+ transfer-plan extension (requires --with-anser)]) +AC_SUBST(with_rpt) +if test "$with_rpt" = yes && test "$with_anser" != yes; then + AC_MSG_ERROR([--with-rpt requires --with-anser]) +fi + # # Enable fault injector # diff --git a/contrib/interconnect/udp/ic_udpifc.c b/contrib/interconnect/udp/ic_udpifc.c index 1965ca998b1..f34037ef39c 100644 --- a/contrib/interconnect/udp/ic_udpifc.c +++ b/contrib/interconnect/udp/ic_udpifc.c @@ -46,6 +46,7 @@ #include "nodes/pg_list.h" #include "nodes/print.h" #include "miscadmin.h" +#include "storage/proc.h" #include "libpq/libpq-be.h" #include "port/atomics.h" #include "port/pg_crc32c.h" @@ -353,11 +354,18 @@ struct ReceiveControlInfo CursorICHistoryTable cursorHistoryTable; /* - * Last distributed transaction id when SetupUDPInterconnect is called. - * Coupled with cursorHistoryTable, it is used to handle multiple - * concurrent cursor cases. + * Local (virtual) transaction id of the top-level transaction the last + * time SetupUDPInterconnect was called. Coupled with cursorHistoryTable, + * it is used to handle multiple concurrent cursor cases: entries may only + * be pruned once we are in a different transaction. + * + * We deliberately use MyProc->lxid rather than the distributed xid: a + * distributed xid is only assigned lazily to transactions that write (or + * are dispatched two-phase), so for autocommit read-only statements it is + * always InvalidDistributedTransactionId and the "different transaction" + * test would never fire, letting the history table grow without bound. */ - DistributedTransactionId lastDXatId; + LocalTransactionId lastLxid; }; /* @@ -1892,7 +1900,7 @@ InitMotionUDPIFC(int *listenerSocketFd, int32 *listenerPort) /* allocate a buffer for sending disorder messages */ rx_control_info.disorderBuffer = palloc0(MIN_PACKET_SIZE); - rx_control_info.lastDXatId = InvalidTransactionId; + rx_control_info.lastLxid = InvalidLocalTransactionId; rx_control_info.lastTornIcId = 0; initCursorICHistoryTable(&rx_control_info.cursorHistoryTable); @@ -3571,19 +3579,13 @@ SetupUDPIFCInterconnect_Internal(SliceTable *sliceTable) if (Gp_role == GP_ROLE_DISPATCH) { - DistributedTransactionId distTransId = 0; - TransactionId localTransId = 0; - TransactionId subtransId = 0; - - GetAllTransactionXids(&(distTransId), - &(localTransId), - &(subtransId)); + LocalTransactionId curLxid = MyProc->lxid; /* - * Prune only when we are not in the save transaction and there is a + * Prune only when we are not in the same transaction and there is a * large number of entries in the table */ - if (distTransId != rx_control_info.lastDXatId && rx_control_info.cursorHistoryTable.count > (2 * CURSOR_IC_TABLE_SIZE)) + if (curLxid != rx_control_info.lastLxid && rx_control_info.cursorHistoryTable.count > (2 * CURSOR_IC_TABLE_SIZE)) { if (gp_log_interconnect >= GPVARS_VERBOSITY_DEBUG) elog(DEBUG1, "prune cursor history table (count %d), icid %d", rx_control_info.cursorHistoryTable.count, sliceTable->ic_instance_id); @@ -3593,7 +3595,7 @@ SetupUDPIFCInterconnect_Internal(SliceTable *sliceTable) addCursorIcEntry(&rx_control_info.cursorHistoryTable, sliceTable->ic_instance_id, gp_command_count); /* save the latest transaction id. */ - rx_control_info.lastDXatId = distTransId; + rx_control_info.lastLxid = curLxid; } /* now we'll do some setup for each of our Receiving Motion Nodes. */ diff --git a/contrib/pax_storage/src/test/regress/expected/gp_explain.out b/contrib/pax_storage/src/test/regress/expected/gp_explain.out index 6fc3b1ef844..3e02c273089 100644 --- a/contrib/pax_storage/src/test/regress/expected/gp_explain.out +++ b/contrib/pax_storage/src/test/regress/expected/gp_explain.out @@ -41,6 +41,29 @@ begin end loop; end; $$ language plpgsql; +-- Run EXPLAIN ANALYZE BUFFERS, preserving the positions of buffer statistics +create or replace function explain_buffers_filter(explain_query text) returns setof text as +$$ +declare + explainrow text; +begin + for explainrow in execute + 'EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF, SUMMARY OFF) ' || explain_query + loop + if explainrow ~ '^\s*Buffers:' then + explainrow := regexp_replace(explainrow, 'Buffers:.*$', 'Buffers: N'); + else + explainrow := regexp_replace(explainrow, + '\m\d+([kK]B?)\M', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '\m\d+(x)', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '-?\m\d+\M', 'N', 'g'); + end if; + return next explainrow; + end loop; +end; +$$ language plpgsql; -- Test explain wal option CREATE TABLE explainwal (id int4); NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Apache Cloudberry data distribution key for this table. @@ -104,6 +127,23 @@ NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' a HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew. INSERT INTO explaintest SELECT generate_series(1, 10); ANALYZE explaintest; +-- Compare normalized buffer placement +-- explain_processing_off +SELECT plan_line +FROM explain_buffers_filter($$ + SELECT * FROM explaintest; +$$) WITH ORDINALITY AS plan(plan_line, line_number) +ORDER BY line_number; + plan_line +------------------------------------------------------------------ + Gather Motion N:N (slice1; segments: N) (actual rows=N loops=N) + -> Seq Scan on explaintest (actual rows=N loops=N) + Buffers: N + Planning: + Buffers: N + Optimizer: Postgres query optimizer +(6 rows) + EXPLAIN ANALYZE SELECT * FROM explaintest; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------- diff --git a/contrib/pax_storage/src/test/regress/expected/gp_explain_optimizer.out b/contrib/pax_storage/src/test/regress/expected/gp_explain_optimizer.out index 4244ab91437..8c500ee69e0 100644 --- a/contrib/pax_storage/src/test/regress/expected/gp_explain_optimizer.out +++ b/contrib/pax_storage/src/test/regress/expected/gp_explain_optimizer.out @@ -41,6 +41,29 @@ begin end loop; end; $$ language plpgsql; +-- Run EXPLAIN ANALYZE BUFFERS, preserving the positions of buffer statistics +create or replace function explain_buffers_filter(explain_query text) returns setof text as +$$ +declare + explainrow text; +begin + for explainrow in execute + 'EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF, SUMMARY OFF) ' || explain_query + loop + if explainrow ~ '^\s*Buffers:' then + explainrow := regexp_replace(explainrow, 'Buffers:.*$', 'Buffers: N'); + else + explainrow := regexp_replace(explainrow, + '\m\d+([kK]B?)\M', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '\m\d+(x)', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '-?\m\d+\M', 'N', 'g'); + end if; + return next explainrow; + end loop; +end; +$$ language plpgsql; -- Test explain wal option CREATE TABLE explainwal (id int4); NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Apache Cloudberry data distribution key for this table. @@ -104,6 +127,23 @@ NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' a HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew. INSERT INTO explaintest SELECT generate_series(1, 10); ANALYZE explaintest; +-- Compare normalized buffer placement +-- explain_processing_off +SELECT plan_line +FROM explain_buffers_filter($$ + SELECT * FROM explaintest; +$$) WITH ORDINALITY AS plan(plan_line, line_number) +ORDER BY line_number; + plan_line +------------------------------------------------------------------ + Gather Motion N:N (slice1; segments: N) (actual rows=N loops=N) + -> Seq Scan on explaintest (actual rows=N loops=N) + Buffers: N + Planning: + Buffers: N + Optimizer: GPORCA +(6 rows) + EXPLAIN ANALYZE SELECT * FROM explaintest; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------- diff --git a/contrib/pax_storage/src/test/regress/sql/gp_explain.sql b/contrib/pax_storage/src/test/regress/sql/gp_explain.sql index 591d8f61540..6885d078238 100644 --- a/contrib/pax_storage/src/test/regress/sql/gp_explain.sql +++ b/contrib/pax_storage/src/test/regress/sql/gp_explain.sql @@ -45,6 +45,30 @@ begin end; $$ language plpgsql; +-- Run EXPLAIN ANALYZE BUFFERS, preserving the positions of buffer statistics +create or replace function explain_buffers_filter(explain_query text) returns setof text as +$$ +declare + explainrow text; +begin + for explainrow in execute + 'EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF, SUMMARY OFF) ' || explain_query + loop + if explainrow ~ '^\s*Buffers:' then + explainrow := regexp_replace(explainrow, 'Buffers:.*$', 'Buffers: N'); + else + explainrow := regexp_replace(explainrow, + '\m\d+([kK]B?)\M', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '\m\d+(x)', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '-?\m\d+\M', 'N', 'g'); + end if; + return next explainrow; + end loop; +end; +$$ language plpgsql; + -- Test explain wal option CREATE TABLE explainwal (id int4); @@ -94,6 +118,14 @@ CREATE TABLE explaintest (id int4); INSERT INTO explaintest SELECT generate_series(1, 10); ANALYZE explaintest; +-- Compare normalized buffer placement +-- explain_processing_off +SELECT plan_line +FROM explain_buffers_filter($$ + SELECT * FROM explaintest; +$$) WITH ORDINALITY AS plan(plan_line, line_number) +ORDER BY line_number; + EXPLAIN ANALYZE SELECT * FROM explaintest; set explain_memory_verbosity='summary'; diff --git a/devops/build/automation/cloudberry/scripts/configure-cloudberry.sh b/devops/build/automation/cloudberry/scripts/configure-cloudberry.sh index 989b1432365..267fe2ce3cf 100755 --- a/devops/build/automation/cloudberry/scripts/configure-cloudberry.sh +++ b/devops/build/automation/cloudberry/scripts/configure-cloudberry.sh @@ -32,6 +32,7 @@ # - IC Proxy Support # - Oracle Compatibility (orafce) # - ORCA Query Optimizer +# - Anser Runtime Filters (opt-in, ENABLE_ANSER=true) # - PAX Access Method # - Test Automation Support (tap-tests) # @@ -147,6 +148,16 @@ if [ "${ENABLE_MDBLOCALES:-false}" = "true" ]; then CONFIGURE_MDBLOCALES_OPTS="--with-mdblocales" fi +# Anser is opt-in at configure time, and stays opt-in here: passing +# --with-anser unconditionally would put it into every release artifact, which +# is the opposite of what the configure default says. The ic-anser CI job does +# not need it -- gpcontrib/anser's installcheck-with-install builds, installs +# and preloads the extension itself. +CONFIGURE_ANSER_OPTS="" +if [ "${ENABLE_ANSER:-false}" = "true" ]; then + CONFIGURE_ANSER_OPTS="--with-anser" +fi + # Configure build log_section "Configure" execute_cmd ./configure --prefix=${BUILD_DESTINATION} \ @@ -158,6 +169,7 @@ execute_cmd ./configure --prefix=${BUILD_DESTINATION} \ --enable-pax \ --enable-tap-tests \ ${CONFIGURE_DEBUG_OPTS} \ + ${CONFIGURE_ANSER_OPTS} \ --with-diskquota \ --with-gp-stats-collector \ --with-gssapi \ diff --git a/gpcontrib/Makefile b/gpcontrib/Makefile index 63c6f80c8f6..e379616fee5 100755 --- a/gpcontrib/Makefile +++ b/gpcontrib/Makefile @@ -51,6 +51,17 @@ endif ifeq "$(with_gp_stats_collector)" "yes" recurse_targets += gp_stats_collector endif + +# Anser is opt-in at configure time: --with-anser (off by default). +ifeq "$(with_anser)" "yes" + recurse_targets += anser +endif + +# RPT+ rides on Anser: --with-rpt (off by default, needs --with-anser). +ifeq "$(with_rpt)" "yes" + recurse_targets += robust_predicate_transfer +endif + $(call recurse,all install clean distclean, $(recurse_targets)) all: gpcloud orafce @@ -92,3 +103,4 @@ installcheck: $(MAKE) -C gp_toolkit installcheck $(MAKE) -C gp_exttable_fdw installcheck if [ "$(with_diskquota)" = "yes" ]; then $(MAKE) -C diskquota installcheck; fi + if [ "$(with_anser)" = "yes" ]; then $(MAKE) -C anser installcheck; fi diff --git a/gpcontrib/anser/Makefile b/gpcontrib/anser/Makefile new file mode 100644 index 00000000000..dbad6d5a823 --- /dev/null +++ b/gpcontrib/anser/Makefile @@ -0,0 +1,155 @@ +#------------------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +# Makefile for the anser extension. +# +# IDENTIFICATION +# gpcontrib/anser/Makefile +# +#------------------------------------------------------------------------- + +MODULE_big = anser +OBJS = \ + $(WIN32RES) \ + src/anserbloomconsume.o \ + src/anserbloomproduce.o \ + src/anserdispatch.o \ + src/anserfilter.o \ + src/anserinit.o \ + src/anserpayload.o \ + src/anserplan.o \ + src/anserplanexec.o \ + src/ansersideband.o + +PGFILEDESC = "anser - adaptive information sharing runtime filters" + +# No catalog objects: the subsystem is configured entirely by GUCs and travels +# over the dispatch connection, so there is no extension to install here. The +# harness the tests use to reach the C API from SQL is a library of its own in +# anser_test/ -- this one is preloaded into every backend and has no business +# carrying test code. +# anser_faults is split out rather than gated: everything in it calls +# gp_inject_fault, which exists only in a build with the debug extensions, and +# the regression driver drops any file that calls one (faultinjector_prep in +# src/Makefile.global.in). Keeping them in anser_runtime_filter would take the +# rest of that file down with them wherever the extensions are off. +REGRESS = anser_test anser_runtime_filter anser_faults anser_multijoin + +# src/anserdispatch.c writes sideband messages onto the dispatcher's own libpq +# connections, so it needs libpq's internal headers (as the dispatcher itself +# does). Deliberately NOT linked against libpq: the pq* writers we use stay +# global in the postgres binary, while the public PQ* API is made local there +# on purpose (src/backend/Makefile), so linking libpq.so would put a second +# copy of libpq on connections the backend's copy created. +# +# Headers only means no SHLIB_PREREQS either. submake-libpq is there to build +# what a module links against, and under PGXS libpq_builddir is $(libdir), so +# it would run "make -C $(libdir) all" and fail outright; in tree it stopped +# the build on a libpq that had not been built yet, before compiling anything. +PG_CPPFLAGS = -I$(srcdir)/include -I$(libpq_srcdir) + +ifdef USE_PGXS +PG_CONFIG ?= pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) +else +subdir = gpcontrib/anser +top_builddir = ../.. +include $(top_builddir)/src/Makefile.global +include $(top_srcdir)/contrib/contrib-global.mk +endif + +# The tests need the live gather/send services, which only exist when the +# library is preloaded and anser.enable is on -- both postmaster-context +# settings. Ensure them (and restart) before installcheck, as +# gp_relsizes_stats does for its worker. They also need the anser_test +# extension, which install deliberately leaves out: it is a test tool, so it is +# put in place here rather than shipped with the subsystem. +installcheck: unitcheck install-anser-test preload-anser + +.PHONY: install-anser-test +install-anser-test: + $(MAKE) -C anser_test install + +# The unit tests need no cluster of their own. Hanging them off installcheck +# is what gets them run at all: a failure here stops make before the cluster is +# touched. (Prerequisites are unordered under -j; what matters is that a +# broken one fails the target, not which runs first.) +.PHONY: unitcheck +unitcheck: + $(MAKE) -C test unitcheck + +# Neither the harness nor the unit tests are part of this library's build, so +# they are not part of its clean either unless said so. +clean: clean-anser-test + +.PHONY: clean-anser-test +clean-anser-test: + $(MAKE) -C test clean + $(MAKE) -C anser_test clean + +# Arm the cluster, and only disturb it when that is actually needed. +# +# The decision takes one shell: make runs each recipe line in its own, and +# whether a restart is called for is only known after the first half. That +# gives up make's per-line status check, so every command below ends in +# "|| exit 1". +# +# SHOW reports what the postmaster really loaded rather than what +# postgresql.conf says, which is the question being asked -- a library +# configured but never restarted into still needs the restart. anser.enable is +# only worth asking about once anser is loaded: until then the GUC does not +# exist and SHOW errors out instead of answering "off". +.PHONY: preload-anser +preload-anser: + @if [ -z "$$COORDINATOR_DATA_DIRECTORY$$MASTER_DATA_DIRECTORY" ]; then \ + echo "ERROR: COORDINATOR_DATA_DIRECTORY (or MASTER_DATA_DIRECTORY) is not set;" >&2; \ + echo " source cloudberry-env.sh and gpdemo-env.sh before running installcheck." >&2; \ + exit 1; \ + fi + @current=`psql -d postgres -At -c "SHOW shared_preload_libraries;"` || { \ + echo "ERROR: cannot read shared_preload_libraries; is the cluster up?" >&2; \ + exit 1; \ + }; \ + current=`echo "$$current" | tr -d ' '`; \ + need_restart=; \ + case ",$$current," in \ + *,anser,*) \ + echo "==> shared_preload_libraries already contains anser" ;; \ + *) \ + if [ -z "$$current" ]; then new=anser; else new="$$current,anser"; fi; \ + echo "==> Adding anser to shared_preload_libraries ($$new)"; \ + gpconfig -c shared_preload_libraries -v "'$$new'" --skipvalidation || exit 1; \ + need_restart=1 ;; \ + esac; \ + if [ -z "$$need_restart" ] && \ + [ "`psql -d postgres -At -c 'SHOW anser.enable' 2>/dev/null`" = on ]; then \ + echo "==> anser.enable is already on; leaving the cluster alone"; \ + else \ + gpconfig -c anser.enable -v on --skipvalidation || exit 1; \ + gpstop -ra || exit 1; \ + fi; \ + psql -d postgres -c "SHOW shared_preload_libraries;" || exit 1 + +# CI runs a single make target per test config, so offer one target that +# installs the extension, arms the cluster, and runs the tests. +.PHONY: installcheck-with-install +installcheck-with-install: + $(MAKE) install + $(MAKE) installcheck diff --git a/gpcontrib/anser/README.md b/gpcontrib/anser/README.md new file mode 100644 index 00000000000..34367c23eb9 --- /dev/null +++ b/gpcontrib/anser/README.md @@ -0,0 +1,557 @@ +# Anser — adaptive information sharing + +Anser is a runtime pub/sub facility for MPP query execution. Producers on the +segments publish a small piece of information about a running query (today: a +bloom filter over a join-build key), the coordinator combines the per-segment +parts into one, and consumers on the segments receive it and use it to prune +work (today: skip probe rows that cannot join). + +Everything travels over the **dispatch connection the coordinator already holds +open to every segment**. There is no shared memory, no background worker, no +second connection and nothing extra to authenticate: a channel exists only in +the coordinator backend running the query, for exactly as long as that query +runs. + +This document covers installation, the architecture, the wire protocol, the +configuration surface, and what a *channel* is. For the plan-tree integration +see `anserplan.c`; for the payload/bloom protocol see `anserfilter.c` and +`lib/bloomfilter.c`. + +## Installation + +Anser is configured entirely by GUCs — it creates no catalog objects, so there +is nothing to `CREATE EXTENSION` in each database. + +``` +gpconfig -c shared_preload_libraries -v "'anser'" --skipvalidation +gpconfig -c anser.enable -v on +gpstop -ra +``` + +The library **must** be preloaded: a segment backend deserializing a dispatched +plan has no opportunity to load it on demand, and its CustomScan providers have +to be registered before that happens. + +Then turn the filter on where you want it — `anser.runtime_filter` is +`PGC_USERSET`, so per session, per role, or cluster-wide. + +`anser_test` is a separate extension, built from `anser_test/` as a library of +its own, that exposes the internal C API to the regression tests. It resolves +that API at load time against the preloaded `anser.so`, which is what the +`PGDLLEXPORT` markers on the public headers are for. `make install` does not +build or install it — `make installcheck` does — so a cluster never carries it +unless someone puts it there deliberately. + +## Architecture + +A **channel** is one rendezvous point between the producers and consumers of a +single piece of runtime information, for a single query: + +``` +AnserChannelKey = { gp_session_id, gp_command_count, condition_id, condition_key[64] } +``` + +- `gp_session_id` + `gp_command_count` scope the channel to one query execution, + so keys never collide across sessions or across statements in a session. +- `condition_id` distinguishes multiple filters within the same query. +- `condition_key` is an opaque string describing the filtered condition — today + just `anser_rf_`, since the planner stamps the same key into + both plan nodes at injection time. It exists for the case where the key has to + be derived from the build's semantic identity instead (so that producer and + consumer can find each other without having been planned together), which is + why the wire format treats it as arbitrary bytes rather than as an identifier. + +Channels live in a hash in the coordinator backend, created on first use and +dropped at `ExecutorEnd` (or on transaction abort). Since the merge and the +delivery both happen in that one process, the accumulator is an ordinary +`palloc`'d buffer. + +### Where the nodes go + +Both nodes wrap a **base scan**, never an intermediate node. The producer goes +above the scan the join's build key comes from, the consumer above the scan its +probe key comes from; either may be several joins away, and `anser_resolve_key_scan()` +follows the key column down through Hash, Motion, HashJoin and our own nodes to +find it. + +Wrapping a base scan is not a preference. After `set_plan_references` an +intermediate node's targetlist holds `OUTER_VAR`/`INNER_VAR` Vars, and a +`CustomScan` keeps its child in `custom_plans` rather than in `lefttree`, so +copying such a targetlist into `custom_scan_tlist` leaves EXPLAIN resolving an +`OUTER_VAR` against a node with no outer plan — `get_variable()` raises +"bogus varno" and the plan cannot be printed at all. + +Two consequences: + +- A filter built from a base scan holds that relation's **whole** key column, + not the subset the joins below it would have left. A superset never rejects a + row that could join, so it is correct; it is just less selective than a filter + built on the joined result would be. +- When two joins reach the same relation, the second **stacks** its consumer + under the first rather than replacing it, so the scan is filtered by both. + +The direction is fixed: a filter can only flow **build → probe**. A hash join +builds its hash table from the inner side before reading the outer, so a +consumer on the inner side would wait for a producer whose rows the join is not +yet reading — and that producer only publishes once its subtree is drained, +which it cannot be. That is a deadlock, not a slow path, and it is why the +intermediate result cannot filter the table it is being joined to. + +### Only across a Motion + +A join is injected into only when its build and probe scans run in **different +slices**. Slices are divided by Motions and nothing else, so this says: filter +only where the rows already travel over the network. + +It is a safety rule first. An exchange is a synchronisation point — the +consumer stops until the coordinator has heard from every producer — and +within one slice producer and consumer are the same process, run one after the +other. A consumer placed under an intervening hash join then waits for a +producer in that join's inner side, which cannot start until the outer side +yields the very tuple the consumer is holding. `defer_first` covers exactly +one tuple of that, and no more. Across a Motion they are separate processes +and the question does not arise. + +It is also what makes the exchange worth its cost. Where a Motion already +carries the rows, the barrier is paid for by work the query was doing anyway. +In a co-located join it is new cost against a join that was already local, and +whether it pays for itself is a measurement nobody has made — so until someone +does, those joins are left alone. + +### Giving up early + +A bloom filter that is too small for its key count matches almost everything: it +costs a hash and `k` probes per probe row and eliminates nothing. Anser checks +for that at the three points where new information becomes available, and always +fails open — giving up means the query runs unfiltered, never that it runs wrong. + +| Where | Knows | Check | Cost of giving up | +| --- | --- | --- | --- | +| Planner (`anserplan.c`) | the row estimate | bitset bits / estimated keys ≥ 4 | nothing — the nodes are never injected | +| Producer init (`anserfilter.c`) | the plan parameters | the filter fits the payload cap, and still ≥ 4 bits/key | one `palloc0`, freed immediately; cancel published on the first tuple, before the build side is scanned | +| Producer publish (`anserbloomproduce.c`) | the filter, fully built | estimated FPR (`fill^k`) ≤ 50% | the build scan, already spent; saves the network and every consumer's probing | +| Coordinator merge (`anserdispatch.c`) | the merged payload | fill ≤ 95% | the merge, already spent; saves delivery and probing | + +The planner gate is the one that matters, because it is the only one that costs +nothing. The rest exist because a row estimate can be wrong, and the last one +exists because **folding changes the answer**: OR-ing three parts that are each +60% full gives a merged filter that is 94% full, so no producer can tell whether +the result will be useful. + +One subtlety worth knowing if you touch the sizing: do **not** clamp the element +estimate to make a filter fit. `total_elems` is also what `optimal_k()` derives +the hash count from, so understating it misconfigures the filter — a 128M-row +build side declared as 33.5M gets `k=10` where the optimum is `k=3`, turning a +13% false positive rate into 38%. The keys all go in regardless of what was +written down. Clamp the *size*; keep the count honest. + +### Parallel execution + +A parallel slice runs `numsegments × parallel_workers` processes, and **every +one of them is a full QE with its own dispatch connection** — not a PostgreSQL +background worker. So each publishes and subscribes for itself, and the +coordinator folds however many parts arrive. Nothing about the transport or the +merge changes. + +Only the number of parts to expect differs, and it is not the segment count: it +is the width of the build scan's slice, `numsegments × parallel_workers`. The +planner computes that while injecting and stamps it into the plan node +(`ANSER_RF_PRIV_N_PRODUCERS`); the executor reads it from there. Both optimizers +go through the same path, and a join whose slice cannot be identified is not +injected into. + +### Delivery cost + +Fan-in is parallel and fan-out is not. Every producer uploads its part at the +same time while the coordinator reads as it goes, but `anser_disp_deliver()` +then walks its subscribers and writes the merged filter to each in turn. +Delivery latency is therefore the *sum* of the writes, not the slowest one, and +it grows with the width of the slice. + +That is only the tail of what a consumer waits for, and which part dominates +depends on the plan. Where the probe side is local to the join, `prefetch_inner` +has already drained the build side before the consumer runs — every producer has +published, and only the fold and the fan-out are left. Where the probe side +travels through its own Motion, the consumer sits in that scan's slice and runs +alongside the build, so it waits the build side out as well. + +Measured on 4 segments, 5000 build rows, a 1 MB filter (`bloom_create` floors +every bitset at 1 MB, whatever the cardinality): + +| Slice width | Subscribers | Fan-in | Fan-out | Consumer stalls | +| --- | --- | --- | --- | --- | +| 4 | 4 | ~40 ms | ~520 ms | 549 ms | +| 16 | 16 | ~100 ms | ~1140 ms | 1005 ms, then gave up | + +These numbers predate the switch to chunked delivery. Each write above +blocked until that peer had taken the filter, which is what made a wide slice so +expensive — and what could strand the dispatcher outright when nobody was +reading. Delivery now goes one chunk per NEXT request, never blocking, so the +fan-out should be well under this; it has not been re-measured. + +The second row is still what sets the floor for the deadline: at 16 subscribers +the fan-out outlived a 1 s deadline, so every consumer ran unfiltered. The 10 s +default clears even that worst measured case by most of an order of magnitude, +while keeping a filter that never arrives from dominating the query. It is a +budget for an exchange that has gone wrong, not a measurement of a healthy one. +The wait is not free either — the first subscriber is served in under a +millisecond and the last much later, and none of them can tell where in the +queue they are. + +**A filter can cost more than it saves.** In the 16-wide plan the consumer waits +160 ms to skip a probe scan that takes 40 ms. Anser is worth enabling when the +build side is selective *and* the probe side is large enough that the scan +dominates the exchange — not on small joins, and not, today, on wide slices. + +Each part is a whole bitset, because a union is a bitwise OR: N producers send +N × the full payload, and the coordinator returns the full payload to each of N +consumers. Nothing amortises with cluster size. For a 64 MB filter — the +ceiling `anser.max_info_size` allows — on 128 segments: + +| `parallel_workers` | Producers = consumers | Fan-in (base64, ×4/3) | Fan-out (raw) | Through the coordinator | +| --- | --- | --- | --- | --- | +| 1 | 128 | 10.7 GiB | 8.0 GiB | **18.7 GiB** | +| 4 | 512 | 42.7 GiB | 32.0 GiB | **74.7 GiB** | +| 8 | 1024 | 85.3 GiB | 64.0 GiB | **149.3 GiB** | + +Per join, over one coordinator NIC, with a CRC and a base64 decode per part on +one process. A star topology through a single coordinator is the wrong shape for +a reduction that is associative, commutative and idempotent; +`docs/local-fold.md` describes folding a segment's workers together before they +reach the coordinator, which is the first step away from it. + +### Plan shapes: the two optimizers + +Both optimizers reach the same exchange through the same code, but they choose +different slice widths for the same query, and the width is what governs +delivery cost. Same query, same cluster (4 segments): + +GPORCA, `max_parallel_workers_per_gather = 8` — a **parallel** build slice: + +``` + Gather Motion 16:1 (slice1; segments: 16) + -> Hash Join + -> Custom Scan (Anser Bloom Consumer) + -> Parallel Seq Scan on p_probe p + -> Hash + -> Broadcast Motion 16:16 (slice2; segments: 16) + -> Custom Scan (Anser Bloom Producer) + -> Parallel Seq Scan on p_build b +``` + +PostgreSQL planner (`optimizer = off`) — a **serial** build slice: + +``` + Gather Motion 8:1 (slice1; segments: 8) + -> Hash Join + -> Custom Scan (Anser Bloom Consumer) + -> Parallel Seq Scan on p_probe p + -> Hash + -> Broadcast Motion 2:8 (slice2; segments: 2) + -> Custom Scan (Anser Bloom Producer) + -> Seq Scan on p_build b +``` + +The difference is `Broadcast Motion 16:16` against `2:8`: ORCA parallelises the +build scan, the PG planner does not. So ORCA produces 16 producers and 16 +consumers where the PG planner produces 2 and 8 — eight times the parts to fold +and four times the deliveries, for the same filter. The PG plan finished the +whole query in 123 ms; the ORCA plan needed 282 ms with the filter delivered, +and 1293 ms when the fan-out overran the old 1 s deadline. + +Read `Gather Motion N:1` as the process count, not the segment count: +`FillSliceGangInfo()` fills a slice's segment list with `numsegments × +parallel_workers` entries, so `16:1` on 4 segments means 4 workers each. The +`x(0) workers` in the per-slice memory footer is a PostgreSQL `Gather`'s +`nworkers_launched`, which is always 0 here — an MPP slice has no `Gather` node — +and says nothing about MPP parallelism. + +### Payload types + +A channel carries one **payload type**, declared on the wire and registered in +`anserpayload.c`. The transport itself moves opaque bytes and never branches on +what they mean; everything type-specific lives in one descriptor +(`AnserPayloadOps`): + +| | `fold()` | `checksum_body` | +| --- | --- | --- | +| `B` — bloom filter | bitwise OR of equal-sized bitsets | yes | +| `-` — none (a subscription) | n/a | n/a | + +`fold()` is how the coordinator reduces one part per producer to a single +payload; there is no generic answer, which is why each type supplies it. A row +count, for instance, would fold by summing. + +`checksum_body` is about consequence, not size. A bloom filter fails +asymmetrically — a bit flipped 1 → 0 removes a key, so a joinable row is +rejected and the query silently returns too few rows — whereas a row count only +feeds a planning decision, where corruption costs a worse plan and never a wrong +answer. Types of the second kind opt out and pay nothing. (The header and +condition key are checksummed either way; that is ~100 ns and it is what +protects the routing fields.) + +Adding a type is three steps, none of which touch the framing: define a code, +add a row to `AnserPayloadTable`, and pass the code from the producer and +consumer nodes. `anserpayload.h` has the details. + +### How it attaches to the server + +Everything is reached through an existing extensibility point, so the server +carries no Anser-specific code (`anserinit.c`): + +| Hook | Used for | +| --- | --- | +| `planner_hook` | the injection pass, on the finished plan; wrapping the hook covers ORCA too, since it is dispatched from inside `standard_planner()` | +| `RegisterCustomScanMethods` | the producer/consumer nodes, so their methods resolve by name in every backend that deserializes a dispatched plan | +| `cdbdisp_notify_hook` | parts and subscriptions arriving from segments | +| `ExecutorEnd_hook` | dropping a query's channels | +| `DefineCustom*Variable` | the `anser.*` GUCs below | + +The first two already existed; `cdbdisp_notify_hook` and the +`GP_SIDEBAND_MESSAGE` tolerance in the QE command loop are the only additions +Anser needed in the server, and neither mentions Anser. + +## Wire protocol + +The two directions are deliberately asymmetric, because the constraints differ. + +**Segment → coordinator** is a `NOTIFY` on channel `anser_rf`, the model +`nextval()` uses (`cdb_sequence_nextval_qe` in `commands/sequence.c`). The QD is +a libpq *client*, and libpq rejects message types it does not know, so this +direction has to be a message type libpq already understands. `NotifyMyFrontEnd` +imposes no length limit of its own — the ~8 KB `NOTIFY_PAYLOAD_MAX_LENGTH` +applies to the SQL-level `NOTIFY`, which must fit a queue page — but it delivers +through `pq_sendstring`, so the payload must be a NUL-free string: + +``` +anser4 K T SSSSSSSSSS CCCCCCCCCC DDDDDDDDDD PPPPPPPPPP TTTTTTTTTT FFFF KKKK BBBBBBBBBB XXXXXXXX +^tag ^ ^ session command condition part total flags keylen bodylen crc + | \ payload type + \ kind + +``` + +`kind` is `P` (a producer's part), `S` (a consumer subscribing), `U` +(withdrawing), or `N` (a NEXT: ask for the next delivery chunk — see below; +its part field carries the offset the consumer wants, `total` is 0, and there +is no body) — what the message *does*, as opposed to the payload type, which +is what it *carries*. The header is **95 bytes of fixed-width ASCII**, so the +key and the body begin at offsets that nothing in their own contents can +shift, and the two lengths must account for the message exactly — a short, +long or misaligned message is rejected before any of it is used. There is +deliberately no delimiter anywhere in the format: the earlier version ended +its header with a newline and found it with `strchr()`, which was correct only +while the key could not itself contain a newline, an invariant nothing +enforced. + +**Coordinator → segment** is a `GP_SIDEBAND_MESSAGE`, written with `pqPutnchar`, +which performs no conversion — so the merged filter travels as **raw binary**, +with no base64 tax. That is the direction that matters most, since the merged +payload is sent once *per consumer* while each part is sent once. + +The merged payload travels **in chunks, one message per chunk**, and the +coordinator sends a chunk only when the consumer asks for it with a NEXT — +starting with chunk 0 on subscribe (or on channel completion, whichever is +later). The chunk size is chosen once per connection from its send buffer +(capped at 1 MB, floored at 8 kB), so one flush places a chunk whole and +nothing is left queued. The invariant this buys: **at most one whole chunk is +ever in flight**, so a consumer that walks away mid-delivery — deadline +expired, query finishing — never leaves a torn message behind, and its +withdrawal stops the flow at the chunk boundary. + +``` +payload_type | crc32c | condition_id | session_id | command_count | +flags | total_len | offset | keylen | key | chunk_len | chunk +``` + +`total_len` is the full merged payload size and `offset` is where the chunk +starts in it, so the consumer allocates the assembly buffer once, when chunk 0 +arrives, and every later chunk must name the offset that continues it. A +cancelled delivery is a single chunk with `F_CANCELLED`, `total_len = 0`, +`offset = 0`. Both chunk coordinates are inside the CRC, so a chunk's place in +the filter is as protected as its bytes. + +Both directions carry a **CRC32C**, and both discard a message that fails it. +The transport is TCP, not the UDP interconnect, so this is not about a lossy +link — TCP's 16-bit checksum is simply thin cover for a megabyte, and hardware +CRC32C costs about 0.05 ms/MB. The header and key are always covered; whether +the body is covered too is the payload type's decision, for the reasons in +[Payload types](#payload-types) above. Where it applies, the segment → +coordinator CRC covers the pre-base64 bytes, so it validates the decode as +well. + +The coordinator services these while it is blocked receiving tuples: the +interconnect adds every dispatch socket to its wait set +(`ic_udpifc.c`, `ic_tcp.c`), and a readable one leads to +`checkForCancelFromQD` → `cdbdisp_checkForCancel` → `processResults`, which is +where the notify handler runs. Chunked delivery is what keeps that loop fed: +every NEXT is inbound traffic, so an in-flight delivery keeps waking the +coordinator until it is done — the consumer drives the flow. Under +`gp_interconnect_type=proxy` that check is driven by a 2 s timer instead +(`ic_proxy_backend.c`), so filter delivery can lag by up to that long. + +## GUCs + +| GUC | Default | Context | Meaning | +| --- | --- | --- | --- | +| `anser.enable` | `off` | SIGHUP | Master switch. With it off the plan pass never injects anything and no filters are exchanged. | +| `anser.runtime_filter` | `off` | USERSET | Enables the post-planning pass that injects bloom-filter producer/consumer nodes into a matching plan. Requires `anser.enable`. | +| `anser.max_info_size` | `65 MB` | SUSET | Maximum serialized payload (merged bloom filter + part header) a channel may hold; caps the effective bloom-filter size. The default is `64 MB + 1 MB` so a full 64 MB power-of-two bitset fits with its header; `bloom_create` also floors every bitset at 1 MB. The ceiling is a little under 1 GB: every payload is held whole in a single palloc on each end — the coordinator's merged payload and the consumer's assembly buffer, plus the base64 copy of a part on a producer — so it must stay inside `MaxAllocSize`. Both ends read it — see [Changing max_info_size](#changing-max_info_size) before touching it on a live session. | +| `anser.timeout_ms` | `10 s` | USERSET | How long a consumer waits for its filter before running unfiltered. What the wait covers depends on the plan — see [Delivery cost](#delivery-cost). The deadline matters because a producer that gets squelched never publishes at all: `ExecSquelchNode` only marks a `CustomScanState`, it does not call the node back. | +| `anser.debug` | `off` | SUSET | Traces the exchange — publish, merge, delivery, receive — in the log of the process each step happens in. See below. | +| `anser.debug_min_filter_bytes` | `0` | SUSET | Test knob: a floor on the bitset size, so a small table still produces a full-size filter. | +| `anser.debug_delivery_chunk_bytes` | `0` | SUSET | Test knob: force the delivery chunk size instead of deriving it from the connection's send buffer, so a test can turn one delivery into hundreds of NEXT round trips. | + +### Changing max_info_size + +`anser.max_info_size` is the one GUC both ends of the exchange read, and they +read it for different things. The coordinator sizes the filter against it when +it plans, and bounds the parts it will accept; a segment bounds what it +publishes, and passes it to `pq_getmessage()` as the cap on the sideband message +it is about to read. A coordinator and a segment that disagree therefore do not +merely compute different sizes — they frame the wire differently. + +It is `SUSET` rather than `POSTMASTER` because a filter size is worth trying +without a cluster restart, but not worth handing to every role: it drives +per-segment memory and wire traffic. That is a trade-off, not a free choice: +**do not change it on a live session.** `GUC_GPDB_NEED_SYNC` gets the new value +to the segments — synchronously for every idle one, and in the connection +options of every gang created afterwards — but a gang that is busy with an open +cursor is not reached. Core does not update such a gang; it marks it as not +reusable and moves on (`cdbdisp_markNamedPortalGangsDestroyed()`), so its +segments keep the old value until the cursor is closed. + +While that lasts, the two failure directions differ. A part sized by the old +ceiling and rejected by the new one simply never completes its channel, and the +consumers run unfiltered after `anser.timeout_ms` — the usual fail-open. But a +delivery that overruns a segment's `pq_getmessage()` cap takes the length word +and leaves the body in the stream, and there is no recovering a message boundary +from there: the consumer terminates the connection rather than return it to the +gang pool poisoned. Set it in `postgresql.conf`, or set it before the session +does any work. + +### Tracing an exchange + +Until a filter is either used or timed out, none of the handoff is visible in +`EXPLAIN`: a consumer that waited and got nothing looks exactly like one whose +producers never published. `anser.debug` makes each step log where it happened, +which is normally the fastest way to find where an exchange broke: + +``` +seg0 producer init cond=0 part=0/3 elems=3334 payload=1048592 state=ok +seg0 producer child exhausted, publishing (state=ok) +seg0 published cond=0 part=0/3 bytes=1048592 cancelled=0 sent=1 +QD part cond=0 from seg0 (says part 0 of 3) 1/3 bytes=1048592 -> collecting +QD part cond=0 from seg1 (says part 1 of 3) 2/3 bytes=1048592 -> collecting +QD part cond=0 from seg2 (says part 2 of 3) 3/3 bytes=1048592 -> complete +QD delivering cond=0 to 3 subscriber(s) +QD pushed cond=0 to seg0 chunk=[0,1048592) of 1048592 cancelled=0 +seg0 received cond=0 chunk=[0,1048592) of 1048592 cancelled=0 +``` + +(That delivery fits one chunk; anything past the chunk size repeats the last +two lines per NEXT.) + +Set it in `postgresql.conf` (`gpconfig -c anser.debug -v on`) rather than with +`SET` if you need to see the producer gang: a session-level `SET` does not +reliably reach every gang, and the producers are the half you usually want. + +A consumer that gives up reports what it saw — `received 0 of 0 bytes` means +nothing arrived at all, whereas bytes in the middle mean the delivery stalled +part-way. + +## Data flow: producer → merge (bitwise union) → consumer + +The parts from all segments are combined into **one** payload by a **bitwise OR +on the coordinator**, and that single combined payload is delivered to every +consumer. This is the core of Anser and worth stating precisely, because it is +*not* a concatenation: + +``` +segment 0 producer: bitset 0000 0001 ┐ +segment 1 producer: bitset 0000 0010 ├─ NOTIFY ─► QD backend (notify hook) +segment N producer: ... ┘ │ + │ fold each part into the + │ running merged bitset: + │ 0000 0001 + │ OR 0000 0010 + ▼ = 0000 0011 (one part) + channel payload = single merged bitset + │ + sideband push ───────────┼───────────────┐ + ▼ ▼ ▼ + consumer seg 0 consumer seg 1 ... consumer seg N + each receives the SAME combined 0000 0011 +``` + +Step by step: + +1. **Produce (per segment, in parallel).** Each segment's producer builds a bloom + filter over its local build keys (`bloom_create` from `total_elems` / + `max_payload` / a `condition_key`-derived seed, all carried in the plan node — + *not* on the wire) and serializes it as one *part*. Because every producer and + the consumer pass the identical parameters, every part has a byte-for-byte + identical size and shape. Publishing is fire-and-forget: the producer sends + its part and carries on without waiting for an acknowledgement. + +2. **Merge (coordinator, once per part).** The coordinator never reconstructs a + filter — it works on raw bytes. The **first** part is stored verbatim; every + later part is folded into the channel's payload with an in-place **bitwise OR** + of the bitset (`AnserBloomFoldPartInPlace` in `anserfilter.c`, called from + `anserdispatch.c`). The payload is therefore always a **single merged bitset**, + the size of one filter — it does **not** grow with the segment count. Folding + happens as parts arrive, so only the last fold is on the critical path. The OR + requires the incoming part to be the same size as the accumulator (guaranteed + by the shared parameters); a mismatch cancels the channel and consumers fail + open. + +3. **Deliver (coordinator → every consumer).** Once every expected part is + folded, the merged payload is judged once (see [Giving up + early](#giving-up-early)) and then pushed to each subscriber. Delivery is per + consumer: a failed write costs that one segment its filter and leaves the + others alone. A consumer that subscribes *after* the channel completed — which + happens routinely, since producers on other segments may finish first — is + served immediately. + +4. **Consume (per segment).** Each consumer rebuilds an empty filter from its own + plan parameters (the same `total_elems` / `max_payload` / seed the producers + used) and loads the received bitset into it (`AnserBloomDeserializePart`), + requiring the received length to match exactly (else it fails open). It does + **not** re-union anything. + +Correctness note: the combined filter is the OR (super-set) of every segment's +build keys, so it can only ever have *false positives*, never false negatives — +a probe row it rejects genuinely cannot join. Anser therefore only changes +performance, never results; any failure along this path degrades to "no filter" +(fail open). + +## Failure handling + +Every failure mode ends in unfiltered execution, never in a wrong answer and +never in an error raised into the query: + +- a producer that is squelched, errors, or produces an oversized part → the + channel never completes (or is cancelled) → consumers hit `anser.timeout_ms` + and run unfiltered; +- a malformed or undecodable part → the channel is cancelled and every consumer + is told so; +- a broken connection → the query is failing anyway, and the interconnect + reports it far more usefully than the filter path could; +- query cancellation → the consumer's wait is a `CHECK_FOR_INTERRUPTS` loop, and + a consumer that walks away mid-delivery withdraws its subscription: the + coordinator stops at the chunk boundary, and the one chunk that may still be + in flight is discarded at the door (not subscribed) or skipped whole by the + QE command loop (`GP_SIDEBAND_MESSAGE` is accepted and ignored there). + +## Tests + +`make installcheck` runs two suites: + +- `anser_test` — the payload protocol (serialize, fold, reject a mismatched + part) and the four give-up decisions, each walked along its boundary: the + estimate either side of 4 bits/key, the payload cap either side of "1 MB plus + a header", fill either side of the 95% limit, and a producer whose filter + saturates. The case tables live in the `.sql` file, so the expected output + records real sizes rather than a bare `t`. +- `anser_runtime_filter` — plan-tree integration end to end: the nodes are + injected, and query results are identical with the feature on and off. diff --git a/gpcontrib/anser/anser_test/Makefile b/gpcontrib/anser/anser_test/Makefile new file mode 100644 index 00000000000..71b1c4f22ac --- /dev/null +++ b/gpcontrib/anser/anser_test/Makefile @@ -0,0 +1,58 @@ +#------------------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +# Makefile for anser_test, the harness the anser regression tests use to +# reach Anser's C API from SQL. +# +# A library of its own on purpose: linked into anser.so it would ride along +# into every backend, since anser is preloaded. Nothing here belongs in a +# running cluster, so the parent's install does not build it -- see the +# install-anser-test target there. +# +# It links against nothing: anser.so is preloaded and dfmgr.c loads modules +# RTLD_GLOBAL, so the AnserXxx entry points resolve at load time. That is +# what the PGDLLEXPORT markers on the public headers are for. +# +# IDENTIFICATION +# gpcontrib/anser/anser_test/Makefile +# +#------------------------------------------------------------------------- + +MODULE_big = anser_test +OBJS = \ + $(WIN32RES) \ + anser_test.o + +PGFILEDESC = "anser_test - test helpers for the anser extension" + +EXTENSION = anser_test +DATA = anser_test--1.0.sql + +PG_CPPFLAGS = -I$(srcdir)/../include + +ifdef USE_PGXS +PG_CONFIG ?= pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) +else +subdir = gpcontrib/anser/anser_test +top_builddir = ../../.. +include $(top_builddir)/src/Makefile.global +include $(top_srcdir)/contrib/contrib-global.mk +endif diff --git a/gpcontrib/anser/anser_test/anser_test--1.0.sql b/gpcontrib/anser/anser_test/anser_test--1.0.sql new file mode 100644 index 00000000000..d94957e7ccf --- /dev/null +++ b/gpcontrib/anser/anser_test/anser_test--1.0.sql @@ -0,0 +1,165 @@ +/* gpcontrib/anser/anser_test--1.0.sql */ + +-- complain if script is sourced in psql, rather than via CREATE EXTENSION +\echo Use "CREATE EXTENSION anser_test" to load this file. \quit + +CREATE FUNCTION anser_test_bloom_roundtrip( + condition_key text, + value int4) +RETURNS bool +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +CREATE FUNCTION anser_test_bloom_fold_inplace() +RETURNS bool +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +CREATE FUNCTION anser_test_bloom_rejects_mismatch() +RETURNS bool +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +CREATE FUNCTION anser_test_node_roundtrip(value int4) +RETURNS bool +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- The planner's give-up decision: NULL means no filter is injected. +CREATE FUNCTION anser_test_rf_size( + est_rows float8, + OUT injected bool, + OUT total_elems bigint, + OUT max_payload bigint, + OUT planned_bytes bigint) +RETURNS record +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- What AnserBloomCreate builds, or NULL when it declines to build anything. +CREATE FUNCTION anser_test_bloom_shape( + total_elems bigint, + cap bigint, + OUT built bool, + OUT bits bigint, + OUT serialized bigint, + OUT bits_per_key float8) +RETURNS record +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- The coordinator's decision about a merged payload, on a synthetic part. +CREATE FUNCTION anser_test_worth_delivering( + bitset_bytes int4, + bytes_set int4, + damage text) +RETURNS bool +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- Producer end to end: ":". +CREATE FUNCTION anser_test_producer_decision( + total_elems bigint, + cap bigint, + n_keys int4) +RETURNS text +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- The formatted QE -> QD message, verbatim. +CREATE FUNCTION anser_test_wire_format( + kind "char", + payload_type "char", + flags int4, + condition_key text, + body bytea) +RETURNS text +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- Format, alter one byte, parse: reports what the reader made of it. +CREATE FUNCTION anser_test_wire_roundtrip( + condition_key text, + body bytea, + tamper text, + payload_type "char", + kind "char") +RETURNS text +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- The QD -> QE checksum, so a test can compare two of them. +CREATE FUNCTION anser_test_push_crc( + payload_type "char", + condition_id int4, + session_id int4, + command_count int4, + flags int4, + total_len int4, + chunk_offset int4, + condition_key text, + body bytea) +RETURNS int8 +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- The runtime-filter pushdown decision, over a synthetic scan. `tlist` +-- describes the scan's output targetlist, one element per entry: a positive +-- value is a plain Var on that table attno, and the rest are the shapes that +-- must be refused -- 0 a whole-row Var, -1 a system column, -2 a computed +-- column, -3 a Var of another relation, -4 a cast wrapping a Var. +-- +-- sk_attno is the attribute number the scan key would carry, and is NULL when +-- the shape is refused. +CREATE FUNCTION anser_test_pushdown_accepts( + tlist int4[], + attno int4, + filter_in_seqscan bool, + as_seqscan bool, + OUT accepts bool, + OUT sk_attno int4) +RETURNS record +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- The same scan put through AnserPushdownTarget, which must agree and name the +-- node itself when there is nothing to descend through. +CREATE FUNCTION anser_test_pushdown_target( + tlist int4[], + attno int4, + filter_in_seqscan bool, + as_seqscan bool, + OUT found_self bool, + OUT sk_attno int4) +RETURNS record +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- How many producers a slice runs, which is what a channel waits for. +-- FillSliceGangInfo() has one formula per gang type, so `gang` selects it: +-- 'unallocated', 'entrydb', 'singleton', 'reader' or 'writer'. `direct` is +-- the direct-dispatch content list, or NULL for none. `slice_index` lets a +-- caller ask about a slice outside the array. +CREATE FUNCTION anser_test_slice_producers( + gang text, + numsegments int4, + parallel_workers int4, + direct int4[], + slice_index int4) +RETURNS int4 +AS 'MODULE_PATHNAME' +LANGUAGE C CALLED ON NULL INPUT; + +-- Withdrawals the coordinator has honoured in this backend, so a test can see +-- that a consumer which ran out of time actually said so. +CREATE FUNCTION anser_test_unsubscribes() +RETURNS int8 +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- Deliveries the coordinator has completed in this backend, so a test can tell +-- "the filter arrived" apart from "the consumer gave up and ran unfiltered". +CREATE FUNCTION anser_test_deliveries_completed() +RETURNS int8 +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; diff --git a/gpcontrib/anser/anser_test/anser_test.c b/gpcontrib/anser/anser_test/anser_test.c new file mode 100644 index 00000000000..e69ce6d3e1b --- /dev/null +++ b/gpcontrib/anser/anser_test/anser_test.c @@ -0,0 +1,1090 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anser_test.c + * SQL-callable test helpers for the Anser subsystem. + * + * IDENTIFICATION + * gpcontrib/anser/anser_test/anser_test.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "access/htup_details.h" +#include "catalog/pg_type.h" +#include "anser.h" +#include "anserbloom.h" +#include "anserfilter.h" +#include "anserpayload.h" +#include "anserplan.h" +#include "ansersideband.h" +#include "cdb/cdbvars.h" +#include "common/base64.h" +#include "fmgr.h" +#include "funcapi.h" +#include "lib/bloomfilter.h" +#include "nodes/makefuncs.h" +#include "miscadmin.h" +#include "utils/array.h" +#include "utils/builtins.h" +#include "varatt.h" + +/* + * Its own library, so its own magic block: this used to be compiled into + * anser.so and covered by the one in anserinit.c. + */ +PG_MODULE_MAGIC; + +/* + * Bloom sizing used by the test helpers. bloom_create floors every filter at + * 1 MB, so these are the smallest filters we can build; producer and consumer + * sides must pass the identical pair (that is the whole point of carrying the + * parameters in the node rather than on the wire). + */ +#define ANSER_TEST_ELEMS 32 + +/* + * bloom_create floors every bitset at 1 MB, so a payload cap must be 1 MB + * *plus* room for the serialized-part header -- the same allowance the planner + * makes (ANSER_RF_HEADER_ROOM). Passing a flat 1 MB asks AnserBloomCreate for + * a filter that cannot fit the cap it was given, and it now declines. + */ +#define ANSER_TEST_MAX_PAYLOAD (1024 * 1024 + 64) + +PG_FUNCTION_INFO_V1(anser_test_bloom_roundtrip); +PG_FUNCTION_INFO_V1(anser_test_bloom_fold_inplace); +PG_FUNCTION_INFO_V1(anser_test_bloom_rejects_mismatch); +PG_FUNCTION_INFO_V1(anser_test_node_roundtrip); + + +Datum +anser_test_bloom_roundtrip(PG_FUNCTION_ARGS) +{ + char *key = text_to_cstring(PG_GETARG_TEXT_PP(0)); + int32 value_arg = PG_GETARG_INT32(1); + Datum value = Int32GetDatum(value_arg); + uint64 seed = AnserBloomSeed(key); + bloom_filter *filter; + bloom_filter *roundtrip; + char *payload; + Size payload_size; + Size payload_len = 0; + uint32 part_index = 0; + uint32 total_parts = 0; + bool lacks; + + filter = AnserBloomCreate(ANSER_TEST_ELEMS, ANSER_TEST_MAX_PAYLOAD, seed); + if (filter == NULL) + PG_RETURN_BOOL(false); + + bloom_add_element(filter, (unsigned char *) &value, sizeof(Datum)); + payload_size = AnserBloomSerializedSize(filter); + payload = palloc(payload_size); + if (!AnserBloomSerializePart(filter, 0, 1, payload, payload_size, + &payload_len)) + PG_RETURN_BOOL(false); + + roundtrip = AnserBloomDeserializePart(payload, payload_len, + ANSER_TEST_ELEMS, ANSER_TEST_MAX_PAYLOAD, + seed, &part_index, &total_parts); + if (roundtrip == NULL) + PG_RETURN_BOOL(false); + + lacks = bloom_lacks_element(roundtrip, (unsigned char *) &value, + sizeof(Datum)); + bloom_free(filter); + bloom_free(roundtrip); + PG_RETURN_BOOL(!lacks && part_index == 0 && total_parts == 1); +} + +/* + * In-place fold: folding an equally-sized part into a merged part is a bitwise + * OR of the bitset, mutating the buffer without realloc. This is the + * coordinator's only combine path: the first part is stored verbatim, every + * later part folds in here. The header is not touched -- total_parts names the + * channel's producer count, which every part agrees on and folding does not + * change. A differently-sized part is rejected and leaves the accumulator + * untouched. + */ +Datum +anser_test_bloom_fold_inplace(PG_FUNCTION_ARGS) +{ + uint64 seed = AnserBloomSeed("inplace_bloom"); + bloom_filter *left; + bloom_filter *right; + bloom_filter *big; + bloom_filter *merged; + Datum left_value = Int32GetDatum(7); + Datum right_value = Int32GetDatum(9); + char *acc; + char *part; + char *big_part; + Size acc_size; + Size part_size; + Size big_size; + Size acc_len = 0; + Size part_len = 0; + Size big_len = 0; + uint32 part_index = 0; + uint32 total_parts = 0; + uint32 tp_before = 0; + uint32 tp_after = 0; + bool same_ok; + bool mismatch_rejected; + + /* Two same-parameter parts: acc is the running merged part, part folds in. */ + left = AnserBloomCreate(ANSER_TEST_ELEMS, ANSER_TEST_MAX_PAYLOAD, seed); + right = AnserBloomCreate(ANSER_TEST_ELEMS, ANSER_TEST_MAX_PAYLOAD, seed); + if (left == NULL || right == NULL) + PG_RETURN_BOOL(false); + bloom_add_element(left, (unsigned char *) &left_value, sizeof(Datum)); + bloom_add_element(right, (unsigned char *) &right_value, sizeof(Datum)); + acc_size = AnserBloomSerializedSize(left); + part_size = AnserBloomSerializedSize(right); + acc = palloc(acc_size); + part = palloc(part_size); + if (!AnserBloomSerializePart(left, 0, 1, acc, acc_size, &acc_len) || + !AnserBloomSerializePart(right, 0, 1, part, part_size, &part_len)) + { + bloom_free(left); + bloom_free(right); + PG_RETURN_BOOL(false); + } + bloom_free(left); + bloom_free(right); + + same_ok = AnserBloomFoldPartInPlace(acc, acc_len, part, part_len); + merged = same_ok ? + AnserBloomDeserializePart(acc, acc_len, ANSER_TEST_ELEMS, + ANSER_TEST_MAX_PAYLOAD, seed, + &part_index, &total_parts) : NULL; + same_ok = same_ok && + acc_len == acc_size && /* size unchanged, folded in place */ + merged != NULL && + part_index == 0 && + total_parts == 1 && /* the fold leaves the header alone */ + !bloom_lacks_element(merged, (unsigned char *) &left_value, + sizeof(Datum)) && + !bloom_lacks_element(merged, (unsigned char *) &right_value, + sizeof(Datum)); + if (merged != NULL) + bloom_free(merged); + + /* + * A differently-sized part must be rejected and leave acc untouched. Since + * bloom_create floors every filter at 1 MB, we need a genuinely larger + * cardinality/budget to get a bigger (2 MB) bitset than the 1 MB acc. + */ + big = AnserBloomCreate(1500000, 4 * 1024 * 1024, seed); + if (big == NULL) + PG_RETURN_BOOL(false); + big_size = AnserBloomSerializedSize(big); + big_part = palloc(big_size); + if (!AnserBloomSerializePart(big, 0, 1, big_part, big_size, &big_len)) + { + bloom_free(big); + PG_RETURN_BOOL(false); + } + bloom_free(big); + + tp_before = ((const AnserBloomPartHeader *) acc)->total_parts; + mismatch_rejected = big_len != acc_len && + !AnserBloomFoldPartInPlace(acc, acc_len, big_part, big_len); + tp_after = ((const AnserBloomPartHeader *) acc)->total_parts; + mismatch_rejected = mismatch_rejected && tp_before == tp_after; + + PG_RETURN_BOOL(same_ok && mismatch_rejected); +} + +/* + * Safety regression for the size/format check in AnserBloomDeserializePart. + * + * The consumer rebuilds the filter from its OWN (total_elems, max_payload, seed) + * parameters, then requires the received bitset to be exactly the size those + * parameters imply and the wire header to carry the expected magic. A + * well-formed part must load; a truncated one, an oversized one, and one with a + * corrupted magic must all be rejected (NULL) so the consumer fails open rather + * than loading a wrongly-shaped bitset. Returns true iff the good part loads and + * every bad one is rejected. + */ +Datum +anser_test_bloom_rejects_mismatch(PG_FUNCTION_ARGS) +{ + uint64 seed = AnserBloomSeed("reject_mismatch"); + bloom_filter *filter; + char *good; + Size good_size; + Size good_len = 0; + bloom_filter *ok_load; + bloom_filter *short_load; + bloom_filter *long_load; + bloom_filter *magic_load; + AnserBloomPartHeader *hdr; + uint32 saved_magic; + bool ok; + + filter = AnserBloomCreate(ANSER_TEST_ELEMS, ANSER_TEST_MAX_PAYLOAD, seed); + if (filter == NULL) + PG_RETURN_BOOL(false); + + good_size = AnserBloomSerializedSize(filter); + good = palloc(good_size); + if (!AnserBloomSerializePart(filter, 0, 1, good, good_size, &good_len)) + { + bloom_free(filter); + PG_RETURN_BOOL(false); + } + bloom_free(filter); + + /* Well-formed: loads. */ + ok_load = AnserBloomDeserializePart(good, good_len, ANSER_TEST_ELEMS, + ANSER_TEST_MAX_PAYLOAD, seed, NULL, NULL); + + /* One byte short of the expected bitset: rejected. */ + short_load = AnserBloomDeserializePart(good, good_len - 1, ANSER_TEST_ELEMS, + ANSER_TEST_MAX_PAYLOAD, seed, NULL, NULL); + + /* Claiming more bytes than the expected bitset: rejected. */ + long_load = AnserBloomDeserializePart(good, good_len + 1, ANSER_TEST_ELEMS, + ANSER_TEST_MAX_PAYLOAD, seed, NULL, NULL); + + /* Corrupted wire magic: rejected before the size check. */ + hdr = (AnserBloomPartHeader *) good; + saved_magic = hdr->magic; + hdr->magic = saved_magic ^ 0xFFFFFFFFU; + magic_load = AnserBloomDeserializePart(good, good_len, ANSER_TEST_ELEMS, + ANSER_TEST_MAX_PAYLOAD, seed, NULL, NULL); + hdr->magic = saved_magic; + + ok = ok_load != NULL && short_load == NULL && long_load == NULL && + magic_load == NULL; + + if (ok_load != NULL) + bloom_free(ok_load); + if (short_load != NULL) + bloom_free(short_load); + if (long_load != NULL) + bloom_free(long_load); + if (magic_load != NULL) + bloom_free(magic_load); + pfree(good); + + PG_RETURN_BOOL(ok); +} + +/* Send a cancel request for conn's in-flight query (best effort). */ +/* Printable name for a channel state ("UNKNOWN" when out of range). */ +/* + * Drive a producer and a consumer through the whole path in this one backend. + * + * Coordinator-local, so it exercises the merge, the channel table and the + * lifetime rules without needing segments; the segment half (NOTIFY out, + * sideband message in) is covered by the runtime-filter test on a cluster. + */ +Datum +anser_test_node_roundtrip(PG_FUNCTION_ARGS) +{ + AnserChannelKey key; + AnserBloomFilterProduceState *producer; + AnserBloomFilterConsumeState *consumer; + int32 value_arg = PG_GETARG_INT32(0); + Datum value = Int32GetDatum(value_arg); + bool ok = false; + + MemSet(&key, 0, sizeof(key)); + key.gp_session_id = gp_session_id; + key.gp_command_count = gp_command_count; + key.condition_id = 77; + strlcpy(key.condition_key, "node_roundtrip", ANSER_CONDITION_KEY_SIZE); + + PG_TRY(); + { + producer = ExecInitAnserBloomFilterProduce(&key, ANSER_TEST_ELEMS, + ANSER_TEST_MAX_PAYLOAD, 0, 1); + if (producer == NULL) + ok = false; + else + { + ExecAnserBloomFilterProduceAddDatum(producer, value, false); + ok = ExecAnserBloomFilterProducePublish(producer); + ExecEndAnserBloomFilterProduce(producer); + } + + if (ok) + { + consumer = ExecInitAnserBloomFilterConsume(&key, ANSER_TEST_ELEMS, + ANSER_TEST_MAX_PAYLOAD, 1); + if (consumer == NULL) + ok = false; + else + { + ok = ExecAnserBloomFilterConsume(consumer, 1000) && + ExecAnserBloomFilterConsumerGetFilter(consumer) != NULL && + ExecAnserBloomFilterConsumerReceivedParts(consumer) == 1 && + !ExecAnserBloomFilterConsumerWasCancelled(consumer) && + !bloom_lacks_element(ExecAnserBloomFilterConsumerGetFilter(consumer), + (unsigned char *) &value, + sizeof(Datum)); + ExecEndAnserBloomFilterConsume(consumer); + } + } + } + PG_FINALLY(); + { + AnserSidebandResetAll(); + } + PG_END_TRY(); + + PG_RETURN_BOOL(ok); +} + +/* + * --------------------------------------------------------------------------- + * Sizing and give-up decisions. + * + * These four take their inputs as arguments and return what Anser decided, so + * the case tables live in sql/anser_test.sql where they are readable and the + * expected output records real sizes rather than a bare "ok". Between them + * they cover each of the four points where Anser can decide not to bother: + * the planner, filter construction, publication, and the merged result. + * --------------------------------------------------------------------------- + */ + +PG_FUNCTION_INFO_V1(anser_test_rf_size); +PG_FUNCTION_INFO_V1(anser_test_bloom_shape); +PG_FUNCTION_INFO_V1(anser_test_worth_delivering); +PG_FUNCTION_INFO_V1(anser_test_producer_decision); + +/* + * The planner gate: what AnserRuntimeFilterSize decides for an estimated build + * cardinality. NULL means no filter would be injected at all. + */ +Datum +anser_test_rf_size(PG_FUNCTION_ARGS) +{ + double est_rows = PG_GETARG_FLOAT8(0); + int64 total_elems = 0; + int64 max_payload = 0; + int64 planned_bytes = 0; + bool injected; + Datum values[4] = {0, 0, 0, 0}; + bool nulls[4] = {false, false, false, false}; + TupleDesc tupdesc; + + if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) + elog(ERROR, "anser_test_rf_size: expected a composite return type"); + tupdesc = BlessTupleDesc(tupdesc); + + injected = AnserRuntimeFilterSize(est_rows, &total_elems, &max_payload, + &planned_bytes); + + values[0] = BoolGetDatum(injected); + values[1] = Int64GetDatum(total_elems); + values[2] = Int64GetDatum(max_payload); + values[3] = Int64GetDatum(planned_bytes); + nulls[1] = nulls[2] = nulls[3] = !injected; + + PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); +} + +/* + * Filter construction: what AnserBloomCreate makes of (total_elems, cap). + * NULL means it declined -- either the smallest possible filter does not fit + * the cap, or there are too many keys for it to be worth building. + * + * Returns the realized sizes so the expected output pins down bloom_create's + * actual behaviour (its 1 MB floor, its power-of-two rounding) and not merely + * our verdict on it. + */ +Datum +anser_test_bloom_shape(PG_FUNCTION_ARGS) +{ + int64 total_elems = PG_GETARG_INT64(0); + int64 cap = PG_GETARG_INT64(1); + bloom_filter *filter; + Datum values[4] = {0, 0, 0, 0}; + bool nulls[4] = {false, false, false, false}; + TupleDesc tupdesc; + + if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) + elog(ERROR, "anser_test_bloom_shape: expected a composite return type"); + tupdesc = BlessTupleDesc(tupdesc); + + /* A negative cap would be a Size wraparound; the SQL side never sends one. */ + if (cap < 0) + elog(ERROR, "anser_test_bloom_shape: negative cap"); + + filter = AnserBloomCreate(total_elems, (Size) cap, AnserBloomSeed("shape")); + + values[0] = BoolGetDatum(filter != NULL); + nulls[1] = nulls[2] = nulls[3] = (filter == NULL); + if (filter != NULL) + { + values[1] = Int64GetDatum((int64) bloom_total_bits(filter)); + values[2] = Int64GetDatum((int64) AnserBloomSerializedSize(filter)); + values[3] = Float8GetDatum((double) bloom_total_bits(filter) / + (double) total_elems); + bloom_free(filter); + } + + PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); +} + +/* + * The coordinator's gate, on a synthetic part. + * + * Builds a part whose bitset has 'bytes_set' of its 'bitset_bytes' bytes fully + * set, which pins the fill fraction exactly -- reaching a given fill by + * inserting keys would be both slow and only statistically precise. 'damage' + * corrupts the framing instead: 'magic', 'version', 'parts' or 'null'. + */ +Datum +anser_test_worth_delivering(PG_FUNCTION_ARGS) +{ + int32 bitset_bytes = PG_GETARG_INT32(0); + int32 bytes_set = PG_GETARG_INT32(1); + const char *damage = text_to_cstring(PG_GETARG_TEXT_PP(2)); + AnserBloomPartHeader *header; + char *payload; + Size payload_len; + + if (strcmp(damage, "null") == 0) + PG_RETURN_BOOL(AnserBloomPartWorthSending(NULL, 1024)); + + if (bitset_bytes < 0 || bytes_set < 0 || bytes_set > bitset_bytes) + elog(ERROR, "anser_test_worth_delivering: bad bitset arguments"); + + payload_len = sizeof(AnserBloomPartHeader) + (Size) bitset_bytes; + payload = palloc0(payload_len); + header = (AnserBloomPartHeader *) payload; + header->magic = ANSER_BLOOM_PART_MAGIC; + header->version = ANSER_BLOOM_PART_VERSION; + header->part_index = 0; + header->total_parts = 1; + + if (strcmp(damage, "magic") == 0) + header->magic = ANSER_BLOOM_PART_MAGIC + 1; + else if (strcmp(damage, "version") == 0) + header->version = ANSER_BLOOM_PART_VERSION + 1; + else if (strcmp(damage, "parts") == 0) + header->total_parts = 0; + else if (damage[0] != '\0') + elog(ERROR, "anser_test_worth_delivering: unknown damage \"%s\"", damage); + + if (bytes_set > 0) + memset(payload + sizeof(AnserBloomPartHeader), 0xff, (Size) bytes_set); + + PG_RETURN_BOOL(AnserBloomPartWorthSending(payload, payload_len)); +} + +/* + * Producer end to end, on the coordinator-local path: build a filter for + * (total_elems, cap), insert 'n_keys' distinct keys, publish, then consume. + * + * Returns ":", where construction is built/no-filter + * and delivery is delivered/cancelled/missing. Every combination that can + * occur says something different: + * + * built:delivered the normal case + * built:cancelled the filter saturated, so publication became a cancel + * no-filter:cancelled construction declined, cancelled before any scan + * + * Each call takes a fresh condition_id, since several rows of one query share a + * session and command counter and would otherwise collide on one channel. + */ +Datum +anser_test_producer_decision(PG_FUNCTION_ARGS) +{ + static uint32 next_condition_id = 1000; + + int64 total_elems = PG_GETARG_INT64(0); + int64 cap = PG_GETARG_INT64(1); + int32 n_keys = PG_GETARG_INT32(2); + AnserChannelKey key; + AnserBloomFilterProduceState *producer; + const char *construction; + const char *delivery; + void *payload = NULL; + Size payload_len = 0; + bool cancelled = false; + int32 i; + + MemSet(&key, 0, sizeof(key)); + key.gp_session_id = gp_session_id; + key.gp_command_count = gp_command_count; + key.condition_id = next_condition_id++; + snprintf(key.condition_key, ANSER_CONDITION_KEY_SIZE, "anser_rf_%u", + key.condition_id); + + producer = ExecInitAnserBloomFilterProduce(&key, total_elems, (Size) cap, + 0, 1); + if (producer == NULL) + PG_RETURN_TEXT_P(cstring_to_text("no-producer:missing")); + + construction = ExecAnserBloomFilterProduceHasFilter(producer) + ? "built" : "no-filter"; + + for (i = 0; i < n_keys; i++) + { + Datum value = Int32GetDatum(i); + + ExecAnserBloomFilterProduceAddDatum(producer, value, false); + } + + (void) ExecAnserBloomFilterProducePublish(producer); + ExecEndAnserBloomFilterProduce(producer); + + if (!AnserDispatchLocalConsume(&key, ANSER_PAYLOAD_BLOOM, &payload, + &payload_len, &cancelled)) + delivery = cancelled ? "cancelled" : "missing"; + else + delivery = "delivered"; + + if (payload != NULL) + pfree(payload); + + PG_RETURN_TEXT_P(cstring_to_text(psprintf("%s:%s", construction, delivery))); +} + +/* + * --------------------------------------------------------------------------- + * The wire protocol. + * + * Not exhaustive -- a fuzzer would be the right tool for that, and pg_regress + * is not one. What these cover is the part of the format that is easy to get + * wrong and expensive to get wrong: that the header is a fixed width with the + * fields where they are documented, that a payload cannot be mistaken for + * framing (the reason the newline-delimited format was replaced), that the + * length cross-check rejects a message that does not add up, and that the + * checksum rejects a single altered byte anywhere it is supposed to cover -- + * and does not reject one where it deliberately does not. + * --------------------------------------------------------------------------- + */ + +PG_FUNCTION_INFO_V1(anser_test_wire_format); +PG_FUNCTION_INFO_V1(anser_test_wire_roundtrip); +PG_FUNCTION_INFO_V1(anser_test_push_crc); + +/* Fixed channel coordinates, so the golden headers below are stable. */ +#define ANSER_TEST_WIRE_SESSION 42 +#define ANSER_TEST_WIRE_COMMAND 7 +#define ANSER_TEST_WIRE_CONDITION 3 +#define ANSER_TEST_WIRE_PART 1 +#define ANSER_TEST_WIRE_TOTAL 3 + +static void +anser_test_wire_key(AnserChannelKey *key, const char *condition_key) +{ + MemSet(key, 0, sizeof(*key)); + key->gp_session_id = ANSER_TEST_WIRE_SESSION; + key->gp_command_count = ANSER_TEST_WIRE_COMMAND; + key->condition_id = ANSER_TEST_WIRE_CONDITION; + strlcpy(key->condition_key, condition_key, ANSER_CONDITION_KEY_SIZE); +} + +/* + * The formatted message, verbatim. + * + * Returning it as text is itself a check: a NOTIFY payload travels through + * pq_sendstring and so must be free of NUL bytes, and a text Datum cannot + * carry one -- which is why the body is base64 even though the header is not. + * Pass a body containing NULs and the length in the expected output proves it. + */ +Datum +anser_test_wire_format(PG_FUNCTION_ARGS) +{ + char kind = PG_GETARG_CHAR(0); + char payload_type = PG_GETARG_CHAR(1); + int32 flags = PG_GETARG_INT32(2); + char *condition_key = text_to_cstring(PG_GETARG_TEXT_PP(3)); + bytea *body = PG_GETARG_BYTEA_PP(4); + AnserChannelKey key; + char *msg; + + anser_test_wire_key(&key, condition_key); + msg = AnserWireFormat(&key, kind, payload_type, ANSER_TEST_WIRE_PART, + ANSER_TEST_WIRE_TOTAL, flags, + VARDATA_ANY(body), VARSIZE_ANY_EXHDR(body)); + + PG_RETURN_TEXT_P(cstring_to_text(msg)); +} + +/* + * Format a message, optionally alter one byte of it, then parse it back and + * report what the reader made of it. + * + * The offsets touched are derived from the format macros, never hardcoded, so + * this keeps working if a field is added. Outcomes mirror what the notify + * handler does with each: "malformed" is rejected by framing before anything + * is allocated, "unknown-type" has no registry entry, "checksum-mismatch" is + * well-framed but altered, and "ok" means every field and the decoded body + * came back exactly as they went in. + */ +Datum +anser_test_wire_roundtrip(PG_FUNCTION_ARGS) +{ + char *condition_key = text_to_cstring(PG_GETARG_TEXT_PP(0)); + bytea *body = PG_GETARG_BYTEA_PP(1); + char *tamper = text_to_cstring(PG_GETARG_TEXT_PP(2)); + char payload_type = PG_GETARG_CHAR(3); + char kind = PG_GETARG_CHAR(4); + AnserChannelKey key; + AnserWireMsg parsed; + char *msg; + Size msg_len; + const char *raw_body = VARDATA_ANY(body); + int raw_len = VARSIZE_ANY_EXHDR(body); + char *decoded = NULL; + int decoded_len = 0; + + anser_test_wire_key(&key, condition_key); + msg = AnserWireFormat(&key, kind, payload_type, + ANSER_TEST_WIRE_PART, ANSER_TEST_WIRE_TOTAL, 0, + raw_len > 0 ? raw_body : NULL, (Size) raw_len); + msg_len = strlen(msg); + + /* + * One byte, or one length, altered -- see the case table in the test. An + * empty 'tamper' falls through every branch and leaves the message intact. + */ + if (strcmp(tamper, "truncate") == 0) + msg[msg_len - 1] = '\0'; + else if (strcmp(tamper, "append") == 0) + { + char *longer = palloc(msg_len + 2); + + memcpy(longer, msg, msg_len); + longer[msg_len] = 'x'; + longer[msg_len + 1] = '\0'; + msg = longer; + } + else if (strcmp(tamper, "bodylen") == 0) + { + /* Last digit of the bodylen field: it ends one space before the CRC. */ + char *digit = msg + ANSER_WIRE_CRC_OFFSET - 2; + + *digit = (*digit == '9') ? '8' : (char) (*digit + 1); + } + else if (strcmp(tamper, "tag") == 0) + msg[0] = 'x'; + else if (strcmp(tamper, "kind") == 0) + msg[sizeof(ANSER_WIRE_TAG)] = ANSER_WIRE_KIND_SUBSCRIBE; + else if (strcmp(tamper, "type") == 0) + msg[sizeof(ANSER_WIRE_TAG) + 2] = 'Z'; + else if (strcmp(tamper, "crc") == 0) + { + char *digit = msg + ANSER_WIRE_HDR_LEN - 1; + + *digit = (*digit == '0') ? '1' : '0'; + } + else if (strcmp(tamper, "key") == 0) + { + char *first = msg + ANSER_WIRE_HDR_LEN; + + *first = (*first == 'a') ? 'b' : 'a'; + } + else if (strcmp(tamper, "body") == 0) + { + /* Stay inside the base64 alphabet so this tests the CRC, not decoding. */ + char *first = msg + ANSER_WIRE_HDR_LEN + strlen(condition_key); + + *first = (*first == 'A') ? 'B' : 'A'; + } + else if (strcmp(tamper, "") != 0) + elog(ERROR, "anser_test_wire_roundtrip: unknown tamper \"%s\"", tamper); + + if (!AnserWireParse(msg, &parsed)) + PG_RETURN_TEXT_P(cstring_to_text("malformed")); + + if (AnserPayloadLookup(parsed.payload_type) == NULL) + PG_RETURN_TEXT_P(cstring_to_text("unknown-type")); + + if (parsed.body_len > 0) + { + int maxlen = pg_b64_dec_len(parsed.body_len); + + decoded = palloc(maxlen); + decoded_len = pg_b64_decode(parsed.body, parsed.body_len, decoded, + maxlen); + if (decoded_len < 0) + PG_RETURN_TEXT_P(cstring_to_text("undecodable")); + } + + if (!AnserWireCheckCrc(&parsed, decoded, (Size) decoded_len)) + PG_RETURN_TEXT_P(cstring_to_text("checksum-mismatch")); + + /* Framed and vouched for: now every field must have survived the trip. */ + if (parsed.kind != kind) + PG_RETURN_TEXT_P(cstring_to_text("mismatch: kind")); + if (parsed.payload_type != payload_type) + PG_RETURN_TEXT_P(cstring_to_text("mismatch: payload_type")); + if (parsed.key.gp_session_id != ANSER_TEST_WIRE_SESSION || + parsed.key.gp_command_count != ANSER_TEST_WIRE_COMMAND || + parsed.key.condition_id != ANSER_TEST_WIRE_CONDITION) + PG_RETURN_TEXT_P(cstring_to_text("mismatch: channel")); + if (parsed.part_index != ANSER_TEST_WIRE_PART || + parsed.total_parts != ANSER_TEST_WIRE_TOTAL) + PG_RETURN_TEXT_P(cstring_to_text("mismatch: part")); + if (parsed.flags != 0) + PG_RETURN_TEXT_P(cstring_to_text("mismatch: flags")); + if (strcmp(parsed.key.condition_key, condition_key) != 0) + PG_RETURN_TEXT_P(cstring_to_text("mismatch: condition_key")); + if (decoded_len != raw_len || + (raw_len > 0 && memcmp(decoded, raw_body, raw_len) != 0)) + PG_RETURN_TEXT_P(cstring_to_text("mismatch: body")); + + PG_RETURN_TEXT_P(cstring_to_text("ok")); +} + +/* + * The QD -> QE checksum, as an integer so the test can compare two of them. + * + * What matters is not the value but which inputs change it: every routing field + * and the key always, and the body only for a payload type that asks for its + * body to be covered. + */ +Datum +anser_test_push_crc(PG_FUNCTION_ARGS) +{ + char payload_type = PG_GETARG_CHAR(0); + int32 condition_id = PG_GETARG_INT32(1); + int32 session_id = PG_GETARG_INT32(2); + int32 command_count = PG_GETARG_INT32(3); + int32 flags = PG_GETARG_INT32(4); + int32 total_len = PG_GETARG_INT32(5); + int32 offset = PG_GETARG_INT32(6); + char *condition_key = text_to_cstring(PG_GETARG_TEXT_PP(7)); + bytea *body = PG_GETARG_BYTEA_PP(8); + + PG_RETURN_INT64((int64) (uint32) + AnserWirePushCrc(payload_type, (uint32) condition_id, + (uint32) session_id, (uint32) command_count, + (uint32) flags, (uint32) total_len, + (uint32) offset, condition_key, + (int) strlen(condition_key), + VARDATA_ANY(body), + (int) VARSIZE_ANY_EXHDR(body))); +} + +/* + * --------------------------------------------------------------------------- + * Runtime-filter pushdown: the decision, on its own + * --------------------------------------------------------------------------- + */ + +PG_FUNCTION_INFO_V1(anser_test_pushdown_accepts); +PG_FUNCTION_INFO_V1(anser_test_pushdown_target); + +/* The relation the synthetic scan below claims to be scanning. */ +#define ANSER_TEST_SCANRELID 1 + +/* + * Targetlist entry kinds, as the SQL-visible spec encodes them. A positive + * value is the table attno of a plain Var; the rest are the shapes that must + * be refused. + */ +#define ANSER_TEST_TL_WHOLEROW 0 /* Var, varattno 0 */ +#define ANSER_TEST_TL_SYSCOL (-1) /* Var, negative varattno */ +#define ANSER_TEST_TL_EXPR (-2) /* not a Var at all */ +#define ANSER_TEST_TL_OTHERREL (-3) /* Var of a different RTE */ +#define ANSER_TEST_TL_RELABEL (-4) /* a cast wrapping a Var */ + +/* + * Build the targetlist `spec` describes. Each element becomes one entry, in + * order, so the array index doubles as the output position the decision is + * asked about. + */ +static List * +anser_test_make_tlist(ArrayType *spec) +{ + Datum *elems; + bool *nulls; + int nelems; + List *tlist = NIL; + int i; + + if (ARR_NDIM(spec) > 1) + elog(ERROR, "anser_test: targetlist spec must be one-dimensional"); + + deconstruct_array(spec, INT4OID, sizeof(int32), true, TYPALIGN_INT, + &elems, &nulls, &nelems); + + for (i = 0; i < nelems; i++) + { + int32 kind; + Expr *expr; + + if (nulls[i]) + elog(ERROR, "anser_test: targetlist spec may not contain NULLs"); + kind = DatumGetInt32(elems[i]); + + switch (kind) + { + case ANSER_TEST_TL_EXPR: + + /* + * The decision only asks whether the entry is a Var, so any + * non-Var exercises the same branch. An OpExpr is what a + * computed column really looks like; nothing evaluates it. + */ + expr = (Expr *) makeNode(OpExpr); + break; + case ANSER_TEST_TL_OTHERREL: + expr = (Expr *) makeVar(ANSER_TEST_SCANRELID + 1, 1, INT4OID, + -1, InvalidOid, 0); + break; + case ANSER_TEST_TL_RELABEL: + { + RelabelType *rt = makeNode(RelabelType); + + rt->arg = (Expr *) makeVar(ANSER_TEST_SCANRELID, 1, + INT4OID, -1, InvalidOid, 0); + rt->resulttype = OIDOID; + rt->relabelformat = COERCE_IMPLICIT_CAST; + expr = (Expr *) rt; + } + break; + default: + /* whole-row (0), system column (negative), or a real column */ + expr = (Expr *) makeVar(ANSER_TEST_SCANRELID, + (AttrNumber) kind, INT4OID, -1, + InvalidOid, 0); + break; + } + + tlist = lappend(tlist, makeTargetEntry(expr, (AttrNumber) (i + 1), + NULL, false)); + } + + return tlist; +} + +/* + * A SeqScanState over that targetlist. Nothing is executed, so only the + * fields the decision reads are set: the node tag, filter_in_seqscan, and the + * plan's targetlist and scanrelid. Passing as_seqscan = false substitutes a + * node of another type, which must be refused whatever its targetlist says. + */ +static PlanState * +anser_test_make_scanstate(ArrayType *spec, bool filter_in_seqscan, + bool as_seqscan) +{ + SeqScan *scan = makeNode(SeqScan); + + scan->scan.scanrelid = ANSER_TEST_SCANRELID; + scan->scan.plan.targetlist = anser_test_make_tlist(spec); + + if (!as_seqscan) + { + MaterialState *ms = makeNode(MaterialState); + + ms->ss.ps.plan = (Plan *) scan; + return (PlanState *) ms; + } + + { + SeqScanState *sss = makeNode(SeqScanState); + + sss->ss.ps.plan = (Plan *) scan; + sss->filter_in_seqscan = filter_in_seqscan; + return (PlanState *) sss; + } +} + +/* + * AnserPushdownAccepts on a synthetic scan. Returns the scan key's attribute + * number, or NULL when the shape is refused -- so the expected output records + * the mapping itself and not merely a verdict on it. + */ +Datum +anser_test_pushdown_accepts(PG_FUNCTION_ARGS) +{ + ArrayType *spec = PG_GETARG_ARRAYTYPE_P(0); + AttrNumber attno = (AttrNumber) PG_GETARG_INT32(1); + bool filter_in_seqscan = PG_GETARG_BOOL(2); + bool as_seqscan = PG_GETARG_BOOL(3); + PlanState *ps; + AttrNumber sk_attno = InvalidAttrNumber; + bool accepts; + Datum values[2] = {0, 0}; + bool nulls[2] = {false, false}; + TupleDesc tupdesc; + + if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) + elog(ERROR, "anser_test_pushdown_accepts: expected a composite return type"); + tupdesc = BlessTupleDesc(tupdesc); + + ps = anser_test_make_scanstate(spec, filter_in_seqscan, as_seqscan); + accepts = AnserPushdownAccepts(ps, attno, &sk_attno); + + values[0] = BoolGetDatum(accepts); + values[1] = Int32GetDatum((int32) sk_attno); + nulls[1] = !accepts; + + PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); +} + +/* + * AnserPushdownTarget over the same synthetic scan, with no stacking: it must + * agree with AnserPushdownAccepts and report the node itself as the target. + * + * The descent through stacked nodes cannot be built here -- that needs real + * CustomScanStates, which need a live EState -- so it is covered end to end by + * the multi-join case in anser_runtime_filter instead. What this pins down is + * the part that is easy to get wrong without noticing: that a found target + * carries the mapped attno through, and that nothing is reported when the + * bottom node refuses. + */ +Datum +anser_test_pushdown_target(PG_FUNCTION_ARGS) +{ + ArrayType *spec = PG_GETARG_ARRAYTYPE_P(0); + AttrNumber attno = (AttrNumber) PG_GETARG_INT32(1); + bool filter_in_seqscan = PG_GETARG_BOOL(2); + bool as_seqscan = PG_GETARG_BOOL(3); + PlanState *ps; + PlanState *target; + AttrNumber sk_attno = InvalidAttrNumber; + Datum values[2] = {0, 0}; + bool nulls[2] = {false, false}; + TupleDesc tupdesc; + + if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) + elog(ERROR, "anser_test_pushdown_target: expected a composite return type"); + tupdesc = BlessTupleDesc(tupdesc); + + ps = anser_test_make_scanstate(spec, filter_in_seqscan, as_seqscan); + target = AnserPushdownTarget(ps, attno, &sk_attno); + + values[0] = BoolGetDatum(target == ps); + values[1] = Int32GetDatum((int32) sk_attno); + nulls[1] = (target == NULL); + + PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); +} + +/* + * --------------------------------------------------------------------------- + * How many producers a slice runs + * --------------------------------------------------------------------------- + */ + +PG_FUNCTION_INFO_V1(anser_test_slice_producers); + +/* + * AnserSliceProducers() over a hand-built PlanSlice. + * + * The count is the channel's completion criterion, and FillSliceGangInfo gives + * a different answer per gang type -- so the cases below are the four formulas + * it uses, plus the boundaries where the slice cannot be identified at all. + * Nothing is executed: the function reads the slice array and nothing else. + * + * `gang` names the gang type; `direct` is the direct-dispatch content list, + * or NULL for no direct dispatch. `slice_index` is what to ask about, so a + * caller can also probe outside the array. + */ +Datum +anser_test_slice_producers(PG_FUNCTION_ARGS) +{ + char *gang = text_to_cstring(PG_GETARG_TEXT_PP(0)); + int32 numsegments = PG_GETARG_INT32(1); + int32 parallel_workers = PG_GETARG_INT32(2); + int32 slice_index = PG_GETARG_INT32(4); + PlannedStmt *stmt = makeNode(PlannedStmt); + PlanSlice slice; + + MemSet(&slice, 0, sizeof(slice)); + + if (strcmp(gang, "unallocated") == 0) + slice.gangType = GANGTYPE_UNALLOCATED; + else if (strcmp(gang, "entrydb") == 0) + slice.gangType = GANGTYPE_ENTRYDB_READER; + else if (strcmp(gang, "singleton") == 0) + slice.gangType = GANGTYPE_SINGLETON_READER; + else if (strcmp(gang, "reader") == 0) + slice.gangType = GANGTYPE_PRIMARY_READER; + else if (strcmp(gang, "writer") == 0) + slice.gangType = GANGTYPE_PRIMARY_WRITER; + else + elog(ERROR, "anser_test: unknown gang type \"%s\"", gang); + + slice.numsegments = numsegments; + slice.parallel_workers = parallel_workers; + + if (!PG_ARGISNULL(3)) + { + ArrayType *direct = PG_GETARG_ARRAYTYPE_P(3); + Datum *elems; + bool *nulls; + int nelems; + int i; + + deconstruct_array(direct, INT4OID, sizeof(int32), true, TYPALIGN_INT, + &elems, &nulls, &nelems); + + slice.directDispatch.isDirectDispatch = true; + for (i = 0; i < nelems; i++) + { + if (nulls[i]) + elog(ERROR, "anser_test: direct dispatch list may not contain NULLs"); + slice.directDispatch.contentIds = + lappend_int(slice.directDispatch.contentIds, + DatumGetInt32(elems[i])); + } + } + + stmt->numSlices = 1; + stmt->slices = &slice; + + PG_RETURN_INT32(AnserSliceProducers(stmt, slice_index)); +} + +PG_FUNCTION_INFO_V1(anser_test_unsubscribes); + +/* + * Withdrawals the coordinator has honoured in this backend. + * + * A consumer that runs out of time says so, and the coordinator drops it from + * the channel rather than delivering 1-64 MB to a process that has moved on. + * Nothing else in the system records that it happened: the channel is gone by + * the time the query returns, and a filter that simply arrived late looks the + * same from SQL. A test reads the difference across a query. + */ +Datum +anser_test_unsubscribes(PG_FUNCTION_ARGS) +{ + PG_RETURN_INT64(AnserDispatchUnsubscribeCount()); +} + +PG_FUNCTION_INFO_V1(anser_test_deliveries_completed); + +/* + * Deliveries that reached their last chunk in this backend. + * + * The counterpart of anser_test_unsubscribes: a test that wants to know the + * filter really arrived -- rather than the consumer quietly running + * unfiltered after its deadline, which produces the same rows -- reads the + * difference across a query, together with the unsubscribe count staying put. + */ +Datum +anser_test_deliveries_completed(PG_FUNCTION_ARGS) +{ + PG_RETURN_INT64(AnserDispatchDeliveriesCompleted()); +} diff --git a/gpcontrib/anser/anser_test/anser_test.control b/gpcontrib/anser/anser_test/anser_test.control new file mode 100644 index 00000000000..e4d7a9e5ac5 --- /dev/null +++ b/gpcontrib/anser/anser_test/anser_test.control @@ -0,0 +1,22 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# anser_test extension +comment = 'test helpers for the anser extension' +default_version = '1.0' +module_pathname = '$libdir/anser_test' +relocatable = true diff --git a/gpcontrib/anser/expected/anser_faults.out b/gpcontrib/anser/expected/anser_faults.out new file mode 100644 index 00000000000..43f41d49dce --- /dev/null +++ b/gpcontrib/anser/expected/anser_faults.out @@ -0,0 +1,348 @@ +-- Anser runtime bloom filter: the cases that need gp_inject_fault. +-- +-- Kept apart from anser_runtime_filter so that a build configured with +-- --disable-debug-extensions loses only these. gp_inject_fault is built only +-- with the debug extensions, and the regression driver excludes any file that +-- calls one -- see faultinjector_prep in src/Makefile.global.in -- so whatever +-- shares a file with a fault is skipped along with it. Everything that can be +-- asked for with data alone therefore stays in the other file. +-- +-- Requires the cluster to run with anser.enable=on, like the rest of the suite. +-- +-- The extension may or may not already be there, and saying so is not part of +-- what is being tested; it is never dropped, for the same reason -- it can +-- predate this test, and other tests in the same database rely on it. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS gp_inject_fault; +NOTICE: extension "gp_inject_fault" already exists, skipping +-- end_ignore +-- Deterministic plan shape: a hash join under the Postgres planner, as in +-- anser_runtime_filter. A fresh session inherits none of its settings. +SET enable_nestloop = off; +SET enable_mergejoin = off; +SET optimizer = off; +-- Trace every exchange step into the server logs (never reaches the client), +-- so a CI run that goes wrong can be diagnosed from the log artifacts. +SET anser.debug = on; +-- nextval guard: the QD sends two kinds of message down a QE's dispatch +-- connection -- the nextval reply and a sideband message -- and each has its +-- own reader. A reader that meets the other's message has to skip it whole: +-- taking the type byte and erroring leaves the body in the stream, and the +-- session then dies somewhere else with "protocol synchronization was lost". +-- +-- In the field this needs an Anser consumer to give up on a filter the +-- coordinator goes on to deliver anyway, which is a race and cannot be asked +-- for from SQL. The fault puts a sideband message in front of one nextval +-- reply instead, so the ordering is certain rather than likely; the +-- three-argument form is occurrence 1..1, so it fires once. +-- +-- Nothing here is Anser's -- the fault supplies the message and the fix is in +-- cdb_sequence_nextval_qe -- but Anser is why a second message type exists on +-- that connection at all, so the test lives with it. +-- +CREATE SEQUENCE anser_rf_seq; +CREATE TABLE anser_rf_seq_t (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_seq_t SELECT g FROM generate_series(1, 10) g; +SELECT gp_inject_fault('nextval_sideband_before_response', 'skip', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +SELECT count(nextval('anser_rf_seq')) AS seq_rows FROM anser_rf_seq_t; + seq_rows +---------- + 10 +(1 row) + +SELECT gp_inject_fault('nextval_sideband_before_response', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +DROP SEQUENCE anser_rf_seq; +DROP TABLE anser_rf_seq_t; +-- Delivery to a consumer that stopped reading must not block the coordinator. +-- +-- A merged filter is 1-64 MB and the dispatch connections are blocking, so +-- pushing to a QE that is no longer reading used to wait in pqWait() with no +-- deadline and no CHECK_FOR_INTERRUPTS -- inside the dispatcher loop, which +-- then served nobody. Neither cancel nor statement_timeout could end it. +-- +-- The ordering cannot be arranged with data: the channel completes when the +-- last part lands, which is normally while the consumers are still waiting. +-- Holding the delivery back is what lets every consumer reach its deadline +-- and move on to its probe side -- and it is while they are busy there that +-- the coordinator finally writes to all of them. +-- +-- Note this one hangs rather than fails if the fix is reverted, for the same +-- reason the bug was unkillable. It is here because the alternative is a +-- regression nobody notices until a cluster wedges. +SET anser.timeout_ms = 1; +CREATE TABLE anser_rf_push_build (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_rf_push_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_push_build SELECT g, 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_rf_push_probe SELECT g % 1000 + 1 FROM generate_series(1, 1000000) g; +ANALYZE anser_rf_push_build; +ANALYZE anser_rf_push_probe; +-- One segment publishes a second late; every other consumer gives up at 1 ms. +SELECT gp_inject_fault('anser_before_deliver', 'sleep', '', '', '', 1, 1, 1, dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +SET anser.runtime_filter = on; +SELECT count(*) AS push_rows + FROM anser_rf_push_probe p JOIN anser_rf_push_build b ON p.id = b.id; + push_rows +----------- + 1000000 +(1 row) + +SELECT gp_inject_fault('anser_before_deliver', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +DROP TABLE anser_rf_push_build, anser_rf_push_probe; +-- A consumer that runs out of time withdraws, so the coordinator stops +-- counting it as a subscriber. Without that it would keep delivering to a +-- process that has moved on -- which is what the push above has to survive, +-- and what this keeps from quietly regressing into. +-- +-- The withdrawal leaves no trace a query can see: the channel is gone by the +-- time the query returns, and a filter that merely arrived late looks the +-- same. So the coordinator counts them, and this reads the difference. +CREATE TABLE anser_rf_unsub_build (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_rf_unsub_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_unsub_build SELECT g, 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_rf_unsub_probe SELECT g % 1000 + 1 FROM generate_series(1, 200000) g; +ANALYZE anser_rf_unsub_build; +ANALYZE anser_rf_unsub_probe; +-- start_ignore +CREATE EXTENSION IF NOT EXISTS anser_test; +NOTICE: extension "anser_test" already exists, skipping +-- end_ignore +SELECT anser_test_unsubscribes() AS unsub_before \gset +SELECT gp_inject_fault('anser_before_deliver', 'sleep', '', '', '', 1, 1, 1, dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +SELECT count(*) AS unsub_rows + FROM anser_rf_unsub_probe p JOIN anser_rf_unsub_build b ON p.id = b.id; + unsub_rows +------------ + 200000 +(1 row) + +SELECT gp_inject_fault('anser_before_deliver', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +SELECT anser_test_unsubscribes() > :unsub_before AS consumers_withdrew; + consumers_withdrew +-------------------- + t +(1 row) + +DROP EXTENSION anser_test; +DROP TABLE anser_rf_unsub_build, anser_rf_unsub_probe; +-- A partially delivered filter must not wedge the next command. +-- +-- With chunked delivery there is no torn 64 MB push any more: the coordinator +-- sends one chunk and waits to be asked for the next. What can still be in +-- flight when every consumer has given up is exactly one chunk -- the QE is +-- busy on its probe side by then, so the chunk sits in the stream until the +-- command loop reads and skips it whole (postgres.c, GP_SIDEBAND_MESSAGE). +-- The next command on the same gang must answer. +SET anser.timeout_ms = 100; +SET anser.debug_min_filter_bytes = 67108864; +CREATE TABLE anser_rf_stale_build (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_rf_stale_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_stale_build SELECT g, 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_rf_stale_probe SELECT g % 1000 + 1 FROM generate_series(1, 1000000) g; +ANALYZE anser_rf_stale_build; +ANALYZE anser_rf_stale_probe; +-- Hold the delivery for 1 s so every consumer gives up at 100 ms and the +-- first chunk goes out to connections nobody is reading. +SELECT gp_inject_fault('anser_before_deliver', 'sleep', '', '', '', 1, 1, 1, dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +SET anser.runtime_filter = on; +SELECT count(*) AS stale_rows + FROM anser_rf_stale_probe p JOIN anser_rf_stale_build b ON p.id = b.id; + stale_rows +------------ + 1000000 +(1 row) + +SELECT gp_inject_fault('anser_before_deliver', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +-- The leftover chunk is skipped whole, so the next command on the same gang +-- must answer. +SELECT 'next command' AS marker, count(*) AS build_rows FROM anser_rf_stale_build; + marker | build_rows +--------------+------------ + next command | 1000 +(1 row) + +DROP TABLE anser_rf_stale_build, anser_rf_stale_probe; +RESET anser.debug_min_filter_bytes; +-- Walking away mid-delivery must end it cleanly. The fault pauses the +-- consumer after its first chunk for longer than its deadline -- as a skip +-- fault: the consumer itself waits out anser.timeout_ms plus a margin, on the +-- monotonic clock, because a 'sleep' fault is a bare nanosleep that any +-- signal cuts short. The consumer withdraws, the coordinator's delivery +-- stalls at the chunk boundary, and the one chunk in flight is discarded at +-- the door: the key is no longer subscribed, so read_one drops it without a +-- NEXT. Results must still be exact, the coordinator must record the +-- withdrawals, and the next command on the same gang must answer. +SET anser.timeout_ms = 500; +SET anser.debug_delivery_chunk_bytes = 8192; +SET anser.debug_min_filter_bytes = 67108864; +CREATE TABLE anser_rf_ab_build (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_rf_ab_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_ab_build SELECT g, 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_rf_ab_probe SELECT g % 1000 + 1 FROM generate_series(1, 200000) g; +ANALYZE anser_rf_ab_build; +ANALYZE anser_rf_ab_probe; +-- start_ignore +CREATE EXTENSION IF NOT EXISTS anser_test; +NOTICE: extension "anser_test" already exists, skipping +-- end_ignore +SELECT anser_test_unsubscribes() AS unsub_before \gset +-- Pause every consumer after its first chunk for longer than its deadline. +SELECT gp_inject_fault('anser_after_chunk', 'skip', '', '', '', 1, 1, 0, dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content >= 0; + gp_inject_fault +----------------- + Success: + Success: + Success: +(3 rows) + +SET anser.runtime_filter = on; +SELECT count(*) AS abort_rows + FROM anser_rf_ab_probe p JOIN anser_rf_ab_build b ON p.id = b.id; + abort_rows +------------ + 200000 +(1 row) + +SELECT gp_inject_fault('anser_after_chunk', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content >= 0; + gp_inject_fault +----------------- + Success: + Success: + Success: +(3 rows) + +SELECT anser_test_unsubscribes() > :unsub_before AS consumers_withdrew; + consumers_withdrew +-------------------- + t +(1 row) + +SELECT 'next command' AS marker, count(*) AS build_rows FROM anser_rf_ab_build; + marker | build_rows +--------------+------------ + next command | 1000 +(1 row) + +DROP EXTENSION anser_test; +DROP TABLE anser_rf_ab_build, anser_rf_ab_probe; +RESET anser.debug_min_filter_bytes; +RESET anser.debug_delivery_chunk_bytes; +-- A filter from one statement must not be claimed by the next. +-- +-- A consumer that runs out of time withdraws, and with chunked delivery the +-- coordinator stops at the chunk boundary: the one chunk in flight is dropped +-- at the door, because the key is no longer subscribed. And even a filter +-- that somehow got parked whole could not be claimed by the next statement: +-- condition keys restart at the same values, but the session id and command +-- count stamped on the wire must match exactly. +-- +-- The LEFT JOIN pins j2 as the build side, so its consumer waits first: it +-- gives up at 700 ms while the coordinator sleeps for 1 s, its late first +-- chunk is then read -- and dropped -- while j1's consumer waits, and the +-- division errors. The second run re-executes the same plan shape with a +-- build side disjoint from the parked filter: claiming anything stale would +-- drop every joinable row, so count(j2.id) must be 1000, not 0. +SET anser.timeout_ms = 700; +CREATE TABLE anser_sf_b1 (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_sf_p1 (id int) DISTRIBUTED BY (id); +CREATE TABLE anser_sf_b2 (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_sf_p2 (id int) DISTRIBUTED BY (id); +INSERT INTO anser_sf_b1 SELECT g, 'b' || g FROM generate_series(1, 2000) g; +INSERT INTO anser_sf_p1 SELECT g FROM generate_series(1, 2000) g; +INSERT INTO anser_sf_b2 SELECT g, 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_sf_p2 SELECT g FROM generate_series(1, 2000) g; +ANALYZE anser_sf_b1; +ANALYZE anser_sf_p1; +ANALYZE anser_sf_b2; +ANALYZE anser_sf_p2; +SELECT gp_inject_fault('anser_before_deliver', 'sleep', '', '', '', 1, 1, 1, dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +SET anser.runtime_filter = on; +BEGIN; +SAVEPOINT anser_sf; +SELECT j1.id / (j1.id - j1.id) AS boom +FROM (SELECT p1.id FROM anser_sf_p1 p1 JOIN anser_sf_b1 b1 ON p1.id = b1.id) j1 +LEFT JOIN (SELECT p2.id FROM anser_sf_p2 p2 JOIN anser_sf_b2 b2 ON p2.id = b2.id) j2 + ON j1.id = j2.id; +ERROR: division by zero +ROLLBACK TO SAVEPOINT anser_sf; +DELETE FROM anser_sf_b2; +INSERT INTO anser_sf_b2 SELECT 1000 + g, 'b' || g FROM generate_series(1, 1000) g; +SELECT count(j2.id) AS reused_rows +FROM (SELECT p1.id FROM anser_sf_p1 p1 JOIN anser_sf_b1 b1 ON p1.id = b1.id) j1 +LEFT JOIN (SELECT p2.id FROM anser_sf_p2 p2 JOIN anser_sf_b2 b2 ON p2.id = b2.id) j2 + ON j1.id = j2.id; + reused_rows +------------- + 1000 +(1 row) + +COMMIT; +SELECT gp_inject_fault('anser_before_deliver', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +DROP TABLE anser_sf_b1, anser_sf_p1, anser_sf_b2, anser_sf_p2; +RESET anser.runtime_filter; +RESET anser.debug; +RESET anser.timeout_ms; +RESET optimizer; +RESET enable_nestloop; +RESET enable_mergejoin; diff --git a/gpcontrib/anser/expected/anser_multijoin.out b/gpcontrib/anser/expected/anser_multijoin.out new file mode 100644 index 00000000000..37cbfe7453a --- /dev/null +++ b/gpcontrib/anser/expected/anser_multijoin.out @@ -0,0 +1,286 @@ +-- Anser runtime bloom filter: chains of 2..10 joins. +-- +-- Each table joins to the next on its own key pair -- t1.nxt = t2.own, then +-- t2.nxt = t3.own, and so on -- which is what a real chain looks like: no two +-- joins share a key, so no table's distribution can serve more than one of +-- them and every join needs a motion of its own. Joining a whole chain on one +-- column would make the joins co-located and would test the easy case only. +-- +-- This is also the shape the feature has to handle: the build side of the third +-- join is the result of the first two, and the key column for that join comes +-- from t2, so it reaches its base relation through a join rather than straight +-- through a Hash. +-- +-- What these cases assert is not that a filter was injected -- that is the +-- planner's choice and it differs between the two optimizers and with the +-- cluster width -- but that the answer is the same either way. A bloom filter +-- fails asymmetrically: a bit that should be set and is not silently removes +-- joinable rows, so "same rows with the filter on as with it off" is the +-- property worth asserting. Plan shapes are deliberately not asserted; an +-- expected file full of them would break on a three-segment cluster and pass on +-- a one-segment one without saying anything about correctness. +-- +-- Every query below reports its violations, so the expected output is empty +-- results and zeros. A failure names the chain that broke rather than leaving +-- a table of numbers to be compared by eye. +SET enable_nestloop = off; +SET enable_mergejoin = off; +-- A regress test must not inherit the production deadline. anser.timeout_ms +-- defaults to 10 s, which is a budget for an exchange that has gone wrong; a +-- test that waits it out has turned a delivery failure into a slow pass. +-- Pinned shorter here, so a filter that never arrives fails the run instead of +-- hiding in the clock. +SET anser.timeout_ms = 5000; +-- own is what the previous table joins to, nxt is what this table joins to the +-- next one with. They hold the same value, which keeps the chain 1:1 and the +-- answer predictable -- 1000 rows summing to 500500, whatever the join count -- +-- while still being two different columns as far as the planner is concerned. +-- +-- The distribution key is deliberately never the whole story: a table +-- distributed by own is joined on nxt, and the ones distributed by payload are +-- joined on both. Every join therefore redistributes or broadcasts one side, +-- and the producer ends up in a different slice from its consumer. +-- +-- t1 is the smallest and every later table contains it, so the chain returns +-- t1 whatever order the planner picks. The widening sizes give it a reason to +-- hash the joined result rather than the next table, which is the shape that +-- exercises the descent through a join. +CREATE TABLE anser_mj_t1 (own int, nxt int, payload text) DISTRIBUTED BY (own); +CREATE TABLE anser_mj_t2 (own int, nxt int, payload text) DISTRIBUTED BY (payload); +CREATE TABLE anser_mj_t3 (own int, nxt int, payload text) DISTRIBUTED BY (own); +CREATE TABLE anser_mj_t4 (own int, nxt int, payload text) DISTRIBUTED BY (payload); +CREATE TABLE anser_mj_t5 (own int, nxt int, payload text) DISTRIBUTED BY (own); +CREATE TABLE anser_mj_t6 (own int, nxt int, payload text) DISTRIBUTED BY (payload); +CREATE TABLE anser_mj_t7 (own int, nxt int, payload text) DISTRIBUTED BY (own); +CREATE TABLE anser_mj_t8 (own int, nxt int, payload text) DISTRIBUTED BY (payload); +CREATE TABLE anser_mj_t9 (own int, nxt int, payload text) DISTRIBUTED BY (own); +CREATE TABLE anser_mj_t10 (own int, nxt int, payload text) DISTRIBUTED BY (payload); +CREATE TABLE anser_mj_t11 (own int, nxt int, payload text) DISTRIBUTED BY (own); +INSERT INTO anser_mj_t1 SELECT g, g, 't1_' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_mj_t2 SELECT g, g, 't2_' || g FROM generate_series(1, 2000) g; +INSERT INTO anser_mj_t3 SELECT g, g, 't3_' || g FROM generate_series(1, 3000) g; +INSERT INTO anser_mj_t4 SELECT g, g, 't4_' || g FROM generate_series(1, 4000) g; +INSERT INTO anser_mj_t5 SELECT g, g, 't5_' || g FROM generate_series(1, 5000) g; +INSERT INTO anser_mj_t6 SELECT g, g, 't6_' || g FROM generate_series(1, 6000) g; +INSERT INTO anser_mj_t7 SELECT g, g, 't7_' || g FROM generate_series(1, 7000) g; +INSERT INTO anser_mj_t8 SELECT g, g, 't8_' || g FROM generate_series(1, 8000) g; +INSERT INTO anser_mj_t9 SELECT g, g, 't9_' || g FROM generate_series(1, 9000) g; +INSERT INTO anser_mj_t10 SELECT g, g, 't10_' || g FROM generate_series(1, 10000) g; +INSERT INTO anser_mj_t11 SELECT g, g, 't11_' || g FROM generate_series(1, 11000) g; +ANALYZE anser_mj_t1; +ANALYZE anser_mj_t2; +ANALYZE anser_mj_t3; +ANALYZE anser_mj_t4; +ANALYZE anser_mj_t5; +ANALYZE anser_mj_t6; +ANALYZE anser_mj_t7; +ANALYZE anser_mj_t8; +ANALYZE anser_mj_t9; +ANALYZE anser_mj_t10; +ANALYZE anser_mj_t11; +-- The same shape, minus every tenth key. The outer-join case below needs a +-- right side that does not cover its left, and none of the tables above can be +-- it: they are joined 1:1 on purpose, and thinning one would break the "1000 +-- rows summing to 500500 whatever the join count" invariant the chains rest on. +CREATE TABLE anser_mj_gap (own int, nxt int, payload text) DISTRIBUTED BY (own); +INSERT INTO anser_mj_gap SELECT g, g, 'gap_' || g + FROM generate_series(1, 3000) g WHERE g % 10 <> 0; +ANALYZE anser_mj_gap; +-- Runs one chain twice, with the filter on and off, and reports whether the two +-- agree. The checksum is what makes the comparison mean something: equal row +-- counts would also come out of two different sets of rows, and a filter that +-- wrongly rejects is exactly the failure that changes which rows come back. +CREATE FUNCTION anser_mj_check(njoins int) +RETURNS TABLE (joins int, nrows bigint, own_sum bigint, agrees boolean) +LANGUAGE plpgsql AS $$ +DECLARE + sql text; + i int; + on_rows bigint; + on_sum bigint; + off_rows bigint; + off_sum bigint; +BEGIN + sql := 'SELECT count(*), coalesce(sum(anser_mj_t1.own), 0) FROM anser_mj_t1'; + FOR i IN 2 .. njoins + 1 LOOP + sql := sql || format(' JOIN anser_mj_t%s ON anser_mj_t%s.nxt = anser_mj_t%s.own', + i, i - 1, i); + END LOOP; + PERFORM set_config('anser.runtime_filter', 'on', false); + EXECUTE sql INTO on_rows, on_sum; + PERFORM set_config('anser.runtime_filter', 'off', false); + EXECUTE sql INTO off_rows, off_sum; + RETURN QUERY SELECT njoins, on_rows, on_sum, + (on_rows = off_rows AND on_sum = off_sum); +END; +$$; +-- 2 through 10 joins, Postgres planner. Expected: no rows. +SET optimizer = off; +SELECT joins, nrows, own_sum, agrees + FROM generate_series(2, 10) g, anser_mj_check(g) + WHERE NOT agrees OR nrows <> 1000 OR own_sum <> 500500 + ORDER BY joins; + joins | nrows | own_sum | agrees +-------+-------+---------+-------- +(0 rows) + +-- The same under ORCA, which picks different join orders and slice widths. +SET optimizer = on; +SELECT joins, nrows, own_sum, agrees + FROM generate_series(2, 10) g, anser_mj_check(g) + WHERE NOT agrees OR nrows <> 1000 OR own_sum <> 500500 + ORDER BY joins; + joins | nrows | own_sum | agrees +-------+-------+---------+-------- +(0 rows) + +SET optimizer = off; +-- A chain whose middle join is an outer join, and whose next join reads the +-- nullable side's key -- so the joined result carries NULLs the base relation +-- does not. +-- +-- Two things make that true of the plan and not just of the text. The right +-- side has to leave keys unmatched, which is what anser_mj_gap is for. And the +-- outer join has to survive planning: the join above it is strict in c.nxt, so +-- reduce_outer_joins() would otherwise turn this LEFT JOIN into an inner one +-- before any path is considered. OFFSET 0 keeps the subquery from being pulled +-- up, which keeps that qual a level away. +-- +-- No filter is injected here, and none can be. The price of keeping the outer +-- join is a SubqueryScan, and AnserResolveKeyScan descends through Hash, Motion +-- and HashJoin only. Nor is there a way around it: a NULL-keyed row reaches +-- the output only under an outer join, an outer join survives only behind a +-- subquery boundary, and Anser does not inject across one. So a filter meeting +-- a NULL key is not something this suite can arrange -- what is checked here is +-- that such a plan returns the same rows either way. +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_mj_lj_on AS + SELECT s.aown, s.cown, d.own AS down + FROM (SELECT a.own AS aown, c.own AS cown, c.nxt AS cnxt + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + LEFT JOIN anser_mj_gap c ON b.nxt = c.own + OFFSET 0) s + JOIN anser_mj_t4 d ON s.cnxt = d.own + DISTRIBUTED BY (aown); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_mj_lj_off AS + SELECT s.aown, s.cown, d.own AS down + FROM (SELECT a.own AS aown, c.own AS cown, c.nxt AS cnxt + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + LEFT JOIN anser_mj_gap c ON b.nxt = c.own + OFFSET 0) s + JOIN anser_mj_t4 d ON s.cnxt = d.own + DISTRIBUTED BY (aown); +SELECT count(*) AS lj_rows FROM anser_mj_lj_on; + lj_rows +--------- + 900 +(1 row) + +SELECT count(*) AS lj_only_on + FROM (SELECT * FROM anser_mj_lj_on EXCEPT ALL SELECT * FROM anser_mj_lj_off) d; + lj_only_on +------------ + 0 +(1 row) + +SELECT count(*) AS lj_only_off + FROM (SELECT * FROM anser_mj_lj_off EXCEPT ALL SELECT * FROM anser_mj_lj_on) d; + lj_only_off +------------- + 0 +(1 row) + +-- A star rather than a chain: every join reads t1's key, so one table's +-- distribution serves them all and the build sides are base scans throughout. +-- This is the shape that worked before the descent went through joins, and it +-- has to keep working. +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_mj_star_on AS + SELECT a.own AS aown, e.payload AS epayload + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + JOIN anser_mj_t3 c ON a.nxt = c.own + JOIN anser_mj_t4 d ON a.nxt = d.own + JOIN anser_mj_t5 e ON a.nxt = e.own + DISTRIBUTED BY (aown); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_mj_star_off AS + SELECT a.own AS aown, e.payload AS epayload + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + JOIN anser_mj_t3 c ON a.nxt = c.own + JOIN anser_mj_t4 d ON a.nxt = d.own + JOIN anser_mj_t5 e ON a.nxt = e.own + DISTRIBUTED BY (aown); +SELECT count(*) AS star_rows FROM anser_mj_star_on; + star_rows +----------- + 1000 +(1 row) + +SELECT count(*) AS star_only_on + FROM (SELECT * FROM anser_mj_star_on EXCEPT ALL SELECT * FROM anser_mj_star_off) d; + star_only_on +-------------- + 0 +(1 row) + +SELECT count(*) AS star_only_off + FROM (SELECT * FROM anser_mj_star_off EXCEPT ALL SELECT * FROM anser_mj_star_on) d; + star_only_off +--------------- + 0 +(1 row) + +-- A chain that returns nothing. A filter that wrongly accepts is only slow, +-- never wrong, so this is the one direction an empty result can still check. +SET anser.runtime_filter = on; +SELECT count(*) AS empty_rows + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + JOIN anser_mj_t3 c ON b.nxt = c.own + WHERE a.own < 0; + empty_rows +------------ + 0 +(1 row) + +-- The chains above assert that the filter changes no answer. That also holds +-- when the filter never arrives and every consumer fails open, which is how a +-- broken exchange passes a green test. This asserts the other half: a chain +-- whose third table is three times the size of the first has rows to prune, so +-- a filter that arrived removes some. +CREATE FUNCTION anser_mj_pruned(q text) RETURNS bigint +LANGUAGE plpgsql AS $$ +DECLARE + line text; + removed bigint := 0; +BEGIN + FOR line IN EXECUTE 'EXPLAIN (ANALYZE, TIMING OFF, COSTS OFF) ' || q LOOP + IF line ~ 'Rows Removed by (Bloom Filter|Pushdown Runtime Filter): ' THEN + removed := removed + coalesce(substring(line from ': *([0-9]+)')::bigint, 0); + END IF; + END LOOP; + RETURN removed; +END; +$$; +SET anser.runtime_filter = on; +SELECT 'the chain pruned nothing: no filter arrived, or none was injected' AS problem + WHERE anser_mj_pruned('SELECT count(*) FROM anser_mj_t1 a JOIN anser_mj_t2 b ON a.nxt = b.own JOIN anser_mj_t3 c ON b.nxt = c.own') = 0; + problem +--------- +(0 rows) + +DROP FUNCTION anser_mj_pruned(text); +DROP FUNCTION anser_mj_check(int); +DROP TABLE anser_mj_t1, anser_mj_t2, anser_mj_t3, anser_mj_t4, anser_mj_t5, + anser_mj_t6, anser_mj_t7, anser_mj_t8, anser_mj_t9, anser_mj_t10, + anser_mj_t11, anser_mj_gap; +RESET anser.runtime_filter; +RESET anser.timeout_ms; +RESET optimizer; +RESET enable_nestloop; +RESET enable_mergejoin; diff --git a/gpcontrib/anser/expected/anser_runtime_filter.out b/gpcontrib/anser/expected/anser_runtime_filter.out new file mode 100644 index 00000000000..e8092995007 --- /dev/null +++ b/gpcontrib/anser/expected/anser_runtime_filter.out @@ -0,0 +1,1096 @@ +-- Anser runtime bloom filter: plan-tree integration (PR4). +-- +-- Requires the cluster to run with anser.enable=on so the gather/send +-- services are live. On a multi-segment cluster the build side redistributes, +-- exercising the Motion-directly-under-CustomScan case; on a single segment it +-- degrades to the leaf case. Either way the feature must (a) inject the +-- producer/consumer nodes and (b) never change query results. +-- Deterministic plan shape: force a hash join. +SET enable_nestloop = off; +SET enable_mergejoin = off; +-- A regress test must not inherit the production deadline. anser.timeout_ms +-- defaults to 10 s, which is a budget for an exchange that has gone wrong; a +-- test that waits it out has turned a delivery failure into a slow pass. +-- Pinned shorter here, so a filter that never arrives fails the run instead of +-- hiding in the clock. +SET anser.timeout_ms = 5000; +-- Trace every exchange step into the server logs for the whole file. LOG +-- level never reaches the client, so the expected output is unaffected; the +-- traces are for the CI logs when a run goes wrong. +SET anser.debug = on; +-- build is the smaller (hashed / preserved) side, distributed so the join key +-- must be redistributed; probe is the larger side distributed by the join key. +CREATE TABLE anser_rf_build (id int, name text) DISTRIBUTED BY (name); +CREATE TABLE anser_rf_probe (id int, payload text) DISTRIBUTED BY (id); +INSERT INTO anser_rf_build SELECT g, 'b' || g FROM generate_series(1, 200) g; +INSERT INTO anser_rf_probe SELECT g, 'p' || g FROM generate_series(1, 2000) g; +ANALYZE anser_rf_build; +ANALYZE anser_rf_probe; +-- Use the Postgres planner for a deterministic plan shape. +SET optimizer = off; +-- With the filter on the plan carries an "Anser Bloom Producer" under the Hash +-- and an "Anser Bloom Consumer" above the probe scan (with the planned bloom +-- size); with it off neither node appears. (COSTS OFF keeps the output stable.) +SET anser.runtime_filter = on; +EXPLAIN (COSTS OFF) +SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; + QUERY PLAN +------------------------------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Hash Right Join + Hash Cond: (p.id = b.id) + -> Custom Scan (Anser Bloom Consumer) + Bloom Filter Size: 1048576 bytes + -> Seq Scan on anser_rf_probe p + -> Hash + -> Redistribute Motion 3:3 (slice2; segments: 3) + Hash Key: b.id + -> Custom Scan (Anser Bloom Producer) + Bloom Filter Size: 1048576 bytes + Bloom Filter Stats: memory=1024kB + -> Seq Scan on anser_rf_build b + Optimizer: Postgres query optimizer +(14 rows) + +SET anser.runtime_filter = off; +EXPLAIN (COSTS OFF) +SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; + QUERY PLAN +------------------------------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Hash Right Join + Hash Cond: (p.id = b.id) + -> Seq Scan on anser_rf_probe p + -> Hash + -> Redistribute Motion 3:3 (slice2; segments: 3) + Hash Key: b.id + -> Seq Scan on anser_rf_build b + Optimizer: Postgres query optimizer +(9 rows) + +-- Correctness: identical results with the filter on vs off (Postgres planner). +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_r_on AS + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + DISTRIBUTED BY (name); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_r_off AS + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + DISTRIBUTED BY (name); +SELECT count(*) AS rows_on FROM anser_rf_r_on; + rows_on +--------- + 200 +(1 row) + +SELECT count(*) AS rows_off FROM anser_rf_r_off; + rows_off +---------- + 200 +(1 row) + +SELECT count(*) AS only_on + FROM (SELECT * FROM anser_rf_r_on EXCEPT ALL SELECT * FROM anser_rf_r_off) d; + only_on +--------- + 0 +(1 row) + +SELECT count(*) AS only_off + FROM (SELECT * FROM anser_rf_r_off EXCEPT ALL SELECT * FROM anser_rf_r_on) d; + only_off +---------- + 0 +(1 row) + +-- Correctness must also hold under ORCA (whether or not it injects the nodes). +SET optimizer = on; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_r_orca AS + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + DISTRIBUTED BY (name); +SELECT count(*) AS only_orca + FROM (SELECT * FROM anser_rf_r_orca EXCEPT ALL SELECT * FROM anser_rf_r_off) d; + only_orca +----------- + 0 +(1 row) + +SELECT count(*) AS only_off_orca + FROM (SELECT * FROM anser_rf_r_off EXCEPT ALL SELECT * FROM anser_rf_r_orca) d; + only_off_orca +--------------- + 0 +(1 row) + +-- Pushdown mix: with gp_enable_runtime_filter_pushdown on, the consumer hands +-- the unioned filter to the probe SeqScan as an SK_BLOOM_FILTER scan key and +-- the scan (or the table AM) does the pruning. Results must still match the +-- filter-off run. +SET optimizer = off; +SET gp_enable_runtime_filter_pushdown = on; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_r_push AS + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + DISTRIBUTED BY (name); +SELECT count(*) AS only_push + FROM (SELECT * FROM anser_rf_r_push EXCEPT ALL SELECT * FROM anser_rf_r_off) d; + only_push +----------- + 0 +(1 row) + +SELECT count(*) AS only_off_push + FROM (SELECT * FROM anser_rf_r_off EXCEPT ALL SELECT * FROM anser_rf_r_push) d; + only_off_push +--------------- + 0 +(1 row) + +RESET gp_enable_runtime_filter_pushdown; +-- Counts the Anser nodes a plan carries. Comparing a filtered count with an +-- unfiltered one says nothing when no filter was injected: both runs then do +-- the same thing and agree. Several blocks below ask this as well, which is +-- why it is defined here rather than beside its first caller. +CREATE FUNCTION anser_rf_nodes(q text) RETURNS bigint +LANGUAGE plpgsql AS $$ +DECLARE + line text; + nodes bigint := 0; +BEGIN + FOR line IN EXECUTE 'EXPLAIN (COSTS OFF) ' || q LOOP + IF line ~ 'Anser Bloom' THEN + nodes := nodes + 1; + END IF; + END LOOP; + RETURN nodes; +END; +$$; +-- Pushdown where the key's table attribute number and its position in the +-- scan's output targetlist disagree. The case above cannot see the +-- difference: anser_rf_probe's key is its first column and also the first +-- thing the scan emits, so the two numbers coincide. Here the key is the +-- probe table's *second* column and its *only* projected one, so a scan key +-- carrying the output position makes the scan test `payload` against a filter +-- built on `id` -- and since no payload is ever a build key, every row is +-- discarded. Both counts must be 200. +SET optimizer = on; +CREATE TABLE anser_rf_proj_build (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_rf_proj_probe (payload int, id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_proj_build SELECT g, 'b' || g FROM generate_series(1, 200) g; +INSERT INTO anser_rf_proj_probe SELECT -g, g FROM generate_series(1, 2000) g; +ANALYZE anser_rf_proj_build; +ANALYZE anser_rf_proj_probe; +SET gp_enable_runtime_filter_pushdown = on; +SET anser.runtime_filter = off; +SELECT count(p.id) AS proj_push_off + FROM anser_rf_proj_build b LEFT JOIN anser_rf_proj_probe p ON b.id = p.id; + proj_push_off +--------------- + 200 +(1 row) + +SET anser.runtime_filter = on; +SELECT count(p.id) AS proj_push_on + FROM anser_rf_proj_build b LEFT JOIN anser_rf_proj_probe p ON b.id = p.id; + proj_push_on +-------------- + 200 +(1 row) + +SELECT anser_rf_nodes('SELECT count(p.id) + FROM anser_rf_proj_build b LEFT JOIN anser_rf_proj_probe p ON b.id = p.id') > 0 AS proj_injected; + proj_injected +--------------- + t +(1 row) + +-- The alias is not the column. This subquery renames `payload` to the name +-- `id` and the real `id` to `id2`, and joins on id2 -- so resolving the key by +-- name would land on `payload`, the one column guaranteed never to match a +-- filter built from the build side. Resolution runs position -> TargetEntry +-- -> Var -> varattno and never reads a name, so the rename is invisible to it. +-- Both counts must be 200. +SET anser.runtime_filter = off; +SELECT count(p.id2) AS rename_push_off + FROM anser_rf_proj_build b + LEFT JOIN (SELECT payload AS id, id AS id2 FROM anser_rf_proj_probe) p + ON b.id = p.id2; + rename_push_off +----------------- + 200 +(1 row) + +SET anser.runtime_filter = on; +SELECT count(p.id2) AS rename_push_on + FROM anser_rf_proj_build b + LEFT JOIN (SELECT payload AS id, id AS id2 FROM anser_rf_proj_probe) p + ON b.id = p.id2; + rename_push_on +---------------- + 200 +(1 row) + +SELECT anser_rf_nodes('SELECT count(p.id2) + FROM anser_rf_proj_build b + LEFT JOIN (SELECT payload AS id, id AS id2 FROM anser_rf_proj_probe) p + ON b.id = p.id2') > 0 AS rename_injected; + rename_injected +----------------- + t +(1 row) + +-- A computed key, which Anser declines outright. The subquery has no ORDER BY, +-- LIMIT or OFFSET, so is_simple_subquery() pulls it up and the hash clause +-- becomes `b.id = p.id * 2`; anser_hashjoin_keys takes two plain Vars and +-- nothing else, so no nodes are injected at all. +-- +-- The comment here used to claim the opposite -- that the clause still reads as +-- a Var into the scan's output and only the pushdown is declined. That would +-- need the subquery to survive as a scan of its own, which asking for it +-- (OFFSET 0) does not buy either: AnserResolveKeyScan does not descend through +-- a SubqueryScan. The decision for a computed column is covered directly by +-- anser_test_pushdown_accepts; what is left to check here is that declining is +-- clean, so expr_injected is f and both counts are 100 -- only the 100 even +-- build ids match. +SET anser.runtime_filter = off; +SELECT count(p.k) AS expr_push_off + FROM anser_rf_proj_build b + LEFT JOIN (SELECT id * 2 AS k FROM anser_rf_proj_probe) p ON b.id = p.k; + expr_push_off +--------------- + 100 +(1 row) + +SET anser.runtime_filter = on; +SELECT count(p.k) AS expr_push_on + FROM anser_rf_proj_build b + LEFT JOIN (SELECT id * 2 AS k FROM anser_rf_proj_probe) p ON b.id = p.k; + expr_push_on +-------------- + 100 +(1 row) + +SELECT anser_rf_nodes('SELECT count(p.k) + FROM anser_rf_proj_build b + LEFT JOIN (SELECT id * 2 AS k FROM anser_rf_proj_probe) p ON b.id = p.k') > 0 AS expr_injected; + expr_injected +--------------- + f +(1 row) + +RESET gp_enable_runtime_filter_pushdown; +DROP TABLE anser_rf_proj_build, anser_rf_proj_probe; +SET optimizer = off; +-- Everything above checks that the nodes were injected and that the results did +-- not change. Both of those also hold when the filter never arrives and every +-- consumer fails open -- which is how a broken exchange passes a green test. +-- This is the assertion that the filter was used: the probe side holds 2000 +-- rows of which 200 join, so a filter that arrived removes the rest. Only the +-- Postgres planner is asserted, because its plan shape here is fixed; ORCA +-- chooses and the count would not be a stable thing to require. +CREATE FUNCTION anser_rf_pruned(q text) RETURNS bigint +LANGUAGE plpgsql AS $$ +DECLARE + line text; + removed bigint := 0; +BEGIN + FOR line IN EXECUTE 'EXPLAIN (ANALYZE, TIMING OFF, COSTS OFF) ' || q LOOP + IF line ~ 'Rows Removed by (Bloom Filter|Pushdown Runtime Filter): ' THEN + removed := removed + coalesce(substring(line from ': *([0-9]+)')::bigint, 0); + END IF; + END LOOP; + RETURN removed; +END; +$$; +SET optimizer = off; +SET anser.runtime_filter = on; +SELECT 'the filter pruned nothing: it never arrived, or it arrived empty' AS problem + WHERE anser_rf_pruned('SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id') = 0; + problem +--------- +(0 rows) + +-- Nested statement guard: a statement run through SPI from a function body +-- goes through its whole Start/Run/End inside the *outer* query's Run, so the +-- inner ExecutorEnd fires while the outer query is still executing. Dropping +-- channels there takes the running query's filters with it, and every consumer +-- that has not read yet fails open -- the exchange still happens, the filter is +-- just never used. +-- +-- This needs an inner join, not the left join above: the consumer defers its +-- first tuple only for INNER (see defer_first), which is what lets the join +-- emit a row -- and so evaluate the function -- before the consumer reads. +CREATE FUNCTION anser_rf_nested(x int) RETURNS int +LANGUAGE plpgsql VOLATILE AS $$ +BEGIN + PERFORM 1; + RETURN x; +END; +$$; +SELECT 'an inner statement dropped the running query''s filter' AS problem + WHERE anser_rf_pruned('SELECT anser_rf_nested(p.id) FROM anser_rf_build b + JOIN anser_rf_probe p ON b.id = p.id') = 0; + problem +--------- +(0 rows) + +-- Narrow build slices must still publish. part_index is only diagnostic, so +-- a slice narrower than the cluster must not reject the only producer there +-- is: direct dispatch to a non-zero segment (segindex >= total_parts) or the +-- entry-DB (segindex = -1) both used to leave the channel a part short and +-- every consumer waiting out anser.timeout_ms. The block also asserts the +-- count, but the load-bearing assertion is that rows were pruned at all. +SET anser.timeout_ms = 100; +CREATE TABLE anser_rf_dd_build (id int, dk text, pad text) DISTRIBUTED BY (dk); +CREATE TABLE anser_rf_dd_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_dd_build SELECT g, 'k' || (g % 6), 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_rf_dd_probe SELECT g % 1000 + 1 FROM generate_series(1, 100000) g; +ANALYZE anser_rf_dd_build; +ANALYZE anser_rf_dd_probe; +-- Check every distribution key, not a chosen one: on any cluster with more +-- than one segment at least one of them lands with segindex >= 1, which is +-- the shape that used to stall -- the direct-dispatched producer was refused +-- (segindex >= total_parts) and every consumer waited out anser.timeout_ms. +-- The discriminating observable is that rows were pruned at all; result +-- counts are identical either way, since a broken exchange fails open. +SET anser.runtime_filter = on; +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k0''') > 0 AS k0_pruned; + k0_pruned +----------- + t +(1 row) + +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k1''') > 0 AS k1_pruned; + k1_pruned +----------- + t +(1 row) + +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k2''') > 0 AS k2_pruned; + k2_pruned +----------- + t +(1 row) + +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k3''') > 0 AS k3_pruned; + k3_pruned +----------- + t +(1 row) + +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k4''') > 0 AS k4_pruned; + k4_pruned +----------- + t +(1 row) + +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k5''') > 0 AS k5_pruned; + k5_pruned +----------- + t +(1 row) + +-- Entry-DB build side: a catalog scan runs on the entry-DB QE, whose +-- segindex is -1 -- 0xFFFFFFFF as a part index, the same rejection. +CREATE TABLE anser_rf_eb_probe (o oid) DISTRIBUTED BY (o); +INSERT INTO anser_rf_eb_probe + SELECT 'anser_rf_eb_probe'::regclass::oid FROM generate_series(1, 10) g + UNION ALL + SELECT 4000000000 + g FROM generate_series(1, 100000) g; +ANALYZE anser_rf_eb_probe; +-- The count uses only the matching rows, so it is exact: the table's own oid +-- exists in pg_class, ten times. +SELECT count(*) AS eb_rows + FROM (SELECT 'anser_rf_eb_probe'::regclass::oid AS o FROM generate_series(1, 10) g) p + JOIN pg_class c ON p.o = c.oid; + eb_rows +--------- + 10 +(1 row) + +SELECT 'entry-DB producer did not publish (consumer failed open)' AS problem + WHERE anser_rf_pruned('SELECT count(*) FROM anser_rf_eb_probe p JOIN pg_class c ON p.o = c.oid') = 0; + problem +--------- +(0 rows) + +DROP TABLE anser_rf_dd_build, anser_rf_dd_probe, anser_rf_eb_probe; +RESET anser.timeout_ms; +DROP FUNCTION anser_rf_nested(int); +DROP FUNCTION anser_rf_pruned(text); +-- Filter sizing follows the estimate, so exercising a production-size payload +-- with test-size data goes through a debug knob instead: force a 64 MB bitset +-- for a 200-row build side, and the full-size filter has to actually exchange +-- (the serialized part rides the wire to every consumer). +SET anser.debug_min_filter_bytes = 67108864; +SET anser.runtime_filter = on; +EXPLAIN (COSTS OFF) +SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; + QUERY PLAN +------------------------------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Hash Right Join + Hash Cond: (p.id = b.id) + -> Custom Scan (Anser Bloom Consumer) + Bloom Filter Size: 67108864 bytes + -> Seq Scan on anser_rf_probe p + -> Hash + -> Redistribute Motion 3:3 (slice2; segments: 3) + Hash Key: b.id + -> Custom Scan (Anser Bloom Producer) + Bloom Filter Size: 67108864 bytes + Bloom Filter Stats: memory=65536kB + -> Seq Scan on anser_rf_build b + Optimizer: Postgres query optimizer +(14 rows) + +-- A plain EXPLAIN must not allocate the filter: the coordinator initializes +-- every slice with EXEC_FLAG_EXPLAIN_ONLY, so the producer must skip building +-- the bitset (state=NULL) while the plan still shows the planned size. The +-- debug log makes the decision observable. anser.debug is already on for the +-- whole file; client_min_messages is what brings the line to the client here. +SET client_min_messages = log; +EXPLAIN (COSTS OFF) +SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; +LOG: statement: EXPLAIN (COSTS OFF) +SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; +LOG: anser: producer init cond=0 part=0/1 elems=33554432 payload=67108928 state=NULL + QUERY PLAN +------------------------------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Hash Right Join + Hash Cond: (p.id = b.id) + -> Custom Scan (Anser Bloom Consumer) + Bloom Filter Size: 67108864 bytes + -> Seq Scan on anser_rf_probe p + -> Hash + -> Redistribute Motion 3:3 (slice2; segments: 3) + Hash Key: b.id + -> Custom Scan (Anser Bloom Producer) + Bloom Filter Size: 67108864 bytes + Bloom Filter Stats: memory=65536kB + -> Seq Scan on anser_rf_build b + Optimizer: Postgres query optimizer +(14 rows) + +RESET client_min_messages; +LOG: statement: RESET client_min_messages; +-- And the filter must still be correct: 200 build rows, each matching one +-- probe row. +SELECT count(*) AS rows_joined + FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; + rows_joined +------------- + 200 +(1 row) + +-- The floor must change the real filter, not just the plan: sizing raises the +-- declared element count to match the forced bitset size, so producer and +-- consumer really build a 64 MB filter instead of displaying one. +CREATE EXTENSION IF NOT EXISTS anser_test; +SELECT injected, total_elems, planned_bytes FROM anser_test_rf_size(200); + injected | total_elems | planned_bytes +----------+-------------+--------------- + t | 33554432 | 67108864 +(1 row) + +DROP EXTENSION anser_test; +-- The same exchange in chunks: an 8 kB chunk turns the 64 MB filter into +-- thousands of NEXT round trips. The result must match the unfiltered run and +-- the query must simply finish -- a whole-filter push that outgrew the +-- connection buffers is exactly what used to wedge the session. Matching +-- results alone would not prove it: a consumer that gives up produces the +-- same rows, so the coordinator's counters must say every consumer was +-- served and none withdrew. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS anser_test; +-- end_ignore +SELECT anser_test_deliveries_completed() AS d_before \gset +SELECT anser_test_unsubscribes() AS u_before \gset +SET anser.debug_delivery_chunk_bytes = 8192; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_r_chunk AS + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + DISTRIBUTED BY (name); +SELECT anser_test_deliveries_completed() = :d_before + 3 AS all_delivered; + all_delivered +--------------- + t +(1 row) + +SELECT anser_test_unsubscribes() = :u_before AS nobody_gave_up; + nobody_gave_up +---------------- + t +(1 row) + +SELECT count(*) AS only_chunk + FROM (SELECT * FROM anser_rf_r_chunk EXCEPT ALL SELECT * FROM anser_rf_r_off) d; + only_chunk +------------ + 0 +(1 row) + +SELECT count(*) AS only_off_chunk + FROM (SELECT * FROM anser_rf_r_off EXCEPT ALL SELECT * FROM anser_rf_r_chunk) d; + only_off_chunk +---------------- + 0 +(1 row) + +DROP TABLE anser_rf_r_chunk; +DROP EXTENSION anser_test; +RESET anser.debug_delivery_chunk_bytes; +-- A LIMIT can finish the query while a consumer is still waiting for its +-- filter: the producer is squelched before it publishes, the consumer must +-- bail out via QueryFinishPending and withdraw its subscription, and the +-- result must match the unfiltered run. +SELECT count(*) AS limited_rows FROM ( + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + LIMIT 10) q; + limited_rows +-------------- + 10 +(1 row) + +-- An open cursor must be undisturbed by statements between its FETCHes: the +-- filter exchange completes inside the FETCH's ExecutorRun window, so a +-- sibling statement's ExecutorEnd sweep has nothing of the cursor's to wipe. +-- The sibling runs the same filtered join, so it really does create and sweep +-- its own channels while the cursor's gang sits suspended. The delivery +-- counter must also say every consumer was served -- because "came back +-- unfiltered" returns the same rows and would hide a broken exchange. (The +-- sweep's failure mode itself is a wait, not a wrong answer.) The cursor +-- query filters the build side to ids 1..3, and on a segment none of them +-- land on the hash is empty, the join skips the probe side, and that +-- consumer never runs -- so its count comes from the data, not from the +-- cluster width. The sibling has no such filter, so it serves all three. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS anser_test; +-- end_ignore +SELECT anser_test_deliveries_completed() AS d_before \gset +SELECT count(DISTINCT gp_segment_id) AS cursor_consumers + FROM anser_rf_probe WHERE id <= 3 \gset +SET optimizer = off; +SET anser.runtime_filter = on; +BEGIN; +DECLARE anser_rf_cur CURSOR FOR + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + WHERE b.id <= 3 ORDER BY b.name; +FETCH 1 anser_rf_cur; + name | payload +------+--------- + b1 | p1 +(1 row) + +-- The ExecutorEnd of this statement must leave the open cursor's state alone. +SELECT count(*) AS sibling_stmt FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; + sibling_stmt +-------------- + 200 +(1 row) + +FETCH ALL anser_rf_cur; + name | payload +------+--------- + b2 | p2 + b3 | p3 +(2 rows) + +COMMIT; +SELECT anser_test_deliveries_completed() + = :d_before + 3 + :cursor_consumers AS cursor_delivered; + cursor_delivered +------------------ + t +(1 row) + +DROP EXTENSION anser_test; +DROP TABLE anser_rf_r_push; +DROP TABLE anser_rf_r_on, anser_rf_r_off, anser_rf_r_orca; +DROP TABLE anser_rf_build, anser_rf_probe; +-- Datatype guard: producer and consumer hash the raw Datum bytes, so injection +-- is restricted to keys where SQL equality is bitwise Datum equality. A +-- cross-type join (float4 vs float8 Datums for equal values differ) must not +-- be injected; results must match the filter-off run either way. +CREATE TABLE anser_rf_build_f (id float8, name text) DISTRIBUTED BY (name); +CREATE TABLE anser_rf_probe_f (id float4, payload text) DISTRIBUTED BY (id); +INSERT INTO anser_rf_build_f SELECT g, 'b' || g FROM generate_series(1, 200) g; +INSERT INTO anser_rf_probe_f SELECT g, 'p' || g FROM generate_series(1, 2000) g; +ANALYZE anser_rf_build_f; +ANALYZE anser_rf_probe_f; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_f_on AS + SELECT b.name, p.payload FROM anser_rf_build_f b JOIN anser_rf_probe_f p ON b.id = p.id + DISTRIBUTED BY (name); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_f_off AS + SELECT b.name, p.payload FROM anser_rf_build_f b JOIN anser_rf_probe_f p ON b.id = p.id + DISTRIBUTED BY (name); +SELECT count(*) AS rows_on FROM anser_rf_f_on; + rows_on +--------- + 200 +(1 row) + +SELECT count(*) AS only_on + FROM (SELECT * FROM anser_rf_f_on EXCEPT ALL SELECT * FROM anser_rf_f_off) d; + only_on +--------- + 0 +(1 row) + +SELECT count(*) AS only_off + FROM (SELECT * FROM anser_rf_f_off EXCEPT ALL SELECT * FROM anser_rf_f_on) d; + only_off +---------- + 0 +(1 row) + +DROP TABLE anser_rf_f_on, anser_rf_f_off, anser_rf_build_f, anser_rf_probe_f; +-- Same-typed float keys are also excluded: -0.0 and 0.0 compare equal in SQL +-- but are not bitwise equal, so hashing raw Datums could prune a joinable row. +CREATE TABLE anser_rf_build_z (id float8, name text) DISTRIBUTED BY (name); +CREATE TABLE anser_rf_probe_z (id float8, payload text) DISTRIBUTED BY (id); +INSERT INTO anser_rf_build_z VALUES (1, 'b1'), (-0.0, 'bz'); +INSERT INTO anser_rf_probe_z VALUES (1, 'p1'), (0.0, 'pz'), (2, 'p2'); +ANALYZE anser_rf_build_z; +ANALYZE anser_rf_probe_z; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_z_on AS + SELECT b.name, p.payload FROM anser_rf_build_z b JOIN anser_rf_probe_z p ON b.id = p.id + DISTRIBUTED BY (name); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_z_off AS + SELECT b.name, p.payload FROM anser_rf_build_z b JOIN anser_rf_probe_z p ON b.id = p.id + DISTRIBUTED BY (name); +SELECT count(*) AS rows_on FROM anser_rf_z_on; + rows_on +--------- + 2 +(1 row) + +SELECT count(*) AS only_on + FROM (SELECT * FROM anser_rf_z_on EXCEPT ALL SELECT * FROM anser_rf_z_off) d; + only_on +--------- + 0 +(1 row) + +SELECT count(*) AS only_off + FROM (SELECT * FROM anser_rf_z_off EXCEPT ALL SELECT * FROM anser_rf_z_on) d; + only_off +---------- + 0 +(1 row) + +DROP TABLE anser_rf_z_on, anser_rf_z_off, anser_rf_build_z, anser_rf_probe_z; +-- The exclusion has to hold through a domain. A domain inherits typlen and +-- typbyval from its base type, so it looks by-value, and its OID is not one of +-- the float OIDs the exclusion names -- a check on the declared type alone lets +-- it straight through and -0.0 would stop matching 0.0. Both sides are +-- distributed on their text column so the join needs a Motion and is a +-- candidate for injection; the key type is what has to refuse it. +CREATE DOMAIN anser_rf_f8 AS float8; +CREATE TABLE anser_rf_build_d (id anser_rf_f8, name text) DISTRIBUTED BY (name); +CREATE TABLE anser_rf_probe_d (id anser_rf_f8, payload text) DISTRIBUTED BY (payload); +INSERT INTO anser_rf_build_d VALUES (1, 'b1'), (-0.0, 'bz'); +INSERT INTO anser_rf_probe_d VALUES (1, 'p1'), (0.0, 'pz'), (2, 'p2'); +ANALYZE anser_rf_build_d; +ANALYZE anser_rf_probe_d; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_d_on AS + SELECT b.name, p.payload FROM anser_rf_build_d b JOIN anser_rf_probe_d p ON b.id = p.id + DISTRIBUTED BY (name); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_d_off AS + SELECT b.name, p.payload FROM anser_rf_build_d b JOIN anser_rf_probe_d p ON b.id = p.id + DISTRIBUTED BY (name); +SELECT count(*) AS rows_on FROM anser_rf_d_on; + rows_on +--------- + 2 +(1 row) + +SELECT count(*) AS only_on + FROM (SELECT * FROM anser_rf_d_on EXCEPT ALL SELECT * FROM anser_rf_d_off) d; + only_on +--------- + 0 +(1 row) + +SELECT count(*) AS only_off + FROM (SELECT * FROM anser_rf_d_off EXCEPT ALL SELECT * FROM anser_rf_d_on) d; + only_off +---------- + 0 +(1 row) + +DROP TABLE anser_rf_d_on, anser_rf_d_off, anser_rf_build_d, anser_rf_probe_d; +DROP DOMAIN anser_rf_f8; +-- Parameterized rescan guard: a build side whose rows depend on a parameter an +-- enclosing nested loop reassigns yields a different key set per outer tuple, +-- and neither node rebuilds on rescan -- the producer stays published, the +-- consumer keeps the filter it already has. The second iteration would be +-- probed against the first iteration's keys, dropping every row that joins. +-- So the plan pass must refuse the join outright. +-- +-- OFFSET 0 keeps the subquery from being pulled up, so it stays a nested loop +-- with a correlated inner side. Each group's ids are disjoint, which is what +-- makes a stale filter visible: with the first group's filter still in place +-- the second group matches nothing. +CREATE TABLE anser_rf_nl_outer (g int) DISTRIBUTED REPLICATED; +CREATE TABLE anser_rf_nl_build (g int, id int) DISTRIBUTED BY (g); +CREATE TABLE anser_rf_nl_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_nl_outer VALUES (1), (2); +INSERT INTO anser_rf_nl_build SELECT 1, g FROM generate_series(1, 100) g; +INSERT INTO anser_rf_nl_build SELECT 2, g FROM generate_series(101, 200) g; +INSERT INTO anser_rf_nl_probe SELECT g FROM generate_series(1, 20000) g; +ANALYZE anser_rf_nl_outer; +ANALYZE anser_rf_nl_build; +ANALYZE anser_rf_nl_probe; +-- Both groups must report 100, with the filter on and off alike, and the plan +-- must carry no Anser node at all. +SET anser.runtime_filter = off; +SELECT o.g, s.n AS nl_off +FROM anser_rf_nl_outer o +CROSS JOIN LATERAL (SELECT count(*) n FROM anser_rf_nl_probe p + JOIN anser_rf_nl_build b ON p.id = b.id + WHERE b.g = o.g OFFSET 0) s +ORDER BY o.g; + g | nl_off +---+-------- + 1 | 100 + 2 | 100 +(2 rows) + +SET anser.runtime_filter = on; +SELECT o.g, s.n AS nl_on +FROM anser_rf_nl_outer o +CROSS JOIN LATERAL (SELECT count(*) n FROM anser_rf_nl_probe p + JOIN anser_rf_nl_build b ON p.id = b.id + WHERE b.g = o.g OFFSET 0) s +ORDER BY o.g; + g | nl_on +---+------- + 1 | 100 + 2 | 100 +(2 rows) + +SELECT anser_rf_nodes('SELECT o.g, s.n FROM anser_rf_nl_outer o +CROSS JOIN LATERAL (SELECT count(*) n FROM anser_rf_nl_probe p + JOIN anser_rf_nl_build b ON p.id = b.id + WHERE b.g = o.g OFFSET 0) s') AS nodes_in_parameterized_plan; + nodes_in_parameterized_plan +----------------------------- + 0 +(1 row) + +-- The same two tables joined without the correlation are still injected into: +-- the guard must refuse a parameterized build side, not every build side. +SELECT anser_rf_nodes('SELECT count(*) FROM anser_rf_nl_probe p + JOIN anser_rf_nl_build b ON p.id = b.id + WHERE b.g = 1') AS nodes_when_not_parameterized; + nodes_when_not_parameterized +------------------------------ + 2 +(1 row) + +DROP FUNCTION anser_rf_nodes(text); +DROP TABLE anser_rf_nl_outer, anser_rf_nl_build, anser_rf_nl_probe; +-- --------------------------------------------------------------------------- +-- Parallel execution. +-- +-- A parallel slice runs numsegments * parallel_workers processes and every one +-- of them publishes its own part, so the coordinator must wait for that many +-- parts rather than for one per segment. Undercounting completes the channel +-- early and delivers a filter missing build keys; a bloom filter short of keys +-- gives false negatives, which silently drops joinable rows. So the assertion +-- that matters is not the plan shape but that results are *identical* with the +-- filter on and off -- at more than one worker count, and under both planners, +-- which disagree here: for one query the Postgres planner may give a serial +-- build slice where GPORCA gives a parallel one. +-- +-- The build side is distributed by v and joined on id, so it is redistributed: +-- Anser only injects across a Motion, and a co-located join -- which is what +-- this used to use -- is now skipped outright, making the whole section +-- vacuous. The cost is that only GPORCA still parallelises it; the Postgres +-- planner marks Motion paths parallel-unsafe, so its two blocks below are +-- reduced to a filter-on/filter-off equivalence check on a serial slice. +-- They are kept because that equivalence is worth asserting on its own. +-- --------------------------------------------------------------------------- +CREATE TABLE anser_rf_par_build (id int, v text) DISTRIBUTED BY (v); +CREATE TABLE anser_rf_par_probe (id int, pad text) DISTRIBUTED BY (id); +INSERT INTO anser_rf_par_build SELECT g, 'b' || g FROM generate_series(1, 500) g; +INSERT INTO anser_rf_par_probe SELECT g, 'p' || g FROM generate_series(1, 20000) g; +ANALYZE anser_rf_par_build; +ANALYZE anser_rf_par_probe; +-- Force a parallel plan on tables this small, the way PostgreSQL's own tests +-- do; without this the scans are far too cheap to be worth splitting. +SET enable_parallel = on; +SET min_parallel_table_scan_size = 0; +SET parallel_setup_cost = 0; +SET parallel_tuple_cost = 0; +-- Postgres planner, serial baseline +SET optimizer = off; +SET max_parallel_workers_per_gather = 0; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_par_on AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_par_off AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SELECT (SELECT count(*) FROM anser_rf_par_on) AS rows_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_on + EXCEPT ALL + SELECT * FROM anser_rf_par_off) d) AS only_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_off + EXCEPT ALL + SELECT * FROM anser_rf_par_on) d) AS only_off; + rows_on | only_on | only_off +---------+---------+---------- + 500 | 0 | 0 +(1 row) + +DROP TABLE anser_rf_par_on, anser_rf_par_off; +-- Postgres planner, parallel +SET optimizer = off; +SET max_parallel_workers_per_gather = 4; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_par_on AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_par_off AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SELECT (SELECT count(*) FROM anser_rf_par_on) AS rows_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_on + EXCEPT ALL + SELECT * FROM anser_rf_par_off) d) AS only_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_off + EXCEPT ALL + SELECT * FROM anser_rf_par_on) d) AS only_off; + rows_on | only_on | only_off +---------+---------+---------- + 500 | 0 | 0 +(1 row) + +DROP TABLE anser_rf_par_on, anser_rf_par_off; +-- GPORCA, serial baseline +SET optimizer = on; +SET max_parallel_workers_per_gather = 0; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_par_on AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_par_off AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SELECT (SELECT count(*) FROM anser_rf_par_on) AS rows_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_on + EXCEPT ALL + SELECT * FROM anser_rf_par_off) d) AS only_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_off + EXCEPT ALL + SELECT * FROM anser_rf_par_on) d) AS only_off; + rows_on | only_on | only_off +---------+---------+---------- + 500 | 0 | 0 +(1 row) + +DROP TABLE anser_rf_par_on, anser_rf_par_off; +-- GPORCA, parallel +SET optimizer = on; +SET max_parallel_workers_per_gather = 4; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_par_on AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_par_off AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SELECT (SELECT count(*) FROM anser_rf_par_on) AS rows_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_on + EXCEPT ALL + SELECT * FROM anser_rf_par_off) d) AS only_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_off + EXCEPT ALL + SELECT * FROM anser_rf_par_on) d) AS only_off; + rows_on | only_on | only_off +---------+---------+---------- + 500 | 0 | 0 +(1 row) + +DROP TABLE anser_rf_par_on, anser_rf_par_off; +DROP TABLE anser_rf_par_build, anser_rf_par_probe; +RESET enable_parallel; +RESET min_parallel_table_scan_size; +RESET parallel_setup_cost; +RESET parallel_tuple_cost; +RESET max_parallel_workers_per_gather; +-- A probe side under a WindowAgg must not be filtered: the window is computed +-- over the whole input, so a consumer on the base scan below it would change +-- the result. The resolver must refuse the descent (the node whitelist +-- applies to the probe-side top too) and inject nothing. +SET optimizer = off; +SET anser.runtime_filter = on; +CREATE TABLE anser_rf_win (k int8, pad int) DISTRIBUTED BY (pad); +INSERT INTO anser_rf_win SELECT i % 1000, i FROM generate_series(1, 100000) i; +ANALYZE anser_rf_win; +-- A temp table is scanned on the coordinator, so the build side runs in the +-- coordinator's slice while the probe side runs on the segments: the slices +-- differ and the join would inject if the WindowAgg did not refuse it. k=5 +-- exists in the table (k = i % 1000, a hundred times), so the row matches and +-- the window total must be the full 100000 -- the windowed side returns every +-- row with the total attached, hence the max(). +CREATE TEMP TABLE anser_rf_dim (k int8) DISTRIBUTED BY (k); +INSERT INTO anser_rf_dim VALUES (5); +SELECT max(s.total) AS window_total +FROM anser_rf_dim d +LEFT JOIN (SELECT k, count(*) OVER () AS total FROM anser_rf_win) s + ON s.k = d.k; + window_total +-------------- + 100000 +(1 row) + +DROP TABLE anser_rf_dim; +DROP TABLE anser_rf_win; +-- IS NOT DISTINCT FROM below an outer join: ORCA puts a plain `=` in +-- hashclauses and the null-safety test in hashqualclauses, so the join passes +-- the equality gate, and the consumer used to land on the nullable side of +-- the lower LEFT JOIN. Every row it dropped came back NULL-extended and then +-- matched the build side's NULL under INDF -- 99997 rows instead of 0. The +-- resolver must now refuse the descent into the nullable side, so no filter +-- is injected and the count stays 0. +SET optimizer = on; +SET gp_enable_runtime_filter = off; +SET anser.runtime_filter = on; +CREATE TABLE anser_rf_s (x int, y int) DISTRIBUTED BY (x); +CREATE TABLE anser_rf_r (x int, k int) DISTRIBUTED BY (x); +CREATE TABLE anser_rf_b (k int) DISTRIBUTED RANDOMLY; +INSERT INTO anser_rf_s SELECT g, g FROM generate_series(1, 100000) g; +INSERT INTO anser_rf_r SELECT g, g + 1000000 FROM generate_series(1, 100000) g; +INSERT INTO anser_rf_b VALUES (NULL); +INSERT INTO anser_rf_b SELECT g FROM generate_series(1, 10) g; +ANALYZE anser_rf_s; +ANALYZE anser_rf_r; +ANALYZE anser_rf_b; +SELECT count(*) AS indf_rows FROM (anser_rf_s s LEFT JOIN anser_rf_r r ON s.x = r.x) + JOIN anser_rf_b b ON r.k IS NOT DISTINCT FROM b.k; + indf_rows +----------- + 0 +(1 row) + +-- Plain two-table INDF remains sound to filter: the bloom holds the build +-- side's non-NULL keys and NULL probe keys pass through, so matches and the +-- NULL row must all survive -- 1 and 2 are in b (1..10), NULL matches b's. +CREATE TABLE anser_rf_m (k int) DISTRIBUTED RANDOMLY; +INSERT INTO anser_rf_m VALUES (1), (2), (NULL); +SELECT count(*) AS indf_simple_rows FROM anser_rf_m m + JOIN anser_rf_b b ON m.k IS NOT DISTINCT FROM b.k; + indf_simple_rows +------------------ + 3 +(1 row) + +DROP TABLE anser_rf_m, anser_rf_s, anser_rf_r, anser_rf_b; +-- A computed probe key must refuse the filter: ORCA evaluates the cast in +-- the scan's projection (Output: (big)::oid), so the consumer would hash the +-- zero-extended computed oid while the producer hashes the sign-extended +-- stored oid -- every oid >= 2^31 mismatches, and only false positives pass +-- (3 rows instead of 50). The SeqScan shortcut must apply the same +-- plain-Var check as the resolver, so no consumer is injected. +CREATE TABLE anser_rf_p (id int, big int8) DISTRIBUTED BY (id); +CREATE TABLE anser_rf_o (o oid) DISTRIBUTED RANDOMLY; +INSERT INTO anser_rf_p SELECT g, 3000000000 + g FROM generate_series(1, 100000) g; +INSERT INTO anser_rf_o SELECT (3000000000 + g)::oid FROM generate_series(1, 50) g; +ANALYZE anser_rf_p; +ANALYZE anser_rf_o; +SELECT count(*) AS cast_rows + FROM (SELECT id, big::oid AS o FROM anser_rf_p) p + JOIN anser_rf_o b ON b.o = p.o; + cast_rows +----------- + 50 +(1 row) + +DROP TABLE anser_rf_p, anser_rf_o; +RESET gp_enable_runtime_filter; +-- A filter over another filter must resolve the child's INDEX_VAR outputs +-- in the child's namespace, not copy them into a self-referencing namespace. +CREATE TABLE anser_stack_probe (k1 int, k2 int) DISTRIBUTED RANDOMLY; +CREATE TABLE anser_stack_b1 (k int) DISTRIBUTED RANDOMLY; +CREATE TABLE anser_stack_b2 (k int) DISTRIBUTED RANDOMLY; +INSERT INTO anser_stack_probe SELECT i % 100, i % 50 FROM generate_series(1, 10000) i; +INSERT INTO anser_stack_b1 SELECT i FROM generate_series(0, 99) i; +INSERT INTO anser_stack_b2 SELECT i FROM generate_series(0, 49) i; +ANALYZE anser_stack_probe; +ANALYZE anser_stack_b1; +ANALYZE anser_stack_b2; +DO $$ +DECLARE + opt text; + plan jsonb; +BEGIN + PERFORM set_config('anser.runtime_filter', 'on', true); + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + EXECUTE 'EXPLAIN (VERBOSE, COSTS OFF, FORMAT JSON) + SELECT p.k1, p.k2 FROM anser_stack_probe p + JOIN anser_stack_b1 b1 ON p.k1 = b1.k + JOIN anser_stack_b2 b2 ON p.k2 = b2.k' INTO plan; + IF NOT EXISTS ( + WITH RECURSIVE walk(node) AS ( + SELECT plan->0->'Plan' + UNION ALL + SELECT child FROM walk CROSS JOIN LATERAL + jsonb_array_elements(coalesce(node->'Plans', '[]')) child + ) SELECT FROM walk + WHERE node->>'Custom Plan Provider' = 'Anser Bloom Consumer' + AND node->'Plans'->0->>'Custom Plan Provider' = 'Anser Bloom Consumer') THEN + RAISE EXCEPTION 'stacked-consumer EXPLAIN fixture changed (optimizer=%): %', opt, plan; + END IF; + END LOOP; +END +$$; +DROP TABLE anser_stack_probe, anser_stack_b1, anser_stack_b2; +-- Resource and log knobs are superuser-only: an unprivileged role must not +-- force 64 MB filters on every segment or fill the server log. runtime_filter +-- and timeout_ms only shape the caller's own queries, so they stay +-- user-settable. +CREATE ROLE anser_nopriv LOGIN; +NOTICE: resource queue required -- using default resource queue "pg_default" +SET ROLE anser_nopriv; +SET anser.debug_min_filter_bytes = 67108864; +ERROR: permission denied to set parameter "anser.debug_min_filter_bytes" +SET anser.max_info_size = 100000000; +ERROR: permission denied to set parameter "anser.max_info_size" +SET anser.debug = on; +ERROR: permission denied to set parameter "anser.debug" +SET anser.runtime_filter = on; +SET anser.timeout_ms = 100; +RESET ROLE; +DROP ROLE anser_nopriv; +RESET anser.runtime_filter; +RESET anser.debug_min_filter_bytes; +RESET anser.debug; +RESET anser.timeout_ms; +RESET optimizer; +RESET enable_nestloop; +RESET enable_mergejoin; diff --git a/gpcontrib/anser/expected/anser_test.out b/gpcontrib/anser/expected/anser_test.out new file mode 100644 index 00000000000..70f8f574489 --- /dev/null +++ b/gpcontrib/anser/expected/anser_test.out @@ -0,0 +1,437 @@ +CREATE EXTENSION anser_test; +-- Bloom payload protocol: serialize one part and read it back. +SELECT anser_test_bloom_roundtrip('bf_roundtrip', 42); + anser_test_bloom_roundtrip +---------------------------- + t +(1 row) + +-- In-place fold: same-size union mutates the accumulator; a mismatched size is +-- rejected. This is the coordinator's only combine path (the first part is +-- kept verbatim, every later part folds into it). +SELECT anser_test_bloom_fold_inplace() AS fold_inplace_ok; + fold_inplace_ok +----------------- + t +(1 row) + +-- Safety regression: the consumer rebuilds the filter from its own parameters +-- and requires the received bitset to be exactly the expected size (and the +-- header magic to match); truncated/oversized/corrupt parts are rejected. +SELECT anser_test_bloom_rejects_mismatch() AS reject_mismatch; + reject_mismatch +----------------- + t +(1 row) + +-- Producer and consumer driven end to end in one backend: publish a part, let +-- the coordinator side merge it, then receive and query the filter. +SELECT anser_test_node_roundtrip(168); + anser_test_node_roundtrip +--------------------------- + t +(1 row) + +-- --------------------------------------------------------------------------- +-- Sizing, and the decision not to bother. +-- +-- Anser can decline to build a filter at four points, and these cases walk the +-- boundary of each. Declining is always safe: it costs the query its filter, +-- never its correctness. The payload cap is pinned here because every size +-- below follows from it. +-- --------------------------------------------------------------------------- +SET anser.max_info_size = 68157440; +-- 1. The planner's gate -- the only one that costs nothing, since nothing is +-- injected and no consumer is left waiting. A 64 MB bitset is 536870912 bits, +-- so 134217728 estimated keys is exactly 4 bits/key and one more key is not. +-- A zero or negative estimate is treated as one key, not as "no filter"; the +-- 1 MB bitset floor then raises the declared count to match, so every estimate +-- the floor covers shows the same 524288 elements. +SELECT t.est_rows, s.injected, s.total_elems, s.planned_bytes +FROM (VALUES (0), (-5), (1), (1000), (1000000), (10000000), + (134217728), (134217729), (150000000), (1000000000)) + AS t(est_rows), + LATERAL anser_test_rf_size(t.est_rows) AS s; + est_rows | injected | total_elems | planned_bytes +------------+----------+-------------+--------------- + 0 | t | 524288 | 1048576 + -5 | t | 524288 | 1048576 + 1 | t | 524288 | 1048576 + 1000 | t | 524288 | 1048576 + 1000000 | t | 1000000 | 1048576 + 10000000 | t | 10000000 | 16777216 + 134217728 | t | 134217728 | 67108864 + 134217729 | f | | + 150000000 | f | | + 1000000000 | f | | +(10 rows) + +-- 2. Filter construction. bloom_create floors every bitset at 1 MB and that +-- floor overrides the work_mem cap, so a cap below "1 MB + header" can only +-- yield a filter too large to send -- which is why a flat 1 MB cap is refused +-- while 1048592 (the floor plus exactly one header) is accepted and serializes +-- to precisely the cap. fits_cap is the invariant that must never be false. +SELECT t.what, t.elems, t.cap, s.built, s.bits, s.serialized, + round(s.bits_per_key::numeric, 3) AS bits_per_key, + (NOT s.built OR s.serialized <= t.cap) AS fits_cap +FROM (VALUES + ('no keys', 0::bigint, 1048640::bigint), + ('negative keys', -1, 1048640), + ('cap equals the header', 32, 16), + ('cap one byte over the header', 32, 17), + ('cap is 1 MB, no header room', 32, 1048576), + ('cap is 1 MB plus a header', 32, 1048592), + ('cap one byte short of that', 32, 1048591), + ('a single key', 1, 1048640), + ('exactly 4 bits/key', 2097152, 1048640), + ('one key too many', 2097153, 1048640), + ('exactly 4 bits/key at 2 MB', 4194304, 2097216), + ('one key too many at 2 MB', 4194305, 2097216), + ('a billion keys', 1000000000, 68157440)) + AS t(what, elems, cap), + LATERAL anser_test_bloom_shape(t.elems, t.cap) AS s; + what | elems | cap | built | bits | serialized | bits_per_key | fits_cap +------------------------------+------------+----------+-------+----------+------------+--------------+---------- + no keys | 0 | 1048640 | f | | | | t + negative keys | -1 | 1048640 | f | | | | t + cap equals the header | 32 | 16 | f | | | | t + cap one byte over the header | 32 | 17 | f | | | | t + cap is 1 MB, no header room | 32 | 1048576 | f | | | | t + cap is 1 MB plus a header | 32 | 1048592 | t | 8388608 | 1048592 | 262144.000 | t + cap one byte short of that | 32 | 1048591 | f | | | | t + a single key | 1 | 1048640 | t | 8388608 | 1048592 | 8388608.000 | t + exactly 4 bits/key | 2097152 | 1048640 | t | 8388608 | 1048592 | 4.000 | t + one key too many | 2097153 | 1048640 | f | | | | t + exactly 4 bits/key at 2 MB | 4194304 | 2097216 | t | 16777216 | 2097168 | 4.000 | t + one key too many at 2 MB | 4194305 | 2097216 | f | | | | t + a billion keys | 1000000000 | 68157440 | f | | | | t +(13 rows) + +-- 3. The coordinator's gate on a merged payload. The fill is fixed exactly by +-- setting whole bytes rather than by inserting keys, which would be both slow +-- and only statistically precise. The rule is "reject above 95%", so a payload +-- sitting exactly on the limit is still delivered. Malformed framing is not +-- worth delivering either, since it cannot be a filter at all. +SELECT t.what, t.bitset_bytes, t.bytes_set, t.damage, + anser_test_worth_delivering(t.bitset_bytes, t.bytes_set, t.damage) + AS worth_delivering +FROM (VALUES + ('empty', 1000, 0, ''), + ('half full', 1000, 500, ''), + ('just under the limit', 1000, 949, ''), + ('exactly on the limit', 1000, 950, ''), + ('just over the limit', 1000, 951, ''), + ('completely full', 1000, 1000, ''), + ('no bitset at all', 0, 0, ''), + ('corrupt magic', 1000, 0, 'magic'), + ('corrupt version', 1000, 0, 'version'), + ('zero part count', 1000, 0, 'parts'), + ('null payload', 1000, 0, 'null')) + AS t(what, bitset_bytes, bytes_set, damage); + what | bitset_bytes | bytes_set | damage | worth_delivering +----------------------+--------------+-----------+---------+------------------ + empty | 1000 | 0 | | t + half full | 1000 | 500 | | t + just under the limit | 1000 | 949 | | t + exactly on the limit | 1000 | 950 | | t + just over the limit | 1000 | 951 | | f + completely full | 1000 | 1000 | | f + no bitset at all | 0 | 0 | | f + corrupt magic | 1000 | 0 | magic | f + corrupt version | 1000 | 0 | version | f + zero part count | 1000 | 0 | parts | f + null payload | 1000 | 0 | null | f +(11 rows) + +-- 4. The producer driven end to end on the coordinator-local path: build, +-- insert, publish, consume. "no-filter:cancelled" is the case that matters +-- most -- construction declined, so the cancel goes out before the build side +-- is scanned and no consumer waits for its timeout. The last row is the one +-- the planner cannot predict: a filter sized for 32 keys that receives three +-- million, which saturates and so publishes a cancel instead of a payload. +SELECT t.what, + anser_test_producer_decision(t.elems, t.cap, t.n_keys) AS decision +FROM (VALUES + ('sparse filter', 32::bigint, 1048640::bigint, 100), + ('cap with no header room', 32, 1048576, 100), + ('no keys declared', 0, 1048640, 0), + ('one key past the floor', 2097153, 1048640, 0), + ('exactly 4 bits/key', 2097152, 1048640, 100000), + ('estimate of 32, three million inserted', 32, 1048640, 3000000)) + AS t(what, elems, cap, n_keys); + what | decision +----------------------------------------+--------------------- + sparse filter | built:delivered + cap with no header room | no-filter:cancelled + no keys declared | no-filter:cancelled + one key past the floor | no-filter:cancelled + exactly 4 bits/key | built:delivered + estimate of 32, three million inserted | built:cancelled +(6 rows) + +-- --------------------------------------------------------------------------- +-- The wire protocol. +-- +-- Not exhaustive; a fuzzer would be the right tool for that. These cover the +-- properties that are cheap to check and expensive to lose: a fixed-width +-- header with the fields where they are documented, a payload that cannot be +-- mistaken for framing, a length cross-check that rejects a message which does +-- not add up, and a checksum that rejects one altered byte anywhere it covers. +-- --------------------------------------------------------------------------- +-- The bytes, exactly. Session/command/condition are 42/7/3 and part is 1 of 3, +-- so these are golden values: any change to the layout, the field widths or +-- what the checksum covers shows up here as a diff. Note the fourth row -- a +-- cancelled message carries no body even though one was passed, and its +-- checksum must be computed over what is actually sent. +-- +-- That the message comes back as text is itself the check that it is NUL-free, +-- which a NOTIFY payload has to be: the fifth row's body is 00 ff 00. +SELECT t.what, length(m.msg) AS len, m.msg +FROM (VALUES + ('part with a 3-byte body', 'P', 'B', 0, '\x616263'::bytea), + ('subscription, no body', 'S', '-', 0, '\x'::bytea), + ('next request, no body', 'N', '-', 0, '\x'::bytea), + ('cancelled: body suppressed', 'P', 'B', 1, '\x616263'::bytea), + ('body of NUL and 0xff bytes', 'P', 'B', 0, '\x00ff00'::bytea)) + AS t(what, kind, ptype, flags, body), + LATERAL (SELECT anser_test_wire_format(t.kind::"char", t.ptype::"char", + t.flags, 'anser_rf_3', t.body) + AS msg) m; + what | len | msg +----------------------------+-----+--------------------------------------------------------------------------------------------------------------- + part with a 3-byte body | 109 | anser4 P B 0000000042 0000000007 0000000003 0000000001 0000000003 0000 0010 0000000004 c43569b0anser_rf_3YWJj + subscription, no body | 105 | anser4 S - 0000000042 0000000007 0000000003 0000000001 0000000003 0000 0010 0000000000 74e20b74anser_rf_3 + next request, no body | 105 | anser4 N - 0000000042 0000000007 0000000003 0000000001 0000000003 0000 0010 0000000000 d4c4eb99anser_rf_3 + cancelled: body suppressed | 105 | anser4 P B 0000000042 0000000007 0000000003 0000000001 0000000003 0001 0010 0000000000 70bac5ffanser_rf_3 + body of NUL and 0xff bytes | 109 | anser4 P B 0000000042 0000000007 0000000003 0000000001 0000000003 0000 0010 0000000004 31f9d1feanser_rf_3AP8A +(5 rows) + +-- Alter exactly one byte (or one length) and see what the reader does. +-- +-- The split matters: "malformed" is rejected by framing, before a byte is +-- allocated or a channel created, while "checksum-mismatch" got past framing +-- and was caught by the CRC. Corrupting the kind byte is the interesting one +-- -- the message stays perfectly well-formed, and only the checksum notices, +-- which is why the checksum covers the header and not just the payload. +SELECT t.tamper, + anser_test_wire_roundtrip('anser_rf_3', '\x616263'::bytea, t.tamper, + 'B'::"char", 'P'::"char") AS outcome +FROM (VALUES (''), ('truncate'), ('append'), ('bodylen'), ('tag'), ('kind'), + ('type'), ('crc'), ('key'), ('body')) AS t(tamper); + tamper | outcome +----------+------------------- + | ok + truncate | malformed + append | malformed + bodylen | malformed + tag | malformed + kind | checksum-mismatch + type | unknown-type + crc | checksum-mismatch + key | checksum-mismatch + body | checksum-mismatch +(10 rows) + +-- A NEXT request is a message of its own kind: no body, and part_index carries +-- the offset the consumer wants next. The parser must accept it and every +-- field must survive. +SELECT anser_test_wire_roundtrip('anser_rf_3', '\x'::bytea, '', '-'::"char", + 'N'::"char") AS next_roundtrip; + next_roundtrip +---------------- + ok +(1 row) + +-- Keys and bodies that must survive the trip unchanged. "ok" means every +-- field and the decoded body came back identical, so each of these is a case +-- where a naive framing would have gone wrong: the key travels as raw bytes +-- and is taken by length, which is what lets it contain a newline, a space, or +-- the protocol's own tag without any escaping. +SELECT t.what, + anser_test_wire_roundtrip(t.key, t.body, '', 'B'::"char", 'P'::"char") AS outcome +FROM (VALUES + ('plain key', 'anser_rf_3' , '\x616263'::bytea), + ('key containing a newline', E'rf:a\nb' , '\x616263'::bytea), + ('key that looks like a header', 'anser4 P B 0000000000', '\x616263'::bytea), + ('key with spaces and quotes', 'rf:"a b".c' , '\x616263'::bytea), + ('key at the 63-byte limit', repeat('k', 63) , '\x616263'::bytea), + ('empty key', '' , '\x616263'::bytea), + ('empty body', 'anser_rf_3' , '\x'::bytea), + ('body of a single NUL', 'anser_rf_3' , '\x00'::bytea), + ('body of framing-hostile bytes', 'anser_rf_3' , '\x00ff0a0d5c22'::bytea), + ('1 KB body', 'anser_rf_3' , decode(repeat('00ff', 512), 'hex'))) + AS t(what, key, body); + what | outcome +-------------------------------+--------- + plain key | ok + key containing a newline | ok + key that looks like a header | ok + key with spaces and quotes | ok + key at the 63-byte limit | ok + empty key | ok + empty body | ok + body of a single NUL | ok + body of framing-hostile bytes | ok + 1 KB body | ok +(10 rows) + +-- The QD -> QE checksum. The value itself does not matter; which inputs +-- change it does. Every routing field, the chunk's coordinates and the key +-- are always covered, and the body only for a payload type that asks for it -- +-- the other_body_exempt column is that deliberate exemption, not an oversight. +SELECT + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 2, 7, 3, 0, 1000, 0, 'k', '\x01') AS condition_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 8, 3, 0, 1000, 0, 'k', '\x01') AS session_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 4, 0, 1000, 0, 'k', '\x01') AS command_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 1, 1000, 0, 'k', '\x01') AS flags_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 2000, 0, 'k', '\x01') AS total_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 64, 'k', '\x01') AS offset_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'j', '\x01') AS key_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x02') AS bloom_body_covered, + anser_test_push_crc('-'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + = anser_test_push_crc('-'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x02') AS other_body_exempt, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('-'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') AS type_covered; + condition_covered | session_covered | command_covered | flags_covered | total_covered | offset_covered | key_covered | bloom_body_covered | other_body_exempt | type_covered +-------------------+-----------------+-----------------+---------------+---------------+----------------+-------------+--------------------+-------------------+-------------- + t | t | t | t | t | t | t | t | t | t +(1 row) + +-- 5. The runtime-filter pushdown decision. A scan key is numbered by the +-- table's attributes, while the consumer is numbered by the scan's output +-- targetlist -- the slot it probes has already been projected. So the whole +-- job of the decision is that mapping, plus refusing every shape where there +-- is no mapping to make. sk_attno is what the key would carry; NULL means +-- refused, and the consumer then probes the slot itself (always correct, just +-- slower). In the tlist spec a positive value is a plain Var on that table +-- attno; 0 is a whole-row Var, -1 a system column, -2 a computed column, -3 a +-- Var of another relation, -4 a cast wrapping a Var. +SELECT t.what, t.tlist, t.attno, s.accepts, s.sk_attno +FROM (VALUES + ('all columns in order', '{1,2,3}'::int4[], 2, true, true), + ('projecting scan, one column', '{2}', 1, true, true), + ('reordered, first of two', '{3,1}', 1, true, true), + ('reordered, second of two', '{3,1}', 2, true, true), + ('position past the targetlist', '{2}', 2, true, true), + ('position zero', '{2}', 0, true, true), + ('negative position', '{2}', -1, true, true), + ('computed column', '{-2}', 1, true, true), + ('cast wrapping a column', '{-4}', 1, true, true), + ('whole-row reference', '{0}', 1, true, true), + ('system column', '{-1}', 1, true, true), + ('column of another relation', '{-3}', 1, true, true), + ('filter_in_seqscan off', '{2}', 1, false, true), + ('not a SeqScan', '{2}', 1, true, false) + ) AS t(what, tlist, attno, filter_in_seqscan, as_seqscan), + LATERAL anser_test_pushdown_accepts(t.tlist, t.attno, t.filter_in_seqscan, + t.as_seqscan) AS s; + what | tlist | attno | accepts | sk_attno +------------------------------+---------+-------+---------+---------- + all columns in order | {1,2,3} | 2 | t | 2 + projecting scan, one column | {2} | 1 | t | 2 + reordered, first of two | {3,1} | 1 | t | 3 + reordered, second of two | {3,1} | 2 | t | 1 + position past the targetlist | {2} | 2 | f | + position zero | {2} | 0 | f | + negative position | {2} | -1 | f | + computed column | {-2} | 1 | f | + cast wrapping a column | {-4} | 1 | f | + whole-row reference | {0} | 1 | f | + system column | {-1} | 1 | f | + column of another relation | {-3} | 1 | f | + filter_in_seqscan off | {2} | 1 | f | + not a SeqScan | {2} | 1 | f | +(14 rows) + +-- 6. The walk that finds where to push. With nothing to descend through it +-- must agree with the decision and name the node itself; a node that refuses +-- yields no target at all rather than a target with a meaningless attno. The +-- descent through stacked nodes needs live CustomScanStates, so it is covered +-- end to end by anser_runtime_filter instead. +SELECT t.what, s.found_self, s.sk_attno +FROM (VALUES + ('accepting scan is its own target', '{2}'::int4[], 1, true), + ('refused scan has no target', '{-2}', 1, true), + ('non-scan has no target', '{2}', 1, false) + ) AS t(what, tlist, attno, as_seqscan), + LATERAL anser_test_pushdown_target(t.tlist, t.attno, true, + t.as_seqscan) AS s; + what | found_self | sk_attno +----------------------------------+------------+---------- + accepting scan is its own target | t | 2 + refused scan has no target | f | + non-scan has no target | f | +(3 rows) + +-- 7. How many producers a slice runs. This number is the channel's +-- completion criterion, so over-counting costs every consumer the full +-- anser.timeout_ms -- the channel never reaches the count it waits for and +-- nobody is told -- while under-counting completes it early and hands out a +-- filter missing a segment's keys, which drops rows that join. So each of +-- FillSliceGangInfo's four formulas is pinned here, not inferred from one. +-- +-- numsegments is only meaningful for a reader or writer gang; the columns are +-- still filled for the other types to show they are ignored. +SELECT t.what, t.gang, t.nseg, t.workers, t.direct, + anser_test_slice_producers(t.gang, t.nseg, t.workers, t.direct, 0) + AS producers +FROM (VALUES + ('coordinator slice', 'unallocated', 3, 0, NULL::int4[]), + ('coordinator slice, parallel', 'unallocated', 3, 4, NULL), + ('entry db reader', 'entrydb', 3, 0, NULL), + ('singleton reader', 'singleton', 3, 0, NULL), + ('singleton reader, parallel', 'singleton', 3, 4, NULL), + ('reader, serial', 'reader', 3, 0, NULL), + ('reader, parallel', 'reader', 3, 4, NULL), + ('writer, serial', 'writer', 3, 0, NULL), + ('direct dispatch, one segment', 'reader', 3, 0, '{1}'), + ('direct dispatch, two, serial', 'reader', 3, 0, '{0,2}'), + ('direct dispatch, two, par 4', 'reader', 3, 4, '{0,2}'), + ('direct dispatch, nowhere', 'reader', 3, 4, '{}'), + ('no segments', 'reader', 0, 4, NULL), + ('negative segments', 'reader', -1, 4, NULL) + ) AS t(what, gang, nseg, workers, direct); + what | gang | nseg | workers | direct | producers +------------------------------+-------------+------+---------+--------+----------- + coordinator slice | unallocated | 3 | 0 | | 1 + coordinator slice, parallel | unallocated | 3 | 4 | | 1 + entry db reader | entrydb | 3 | 0 | | 1 + singleton reader | singleton | 3 | 0 | | 1 + singleton reader, parallel | singleton | 3 | 4 | | 4 + reader, serial | reader | 3 | 0 | | 3 + reader, parallel | reader | 3 | 4 | | 12 + writer, serial | writer | 3 | 0 | | 3 + direct dispatch, one segment | reader | 3 | 0 | {1} | 1 + direct dispatch, two, serial | reader | 3 | 0 | {0,2} | 2 + direct dispatch, two, par 4 | reader | 3 | 4 | {0,2} | 8 + direct dispatch, nowhere | reader | 3 | 4 | {} | 0 + no segments | reader | 0 | 4 | | 0 + negative segments | reader | -1 | 4 | | 0 +(14 rows) + +-- The slice has to be identifiable at all; anything else is zero, which the +-- injection pass reads as "unknown" and declines on. +SELECT t.what, anser_test_slice_producers('reader', 3, 4, NULL, t.idx) + AS producers +FROM (VALUES + ('slice 0 of 1 exists', 0), + ('slice 1 of 1 does not', 1), + ('negative slice index', -1) + ) AS t(what, idx); + what | producers +-----------------------+----------- + slice 0 of 1 exists | 12 + slice 1 of 1 does not | 0 + negative slice index | 0 +(3 rows) + +RESET anser.max_info_size; +DROP EXTENSION anser_test; diff --git a/gpcontrib/anser/include/anser.h b/gpcontrib/anser/include/anser.h new file mode 100644 index 00000000000..5c87f3295d4 --- /dev/null +++ b/gpcontrib/anser/include/anser.h @@ -0,0 +1,107 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anser.h + * Shared definitions for the Anser adaptive information sharing + * subsystem. + * + * Anser lets producers on the segments publish a small piece of information + * about a running query -- today a bloom filter over a join-build key -- have + * the coordinator combine the per-segment parts, and hand the result back to + * consumers on the segments, which use it to prune work. + * + * Everything travels over the dispatch connection the coordinator already + * holds open to each segment (see ansersideband.h), so there is no shared + * memory, no background worker and no second connection to authenticate: a + * channel exists only in the coordinator backend running the query, for as + * long as that query runs. + * + * IDENTIFICATION + * gpcontrib/anser/include/anser.h + * + *------------------------------------------------------------------------- + */ +#ifndef ANSER_H +#define ANSER_H + +#include "postgres.h" + +#define ANSER_CONDITION_KEY_SIZE 64 + +/* + * What a sideband delivery costs on the wire besides the payload: the fixed + * header fields plus the condition key, rounded up. Not to be confused with + * the room a serialized bloom part keeps for its own header -- that one is + * about the payload's contents, this one about the frame around it. + * + * anser.max_info_size caps the payload, so payload + this is the largest + * message a consumer can be asked to read, and the reason that GUC cannot be + * set as high as an int goes. + */ +#define ANSER_SIDEBAND_FRAME_ROOM (ANSER_CONDITION_KEY_SIZE + 64) + +/* + * Identifies one channel: a single condition within one running command. + * + * condition_key is an opaque symbol naming the condition -- the + * optimizer-generated equivalence-class symbol described in the Anser paper. + * Channels are only ever compared for equality, never ordered. + * + * Zero the whole struct before filling it in. The coordinator keeps channels + * in a HASH_BLOBS table keyed by sizeof(AnserChannelKey), so every byte of it + * is hashed and compared, the bytes past the key's terminating NUL included -- + * and neither strlcpy() nor a memcpy() of a shorter key clears those. Two + * sides naming the same condition would then land on different channels, and + * what that looks like from the outside is consumers timing out for no + * visible reason. + */ +typedef struct AnserChannelKey +{ + int gp_session_id; + int gp_command_count; + uint32 condition_id; + /* NUL-terminated, and zero-padded to the end -- see above. */ + char condition_key[ANSER_CONDITION_KEY_SIZE]; +} AnserChannelKey; + +/* GUCs (defined in anserinit.c). */ +extern PGDLLEXPORT bool gp_anser_enable; +extern PGDLLEXPORT bool gp_anser_runtime_filter; +extern PGDLLEXPORT bool gp_anser_debug; +extern PGDLLEXPORT int gp_anser_max_info_size; +extern PGDLLEXPORT int gp_anser_timeout_ms; +extern PGDLLEXPORT int gp_anser_debug_min_filter_bytes; +extern PGDLLEXPORT int gp_anser_debug_delivery_chunk_bytes; + +/* + * Trace the handoff between producers, the coordinator and consumers. + * + * Every step of the exchange is invisible in EXPLAIN until it is over -- a + * consumer that waits and gets nothing looks exactly like one whose producers + * never published -- so the path is traceable on demand rather than only + * reconstructable from a debugger. LOG level, so it lands in the log of + * whichever process it happened in. + */ +#define ANSER_DEBUG(...) \ + do { \ + if (gp_anser_debug) \ + elog(LOG, __VA_ARGS__); \ + } while (0) + +#endif /* ANSER_H */ diff --git a/gpcontrib/anser/include/anserbloom.h b/gpcontrib/anser/include/anserbloom.h new file mode 100644 index 00000000000..68d5c904d08 --- /dev/null +++ b/gpcontrib/anser/include/anserbloom.h @@ -0,0 +1,81 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserbloom.h + * Standalone Anser Bloom filter producer/consumer executor helpers. + * + * IDENTIFICATION + * gpcontrib/anser/include/anserbloom.h + * + *------------------------------------------------------------------------- + */ +#ifndef ANSERBLOOM_H +#define ANSERBLOOM_H + +#include "postgres.h" + +#include "anser.h" +#include "lib/bloomfilter.h" + +/* + * Opaque executor state handles for the Bloom filter producer and + * consumer; the struct definitions are private to + * anserbloomproduce.c and anserbloomconsume.c. + */ +typedef struct AnserBloomFilterProduceState AnserBloomFilterProduceState; +typedef struct AnserBloomFilterConsumeState AnserBloomFilterConsumeState; + +/* Producer side: build one Bloom filter part and publish it to the channel. */ +extern PGDLLEXPORT AnserBloomFilterProduceState *ExecInitAnserBloomFilterProduce( + const AnserChannelKey *channel_key, + int64 total_elems, + Size max_payload_bytes, + uint32 part_index, + uint32 total_parts); +extern PGDLLEXPORT void ExecAnserBloomFilterProduceAddDatum(AnserBloomFilterProduceState *state, + Datum value, bool isnull); +extern PGDLLEXPORT bool ExecAnserBloomFilterProduceHasFilter(AnserBloomFilterProduceState *state); +extern PGDLLEXPORT bool ExecAnserBloomFilterProducePublish(AnserBloomFilterProduceState *state); +extern PGDLLEXPORT bool ExecAnserBloomFilterProduceCancel(AnserBloomFilterProduceState *state); +extern PGDLLEXPORT void ExecEndAnserBloomFilterProduce(AnserBloomFilterProduceState *state); + +/* Consumer side: gather all parts of a channel into one Bloom filter. */ +extern PGDLLEXPORT AnserBloomFilterConsumeState *ExecInitAnserBloomFilterConsume( + const AnserChannelKey *channel_key, + int64 total_elems, + Size max_payload_bytes, + uint32 expected_parts); +extern PGDLLEXPORT bool ExecAnserBloomFilterConsume(AnserBloomFilterConsumeState *state, + long registration_timeout_ms); +extern PGDLLEXPORT bloom_filter *ExecAnserBloomFilterConsumerGetFilter( + AnserBloomFilterConsumeState *state); +/* + * The producer count the delivered payload declares, or 0 if no filter was + * built. Every part of a channel carries that count and folding does not + * change it, so this is a cross-check on the payload, not a tally of arrivals: + * the coordinator delivers only once it holds every part, so a filter that + * reaches a consumer at all is a complete one. + */ +extern PGDLLEXPORT uint32 ExecAnserBloomFilterConsumerReceivedParts( + AnserBloomFilterConsumeState *state); +extern PGDLLEXPORT bool ExecAnserBloomFilterConsumerWasCancelled( + AnserBloomFilterConsumeState *state); +extern PGDLLEXPORT void ExecEndAnserBloomFilterConsume(AnserBloomFilterConsumeState *state); + +#endif /* ANSERBLOOM_H */ diff --git a/gpcontrib/anser/include/anserfilter.h b/gpcontrib/anser/include/anserfilter.h new file mode 100644 index 00000000000..4db4d0563e4 --- /dev/null +++ b/gpcontrib/anser/include/anserfilter.h @@ -0,0 +1,127 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserfilter.h + * Bloom-filter payload helpers for Anser channels. + * + * IDENTIFICATION + * gpcontrib/anser/include/anserfilter.h + * + *------------------------------------------------------------------------- + */ +#ifndef ANSERFILTER_H +#define ANSERFILTER_H + +#include "postgres.h" + +#include "lib/bloomfilter.h" + +#define ANSER_BLOOM_PART_MAGIC 0x41424631U /* ABF1 */ +#define ANSER_BLOOM_PART_VERSION 1U + +/* + * On-wire framing for a serialized bloom part. It deliberately does NOT carry + * the bitset parameters (size / seed / hash count): both the producer and the + * consumer build the filter with bloom_create from the same plan parameters, so + * the shape is agreed by construction and never reconstructed from the wire. + * magic/version guard the framing; part_index/total_parts track the coordinator + * fold count (surfaced as diagnostics). Kept as a struct for forward + * extensibility. + */ +typedef struct AnserBloomPartHeader +{ + uint32 magic; + uint32 version; + uint32 part_index; + uint32 total_parts; +} AnserBloomPartHeader; + +/* + * When a bloom filter stops being worth building, or sending once built. + * Three checks, at the three points where new information arrives. + * + * ANSER_BLOOM_MIN_BITS_PER_KEY -- a floor on the *planned* density: bitset bits + * divided by the number of distinct keys we expect. Below it no amount of care + * in building the filter helps, so the filter is not built and, at plan time, + * the nodes are not even injected. At 4 bits/key the optimal hash count is 3 + * and the false positive rate is already ~15%; below that it collapses (2 + * bits/key is ~40%). + * + * ANSER_BLOOM_MAX_FPR -- the check on the *realized* filter, at publish time. + * It catches what a row estimate cannot: the estimate was too low, so the + * filter saturated anyway. This one is expressed as a false positive rate + * rather than a fill fraction on purpose. FPR is fill^k, so a single fill + * limit is not a single quality bar: 80% full is a 51% FPR at k=3 but only + * 10.7% at k=10, and a rule that cancelled the latter would be throwing away a + * filter that eliminates nine probe rows in ten. The producer has the filter + * and therefore its k, so it can just ask. + * + * ANSER_BLOOM_MERGED_MAX_FILL -- the same question asked by the coordinator of + * the merged payload, which is the only place it can be asked about what + * consumers will actually receive. It has to fall back on fill, because k is + * derived from plan parameters and is not carried in the serialized part. It + * is therefore set where even k=10 is past saving (0.95^10 = 60% FPR), so that + * it only ever rejects the hopeless. Putting k in AnserBloomPartHeader would + * let this use the FPR too. + * + * All three numbers are first cuts. Calibrating them is the "stop building + * filters that add nothing" part of the bloom-performance work; the debug trace + * logs fill and FPR at every decision point so that study has data. + */ +#define ANSER_BLOOM_MIN_BITS_PER_KEY 4.0 +#define ANSER_BLOOM_MAX_FPR 0.50 +#define ANSER_BLOOM_MERGED_MAX_FILL 0.95 + +/* + * Is a serialized part (or a merged accumulator of them) still worth + * delivering? False when too many of its bits are set for it to reject + * anything useful. Works on the wire form, so the coordinator can ask this of + * a merged payload without rebuilding a filter. + */ +extern PGDLLEXPORT bool AnserBloomPartWorthSending(const void *payload, Size payload_len); + +extern PGDLLEXPORT uint64 AnserBloomSeed(const char *condition_key); + +/* + * Build an empty filter, or return NULL when the sizing rules say it is not + * worth building. A NULL return is not an error: the producer turns it into an + * immediate cancel, so consumers stop waiting instead of timing out. + */ +extern PGDLLEXPORT bloom_filter *AnserBloomCreate(int64 total_elems, + Size max_payload_bytes, + uint64 seed); +extern PGDLLEXPORT Size AnserBloomSerializedSize(const bloom_filter *filter); +extern PGDLLEXPORT bool AnserBloomSerializePart(const bloom_filter *filter, + uint32 part_index, + uint32 total_parts, + void *buffer, + Size buffer_size, + Size *payload_len); +extern PGDLLEXPORT bloom_filter *AnserBloomDeserializePart(const void *payload, + Size payload_len, + int64 total_elems, + Size max_payload_bytes, + uint64 seed, + uint32 *part_index, + uint32 *total_parts); +extern PGDLLEXPORT bool AnserBloomLooksLikePart(const void *payload, Size payload_len); +extern PGDLLEXPORT bool AnserBloomFoldPartInPlace(void *acc, Size acc_len, + const void *part, Size part_len); + +#endif /* ANSERFILTER_H */ diff --git a/gpcontrib/anser/include/anserpayload.h b/gpcontrib/anser/include/anserpayload.h new file mode 100644 index 00000000000..18ab6bab108 --- /dev/null +++ b/gpcontrib/anser/include/anserpayload.h @@ -0,0 +1,150 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserpayload.h + * What a channel carries, and who knows what to do with it. + * + * The transport (ansersideband.c on segments, anserdispatch.c on the + * coordinator) moves opaque bytes: it cannot tell a bloom filter from a row + * count, and should not have to. Everything that depends on what the bytes + * mean lives in one descriptor per payload type, so a new kind of runtime + * information needs no change to the framing, the checksumming or the channel + * bookkeeping. + * + * Exactly two things genuinely differ per type. + * + * How parts merge. The coordinator receives one part per producer and must + * reduce them to a single payload. For a bloom filter that is a bitwise OR of + * equal-sized bitsets; for a row count it would be a sum; for a min/max + * summary it would be two comparisons. There is no generic answer, so each + * type supplies fold(). + * + * Whether corruption can give a wrong answer. This is what 'checksum_body' + * decides, and it is about consequence, not size. A bloom filter fails + * asymmetrically: a bit flipped 0 -> 1 merely costs selectivity, but a bit + * flipped 1 -> 0 removes a key from the filter, so a joinable row is rejected + * and the query returns fewer rows than it should -- silently. A row count, by + * contrast, only feeds a planning decision: a corrupted one costs a worse plan, + * never an incorrect result. It does not need to be checksummed, and paying + * 0.05 ms/MB to checksum it anyway would be waste. + * + * Note what 'checksum_body' does *not* cover: the header and the condition key + * are checksummed for every message regardless. That is ~100 ns for a 95-byte + * header and it is what validates the routing fields, so there is nothing to + * gain by skipping it; the flag controls only the payload, which is where the + * cost lives. + * + * To add a payload type: + * + * 1. #define a code below. It travels as one byte and appears in the + * anser.debug trace, so keep it printable -- and it may not be '\0', + * which AnserPayloadTable uses to recognize a slot nobody registered. + * 2. Write a fold() and add a row to AnserPayloadTable in anserpayload.c, + * keyed by the code: the table is indexed by the wire byte, so the + * designator and the row's own 'code' field must agree (an assertion in + * AnserPayloadLookup catches it if they do not). + * 3. Pass the code to AnserSidebandPublish/AnserDispatchLocalPublish in the + * producer node, and to AnserSidebandConsumeWait/AnserDispatchLocalConsume + * in the consumer node. + * + * That is the whole extension point. In particular, do not add a branch on the + * payload type to the transport: if something there needs to know the type, + * that knowledge belongs in this descriptor instead. + * + * IDENTIFICATION + * gpcontrib/anser/include/anserpayload.h + * + *------------------------------------------------------------------------- + */ +#ifndef ANSERPAYLOAD_H +#define ANSERPAYLOAD_H + +#include "anser.h" + +/* + * Payload type codes. + * + * Distinct from the message kind (P/S), which says what a message *does* + * rather than what it *carries*: a subscription carries nothing, and a + * cancelled part still declares the type it would have carried. + */ +#define ANSER_PAYLOAD_NONE '-' /* no payload (a subscription) */ +#define ANSER_PAYLOAD_BLOOM 'B' /* serialized bloom filter part */ + +/* '\0' is reserved: it is how an unregistered code is recognized. */ + +typedef struct AnserPayloadOps +{ + char code; /* the wire byte */ + const char *name; /* for logs; no other use */ + bool checksum_body; /* see the file header */ + + /* + * Merge 'part' into the accumulator in place. + * + * The accumulator is the first part the channel received, kept verbatim, so + * the two always have the same type -- but not necessarily the same length + * or internal parameters, which is what this has to check. Return false if + * they cannot be merged; the caller then cancels the channel, which costs + * the consumers their filter but is never silently wrong. + * + * Must not modify 'accum' unless it returns true: the caller reports a + * false return and carries on, so a half-folded accumulator would be + * indistinguishable from a good one. + * + * NULL for a type that is never folded (one that has no payload). + */ + bool (*fold) (void *accum, Size accum_len, + const void *part, Size part_len); + + /* + * Is the finished accumulator worth delivering? Asked once, after the last + * part has been folded and before anything is sent to a consumer; a false + * return cancels the channel, so consumers run unfiltered. + * + * This is the only place the question can be answered honestly, because + * folding changes the answer: OR-ing bloom parts together makes the result + * denser than any part, so three parts that each looked worth sending can + * merge into one that is not. Producers cannot see that, and consumers + * should not be made to pay for it. + * + * NULL for a type that is always worth delivering -- a row count does not + * become less useful for having been summed. + */ + bool (*worth_delivering) (const void *accum, Size accum_len); +} AnserPayloadOps; + +/* + * Resolve a code to its descriptor, or NULL if nothing has registered it. + * + * Callers must handle the NULL: the code arrives from the wire, so an + * unregistered one is a real case, and defaulting it silently would turn a + * forgotten table row into a mystery rather than a log line. + */ +extern PGDLLEXPORT const AnserPayloadOps *AnserPayloadLookup(char code); + +/* + * Whether a type's body is covered by the message checksum. Takes a code + * rather than a descriptor because the transport reaches this while parsing, + * before it has resolved anything; an unregistered code answers false, which + * is why this one needs no error handling. + */ +extern PGDLLEXPORT bool AnserPayloadChecksumsBody(char code); + +#endif /* ANSERPAYLOAD_H */ diff --git a/gpcontrib/anser/include/anserplan.h b/gpcontrib/anser/include/anserplan.h new file mode 100644 index 00000000000..35362c0ee78 --- /dev/null +++ b/gpcontrib/anser/include/anserplan.h @@ -0,0 +1,178 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserplan.h + * Post-planning transformation that injects Anser runtime bloom-filter + * producer/consumer nodes into a finished plan tree. + * + * IDENTIFICATION + * gpcontrib/anser/include/anserplan.h + * + *------------------------------------------------------------------------- + */ +#ifndef ANSERPLAN_H +#define ANSERPLAN_H + +#include "nodes/execnodes.h" /* PlanState, for the pushdown helpers */ +#include "nodes/plannodes.h" + +/* + * Post-plan pass: recognize the supported join shape in a finished PlannedStmt + * and inject an Anser bloom-filter producer (on the hash build side) and a + * consumer (above the probe scan). Called once from planner(), so it covers + * both the Postgres planner and ORCA. A no-op unless the Anser runtime-filter + * GUCs are on and this is a coordinator SELECT. + */ +extern PGDLLEXPORT void AnserApplyRuntimeFilters(PlannedStmt *stmt); + +/* + * Register the two CustomScan providers (producer, consumer) so their methods + * resolve by name when a dispatched plan is deserialized. Must run once per + * backend (QD and every QE) before any plan execution. + */ +extern PGDLLEXPORT void AnserRegisterRuntimeFilterMethods(void); + +/* + * Node builders (implemented in anserplanexec.c, where the CustomScan method + * tables live). Each wraps `child` in a pass-through CustomScan carrying the + * runtime-filter parameters in custom_private; the caller assigns plan_node_id. + * `key_attno` is the build (producer) / probe (consumer) join-key attno in the + * child's output tuple. `n_producers` is how many processes will publish a + * part -- the width of the build scan's slice, which under parallel execution + * is numsegments * parallel_workers rather than the segment count. + */ +/* + * True for the producer and consumer nodes this module injects. The injection + * pass meets them where it expects a scan, because two joins can want to filter + * the same base relation; coordinator-only, see the implementation. + */ +extern PGDLLEXPORT bool AnserIsRuntimeFilterScan(const Plan *plan); + +extern PGDLLEXPORT CustomScan *AnserBuildBloomProducerScan( + Plan *child, + AttrNumber key_attno, + uint32 condition_id, + const char *condition_key, + int64 total_elems, + Size max_payload_bytes, + int64 planned_bytes, + int n_producers); +/* + * `defer_first` tells the consumer to pass its first tuple through unfiltered. + * ExecHashJoin pulls one outer tuple before building its hash table, and a + * consumer that blocks there waits for an inner side the join has not started + * -- in a co-located join, for its own process. Set it whenever the prefetch + * cannot be ruled out; the cost is that such a consumer cannot push its filter + * into the scan and probes it itself instead. + */ +extern PGDLLEXPORT CustomScan *AnserBuildBloomConsumerScan( + Plan *child, + AttrNumber key_attno, + uint32 condition_id, + const char *condition_key, + int64 total_elems, + Size max_payload_bytes, + int64 planned_bytes, + int n_producers, + bool defer_first); + +/* + * Bloom sizing for one join, from its estimated build cardinality. False means + * no filter is worth injecting -- see the density rule in anserfilter.h. + * + * Exposed rather than static because it is the cheapest of Anser's give-up + * decisions and therefore the one most worth testing directly at its boundary + * (anser_test.c); nothing but the injection pass and the tests should call it. + */ +extern PGDLLEXPORT bool AnserRuntimeFilterSize(double est_rows, + int64 *total_elems, + int64 *max_payload, + int64 *planned_bytes); + +/* + * Follow a key column of `top` down to the base scan that originates it, + * through the nodes that pass a column through unchanged (Hash, Motion), the + * joins that carry one side's columns up (HashJoin), and our own nodes. True + * on success, with *scan_out the scan, *attno_out the key's attno in it, + * *parent_out the node holding it (ours keep it in custom_plans, everything + * else in the outer link) and *slice_out the slice the scan runs in. + * + * False means no single base scan holds the whole key column -- an Append or a + * SetOp, say, where the column comes from several relations at once and a + * filter built on any one of them would reject rows that can join. A false + * return is therefore a correctness gate, not a missed optimization: callers + * must decline rather than guess. + * + * `for_consumer` is true when resolving the consumer's (probe) key and false + * for the producer's (build) key: a consumer removes rows, so the descent + * refuses an outer join's nullable side, where pruning rewrites a dropped row + * into a NULL-extended one that a downstream condition can distinguish from + * "no row" (IS NOT DISTINCT FROM matches NULL to NULL). A producer only + * summarizes the column, and a superset of the outer join's emitted keys is + * all it needs, so it may descend anywhere. + * + * Only meaningful after set_plan_references, which is also what makes the + * descent necessary: before it the Vars still name their base relations. + */ +extern PGDLLEXPORT bool AnserResolveKeyScan(Plan *top, + AttrNumber key_attno, + int slice_index, + bool for_consumer, + Plan **parent_out, + Plan **scan_out, + AttrNumber *attno_out, + int *slice_out); + +/* + * How many processes execute `slice_index` of `stmt` -- the number of parts a + * channel fed from that slice must wait for. Zero means the slice could not be + * identified, and the caller must decline rather than assume: a channel that + * expects too few parts completes early and hands out a filter missing some + * segment's keys, which drops rows that can join. + * + * Not the segment count. A parallel slice runs numsegments * parallel_workers + * full QEs, each publishing for itself, and the two optimizers choose different + * widths for the same query -- see the implementation. + */ +extern PGDLLEXPORT int AnserMaxPlanNodeId(PlannedStmt *stmt); + +extern PGDLLEXPORT int AnserSliceProducers(PlannedStmt *stmt, int slice_index); + +/* + * Runtime-filter pushdown, split so the decision can be tested on its own. + * + * AnserPushdownAccepts answers "would this node take a scan key for the column + * it emits at output position `attno`?", and on yes reports the attribute + * number the key must carry -- which is the table's attno, not `attno`, because + * the scan evaluates the key against an unprojected tuple. + * + * AnserPushdownTarget walks down from `top` following that column and returns + * the deepest accepting node, so a stack of consumers over one relation all + * push into the same scan instead of probing tuple by tuple. + * + * Both return false/NULL rather than failing when the shape is not supported; + * the caller's fallback is to probe the filter itself, which is always correct. + */ +extern PGDLLEXPORT bool AnserPushdownAccepts(PlanState *ps, AttrNumber attno, + AttrNumber *sk_attno_out); +extern PGDLLEXPORT PlanState *AnserPushdownTarget(PlanState *top, + AttrNumber attno, + AttrNumber *sk_attno_out); + +#endif /* ANSERPLAN_H */ diff --git a/gpcontrib/anser/include/ansersideband.h b/gpcontrib/anser/include/ansersideband.h new file mode 100644 index 00000000000..fc820b050b6 --- /dev/null +++ b/gpcontrib/anser/include/ansersideband.h @@ -0,0 +1,277 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * ansersideband.h + * Anser transport over the existing QD <-> QE dispatch connection. + * + * Instead of opening a second libpq connection back to the coordinator, a + * segment executor reuses the connection the dispatcher already holds open: + * QE -> QD travels as a NOTIFY (the model nextval() uses, see + * cdb_sequence_nextval_qe in commands/sequence.c), and QD -> QE as a + * GP_SIDEBAND_MESSAGE the waiting QE reads off its own socket. + * + * The two directions are not symmetric, and the wire formats differ for a + * reason. A NOTIFY payload is delivered by pq_sendstring(), so it must be a + * NUL-free C string -- hence the text header and base64 body. The QD -> QE + * push is written with pqPutnchar(), which performs no conversion, so the + * merged filter travels as raw binary. That matters: the merged payload is + * sent once per consumer, while each part is sent once. + * + * Both directions carry a CRC32C. Not because the transport is lossy -- it is + * TCP, or a Unix socket; the UDP interconnect is a different channel entirely + * -- but because some payloads fail asymmetrically and TCP's 16-bit checksum is + * thin cover for a megabyte. The header and condition key are always covered; + * whether the body is covered too is the payload type's decision + * (AnserPayloadOps.checksum_body, see anserpayload.h), because that is the part + * that costs 0.05 ms/MB and only some payloads can turn corruption into a wrong + * answer. The QE -> QD checksum covers the raw pre-base64 bytes, so where it + * applies it validates the decode as well. + * + * Neither direction branches on the payload type beyond consulting that one + * flag: everything else about what the bytes mean belongs in the registry. + * + * IDENTIFICATION + * gpcontrib/anser/include/ansersideband.h + * + *------------------------------------------------------------------------- + */ +#ifndef ANSERSIDEBAND_H +#define ANSERSIDEBAND_H + +#include "anser.h" +#include "anserpayload.h" +#include "port/pg_crc32c.h" + +struct CdbDispatchResult; /* #include "cdb/cdbdispatchresult.h" */ +struct pgNotify; /* #include "libpq-fe.h" */ + +/* + * NOTIFY channel for QE -> QD Anser traffic. The dispatcher hands notifies it + * does not recognize to cdbdisp_notify_hook, where we match on this name. + */ +#define ANSER_NOTIFY_CHANNEL "anser_rf" + +/* First token of every QE -> QD payload; bump when the format changes. */ +#define ANSER_WIRE_TAG "anser4" + +/* + * Message kinds (QE -> QD): what the message does. What it carries is a + * separate field, the payload type -- see anserpayload.h. + * + * NEXT is the flow-control request of the chunked delivery: the coordinator + * pushes one chunk per GP_SIDEBAND_MESSAGE and waits for the consumer to ask + * for the following one, so no more than one chunk -- always smaller than the + * connection's send buffer -- is ever in flight. In a NEXT, part_index is + * the offset the consumer wants next (what it has received so far), + * total_parts is 0, and there is no body. + */ +#define ANSER_WIRE_KIND_PART 'P' /* a producer's serialized part */ +#define ANSER_WIRE_KIND_SUBSCRIBE 'S' /* a consumer registering interest */ +#define ANSER_WIRE_KIND_UNSUBSCRIBE 'U' /* ... and withdrawing it again */ +#define ANSER_WIRE_KIND_NEXT 'N' /* request the next chunk; see above */ + +/* Flag bits, shared by both directions. */ +#define ANSER_WIRE_F_CANCELLED 0x0001 + +/* + * QE -> QD header: fixed width, so the key and body start at known offsets and + * nothing about their contents can affect parsing. + * + * anser4 K T SSSSSSSSSS CCCCCCCCCC DDDDDDDDDD PPPPPPPPPP TTTTTTTTTT FFFF KKKK BBBBBBBBBB XXXXXXXX + * ^tag ^ ^ session command condition part total ^ ^ bodylen crc + * | \ payload type flags keylen + * \ kind + * + * followed immediately by key bytes and body bytes. Every + * field is zero-padded (flags and CRC are hex, the rest decimal), so the header + * is pure ASCII of a constant length and holds no delimiter that payload bytes + * could imitate. The two lengths are then a cross-check: the total payload + * length must be exactly the header plus both, or the message is rejected. + * + * The signed session and command ids travel as uint32 and are cast back on + * receipt, which round-trips them exactly while keeping the width fixed -- + * "%010d" of a negative value is eleven characters, which would have made the + * header variable-width again. + * + * The previous format ended its header with a newline and found it with + * strchr(). That was correct only because the header could not itself contain + * a newline -- an invariant nothing enforced, and one that a key derived from + * relation names (quoted identifiers may contain anything) would have broken + * silently. + */ +#define ANSER_WIRE_HDR_FORMAT \ + ANSER_WIRE_TAG " %c %c %010u %010u %010u %010u %010u %04x %04u %010u %08x" + +/* + * Read format for the same header. The widths are what make the parse safe: + * every conversion stops after exactly its field, so no value can consume the + * separator or run into the key bytes that follow the header. + */ +#define ANSER_WIRE_HDR_SCANF \ + ANSER_WIRE_TAG " %c %c %10u %10u %10u %10u %10u %4x %4u %10u %8x" + +/* 6 + the 11 fields above, each preceded by its separating space. */ +#define ANSER_WIRE_HDR_LEN (6 + (1 + 1) + (1 + 1) + (1 + 10) + (1 + 10) \ + + (1 + 10) + (1 + 10) + (1 + 10) + (1 + 4) \ + + (1 + 4) + (1 + 10) + (1 + 8)) + +/* Offset of the 8-hex-digit CRC; the checksum covers everything before it. */ +#define ANSER_WIRE_CRC_OFFSET (ANSER_WIRE_HDR_LEN - 8) + +/* + * Largest key and body a header can describe. Checked before formatting so a + * field can never overflow its width and shift every field after it. + */ +#define ANSER_WIRE_MAX_KEYLEN 9999 +#define ANSER_WIRE_MAX_BODYLEN 999999999 + +/* + * The codec. + * + * Four functions, exported rather than static because together they *are* the + * protocol: the two directions have to agree byte for byte about offsets and + * about what the checksum covers, and that agreement is only auditable -- and + * only testable -- if both halves are reachable by name. sql/anser_test.sql + * drives them directly. + * + * They are implemented on the side that writes: AnserWireFormat in + * ansersideband.c (only a QE formats), AnserWireParse and AnserWireCheckCrc in + * anserdispatch.c (only the QD parses). + * + * AnserWireFormat returns a palloc'd NUL-terminated string. AnserWireParse + * fills 'out' and returns false for anything it will not vouch for; its + * contract and the checks it makes are documented at the definition. + * AnserWireCheckCrc takes the *decoded* body, or NULL/0 when there is none. + */ +typedef struct AnserWireMsg +{ + const char *wire; /* the message itself; the CRC covers its head */ + char kind; + char payload_type; /* resolved against the registry by the caller */ + AnserChannelKey key; + int key_len; + int part_index; + int total_parts; + int flags; + const char *body; /* base64, not NUL-terminated */ + int body_len; + uint32 crc; /* of the header, key and decoded body */ +} AnserWireMsg; + +extern PGDLLEXPORT char *AnserWireFormat(const AnserChannelKey *channel_key, char kind, + char payload_type, uint32 part_index, + uint32 total_parts, int flags, + const void *payload, Size payload_len); +extern PGDLLEXPORT bool AnserWireParse(const char *msg, AnserWireMsg *out); +extern PGDLLEXPORT bool AnserWireCheckCrc(const AnserWireMsg *msg, const void *body, + Size body_len); + +/* + * QD -> QE push: raw binary, written with pqPutInt/pqPutnchar. The payload + * travels in chunks, one message each, and the coordinator sends the next one + * only when the consumer asks with a NEXT -- so a delivery is never more than + * one whole chunk deep on the connection, and the reader can walk away between + * chunks without leaving a torn message behind. + * + * payload_type | crc32c | condition_id | session_id | command_count | + * flags | total_len | offset | keylen | key | chunk_len | chunk + * + * total_len is the full size of the merged payload and offset is where this + * chunk starts in it; a cancelled delivery is a single chunk with + * F_CANCELLED, total_len = 0 and offset = 0. The session and command ids are + * what lets a parked delivery be told apart from the current command's: + * condition_key restarts at the same value for every statement, so the + * consumer must match the whole identity, not the key alone. + * + * Each field is a 4-byte integer except the key and the chunk. The CRC covers + * all of the other fields in that order, the integers in network byte order, + * so it validates the routing information, the chunk's place in the payload, + * and the payload bytes themselves; the chunk is included only when the + * payload type says so. Both ends compute it through this one function so the + * two cannot drift apart. It is implemented in ansersideband.c, beside the + * reader that verifies it. + */ +extern PGDLLEXPORT pg_crc32c AnserWirePushCrc(char payload_type, uint32 condition_id, + uint32 session_id, uint32 command_count, + uint32 flags, uint32 total_len, uint32 offset, + const char *key, int keylen, + const void *body, int bodylen); + +/* + * QE side (ansersideband.c). + * + * AnserSidebandPublish is fire-and-forget: unlike the libpq transport it does + * not wait for the coordinator to acknowledge the part. 'payload_type' is + * stamped on the wire even when cancelled, since it is what tells the + * coordinator how to fold the channel's parts. + * + * AnserSidebandConsumeWait blocks on this backend's own dispatch socket until + * the merged payload arrives, the channel is cancelled, or timeout_ms elapses. + * On success *payload is palloc'd in the caller's context. A false return + * always means "run unfiltered", never an error -- including when what arrived + * is not of 'payload_type', which would mean two different kinds of information + * had collided on one channel. + */ +extern PGDLLEXPORT bool AnserSidebandPublish(const AnserChannelKey *channel_key, + char payload_type, + uint32 part_index, uint32 total_parts, + const void *payload, Size payload_len, + bool cancelled); +extern PGDLLEXPORT bool AnserSidebandConsumeWait(const AnserChannelKey *channel_key, + char payload_type, + void **payload, Size *payload_len, + bool *cancelled, long timeout_ms); + +/* + * QD side (anserdispatch.c). + * + * AnserDispatchNotifyHandler is installed as cdbdisp_notify_hook; it folds + * arriving parts and pushes the merged payload to subscribers. The Local + * variants serve producers and consumers running on the coordinator itself, + * which have no dispatch connection to themselves and so operate on the same + * per-query channel table directly. They take the same payload type for the + * same reasons, so that a coordinator-local producer and a segment producer are + * interchangeable on one channel. + */ +extern PGDLLEXPORT bool AnserDispatchNotifyHandler(struct CdbDispatchResult *dispatchResult, + struct pgNotify *notify); +extern PGDLLEXPORT bool AnserDispatchLocalPublish(const AnserChannelKey *channel_key, + char payload_type, + uint32 part_index, uint32 total_parts, + const void *payload, Size payload_len, + bool cancelled); +extern PGDLLEXPORT bool AnserDispatchLocalConsume(const AnserChannelKey *channel_key, + char payload_type, + void **payload, Size *payload_len, + bool *cancelled); + +/* + * Drop per-query state. AnserSidebandResetAll does both sides and is what + * the executor/transaction-end callbacks call; the halves are exposed because + * each lives with the state it owns. + */ +extern PGDLLEXPORT int64 AnserDispatchUnsubscribeCount(void); +extern PGDLLEXPORT int64 AnserDispatchDeliveriesCompleted(void); +extern PGDLLEXPORT void AnserDispatchFlushPending(long timeout_ms); +extern PGDLLEXPORT void AnserDispatchReset(void); +extern PGDLLEXPORT void AnserDispatchResetCommand(int command_count); +extern PGDLLEXPORT void AnserSidebandResetInbox(void); +extern PGDLLEXPORT void AnserSidebandResetAll(void); + +#endif /* ANSERSIDEBAND_H */ diff --git a/gpcontrib/anser/include/ansertlist.h b/gpcontrib/anser/include/ansertlist.h new file mode 100644 index 00000000000..0e7cc3352bc --- /dev/null +++ b/gpcontrib/anser/include/ansertlist.h @@ -0,0 +1,64 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * ansertlist.h + * Deparse expressions for post-planning pass-through filter scans. + * + *------------------------------------------------------------------------- + */ +#ifndef ANSERTLIST_H +#define ANSERTLIST_H + +#include "nodes/nodeFuncs.h" +#include "nodes/plannodes.h" +#include "parser/parsetree.h" + +/* INDEX_VAR in a child's output belongs to the child's deparse namespace. + * Resolve it before copying the expression into a new CustomScan namespace; + * otherwise stacked filters can make ruleutils resolve a Var to itself. + * Builders using this helper keep their custom_scan_tlist resolved, so one + * substitution also handles arbitrarily deep stacks and reordered outputs. */ +static inline Node * +anser_filter_tlist_mutator(Node *node, void *context) +{ + CustomScan *child = (CustomScan *) context; + + if (node == NULL) + return NULL; + if (IsA(node, Var) && ((Var *) node)->varno == INDEX_VAR) + { + TargetEntry *tle = get_tle_by_resno(child->custom_scan_tlist, + ((Var *) node)->varattno); + if (tle == NULL) + elog(ERROR, "missing child filter output column %d", ((Var *) node)->varattno); + return (Node *) copyObjectImpl(tle->expr); + } + return expression_tree_mutator(node, anser_filter_tlist_mutator, context); +} + +static inline List * +AnserFilterScanTlist(Plan *child) +{ + if (IsA(child, CustomScan)) + return (List *) expression_tree_mutator((Node *) child->targetlist, + anser_filter_tlist_mutator, child); + return (List *) copyObjectImpl(child->targetlist); +} + +#endif /* ANSERTLIST_H */ diff --git a/gpcontrib/anser/sql/anser_faults.sql b/gpcontrib/anser/sql/anser_faults.sql new file mode 100644 index 00000000000..4114b4b83d3 --- /dev/null +++ b/gpcontrib/anser/sql/anser_faults.sql @@ -0,0 +1,244 @@ +-- Anser runtime bloom filter: the cases that need gp_inject_fault. +-- +-- Kept apart from anser_runtime_filter so that a build configured with +-- --disable-debug-extensions loses only these. gp_inject_fault is built only +-- with the debug extensions, and the regression driver excludes any file that +-- calls one -- see faultinjector_prep in src/Makefile.global.in -- so whatever +-- shares a file with a fault is skipped along with it. Everything that can be +-- asked for with data alone therefore stays in the other file. +-- +-- Requires the cluster to run with anser.enable=on, like the rest of the suite. +-- +-- The extension may or may not already be there, and saying so is not part of +-- what is being tested; it is never dropped, for the same reason -- it can +-- predate this test, and other tests in the same database rely on it. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS gp_inject_fault; +-- end_ignore + +-- Deterministic plan shape: a hash join under the Postgres planner, as in +-- anser_runtime_filter. A fresh session inherits none of its settings. +SET enable_nestloop = off; +SET enable_mergejoin = off; +SET optimizer = off; + +-- Trace every exchange step into the server logs (never reaches the client), +-- so a CI run that goes wrong can be diagnosed from the log artifacts. +SET anser.debug = on; + +-- nextval guard: the QD sends two kinds of message down a QE's dispatch +-- connection -- the nextval reply and a sideband message -- and each has its +-- own reader. A reader that meets the other's message has to skip it whole: +-- taking the type byte and erroring leaves the body in the stream, and the +-- session then dies somewhere else with "protocol synchronization was lost". +-- +-- In the field this needs an Anser consumer to give up on a filter the +-- coordinator goes on to deliver anyway, which is a race and cannot be asked +-- for from SQL. The fault puts a sideband message in front of one nextval +-- reply instead, so the ordering is certain rather than likely; the +-- three-argument form is occurrence 1..1, so it fires once. +-- +-- Nothing here is Anser's -- the fault supplies the message and the fix is in +-- cdb_sequence_nextval_qe -- but Anser is why a second message type exists on +-- that connection at all, so the test lives with it. +-- +CREATE SEQUENCE anser_rf_seq; +CREATE TABLE anser_rf_seq_t (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_seq_t SELECT g FROM generate_series(1, 10) g; +SELECT gp_inject_fault('nextval_sideband_before_response', 'skip', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; +SELECT count(nextval('anser_rf_seq')) AS seq_rows FROM anser_rf_seq_t; +SELECT gp_inject_fault('nextval_sideband_before_response', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; +DROP SEQUENCE anser_rf_seq; +DROP TABLE anser_rf_seq_t; + +-- Delivery to a consumer that stopped reading must not block the coordinator. +-- +-- A merged filter is 1-64 MB and the dispatch connections are blocking, so +-- pushing to a QE that is no longer reading used to wait in pqWait() with no +-- deadline and no CHECK_FOR_INTERRUPTS -- inside the dispatcher loop, which +-- then served nobody. Neither cancel nor statement_timeout could end it. +-- +-- The ordering cannot be arranged with data: the channel completes when the +-- last part lands, which is normally while the consumers are still waiting. +-- Holding the delivery back is what lets every consumer reach its deadline +-- and move on to its probe side -- and it is while they are busy there that +-- the coordinator finally writes to all of them. +-- +-- Note this one hangs rather than fails if the fix is reverted, for the same +-- reason the bug was unkillable. It is here because the alternative is a +-- regression nobody notices until a cluster wedges. +SET anser.timeout_ms = 1; +CREATE TABLE anser_rf_push_build (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_rf_push_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_push_build SELECT g, 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_rf_push_probe SELECT g % 1000 + 1 FROM generate_series(1, 1000000) g; +ANALYZE anser_rf_push_build; +ANALYZE anser_rf_push_probe; + +-- One segment publishes a second late; every other consumer gives up at 1 ms. +SELECT gp_inject_fault('anser_before_deliver', 'sleep', '', '', '', 1, 1, 1, dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; +SET anser.runtime_filter = on; +SELECT count(*) AS push_rows + FROM anser_rf_push_probe p JOIN anser_rf_push_build b ON p.id = b.id; +SELECT gp_inject_fault('anser_before_deliver', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; +DROP TABLE anser_rf_push_build, anser_rf_push_probe; + +-- A consumer that runs out of time withdraws, so the coordinator stops +-- counting it as a subscriber. Without that it would keep delivering to a +-- process that has moved on -- which is what the push above has to survive, +-- and what this keeps from quietly regressing into. +-- +-- The withdrawal leaves no trace a query can see: the channel is gone by the +-- time the query returns, and a filter that merely arrived late looks the +-- same. So the coordinator counts them, and this reads the difference. +CREATE TABLE anser_rf_unsub_build (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_rf_unsub_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_unsub_build SELECT g, 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_rf_unsub_probe SELECT g % 1000 + 1 FROM generate_series(1, 200000) g; +ANALYZE anser_rf_unsub_build; +ANALYZE anser_rf_unsub_probe; +-- start_ignore +CREATE EXTENSION IF NOT EXISTS anser_test; +-- end_ignore +SELECT anser_test_unsubscribes() AS unsub_before \gset +SELECT gp_inject_fault('anser_before_deliver', 'sleep', '', '', '', 1, 1, 1, dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; +SELECT count(*) AS unsub_rows + FROM anser_rf_unsub_probe p JOIN anser_rf_unsub_build b ON p.id = b.id; +SELECT gp_inject_fault('anser_before_deliver', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; +SELECT anser_test_unsubscribes() > :unsub_before AS consumers_withdrew; +DROP EXTENSION anser_test; +DROP TABLE anser_rf_unsub_build, anser_rf_unsub_probe; + +-- A partially delivered filter must not wedge the next command. +-- +-- With chunked delivery there is no torn 64 MB push any more: the coordinator +-- sends one chunk and waits to be asked for the next. What can still be in +-- flight when every consumer has given up is exactly one chunk -- the QE is +-- busy on its probe side by then, so the chunk sits in the stream until the +-- command loop reads and skips it whole (postgres.c, GP_SIDEBAND_MESSAGE). +-- The next command on the same gang must answer. +SET anser.timeout_ms = 100; +SET anser.debug_min_filter_bytes = 67108864; +CREATE TABLE anser_rf_stale_build (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_rf_stale_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_stale_build SELECT g, 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_rf_stale_probe SELECT g % 1000 + 1 FROM generate_series(1, 1000000) g; +ANALYZE anser_rf_stale_build; +ANALYZE anser_rf_stale_probe; + +-- Hold the delivery for 1 s so every consumer gives up at 100 ms and the +-- first chunk goes out to connections nobody is reading. +SELECT gp_inject_fault('anser_before_deliver', 'sleep', '', '', '', 1, 1, 1, dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; +SET anser.runtime_filter = on; +SELECT count(*) AS stale_rows + FROM anser_rf_stale_probe p JOIN anser_rf_stale_build b ON p.id = b.id; +SELECT gp_inject_fault('anser_before_deliver', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + +-- The leftover chunk is skipped whole, so the next command on the same gang +-- must answer. +SELECT 'next command' AS marker, count(*) AS build_rows FROM anser_rf_stale_build; +DROP TABLE anser_rf_stale_build, anser_rf_stale_probe; +RESET anser.debug_min_filter_bytes; + +-- Walking away mid-delivery must end it cleanly. The fault pauses the +-- consumer after its first chunk for longer than its deadline -- as a skip +-- fault: the consumer itself waits out anser.timeout_ms plus a margin, on the +-- monotonic clock, because a 'sleep' fault is a bare nanosleep that any +-- signal cuts short. The consumer withdraws, the coordinator's delivery +-- stalls at the chunk boundary, and the one chunk in flight is discarded at +-- the door: the key is no longer subscribed, so read_one drops it without a +-- NEXT. Results must still be exact, the coordinator must record the +-- withdrawals, and the next command on the same gang must answer. +SET anser.timeout_ms = 500; +SET anser.debug_delivery_chunk_bytes = 8192; +SET anser.debug_min_filter_bytes = 67108864; +CREATE TABLE anser_rf_ab_build (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_rf_ab_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_ab_build SELECT g, 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_rf_ab_probe SELECT g % 1000 + 1 FROM generate_series(1, 200000) g; +ANALYZE anser_rf_ab_build; +ANALYZE anser_rf_ab_probe; + +-- start_ignore +CREATE EXTENSION IF NOT EXISTS anser_test; +-- end_ignore +SELECT anser_test_unsubscribes() AS unsub_before \gset +-- Pause every consumer after its first chunk for longer than its deadline. +SELECT gp_inject_fault('anser_after_chunk', 'skip', '', '', '', 1, 1, 0, dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content >= 0; +SET anser.runtime_filter = on; +SELECT count(*) AS abort_rows + FROM anser_rf_ab_probe p JOIN anser_rf_ab_build b ON p.id = b.id; +SELECT gp_inject_fault('anser_after_chunk', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content >= 0; +SELECT anser_test_unsubscribes() > :unsub_before AS consumers_withdrew; +SELECT 'next command' AS marker, count(*) AS build_rows FROM anser_rf_ab_build; +DROP EXTENSION anser_test; +DROP TABLE anser_rf_ab_build, anser_rf_ab_probe; +RESET anser.debug_min_filter_bytes; +RESET anser.debug_delivery_chunk_bytes; + +-- A filter from one statement must not be claimed by the next. +-- +-- A consumer that runs out of time withdraws, and with chunked delivery the +-- coordinator stops at the chunk boundary: the one chunk in flight is dropped +-- at the door, because the key is no longer subscribed. And even a filter +-- that somehow got parked whole could not be claimed by the next statement: +-- condition keys restart at the same values, but the session id and command +-- count stamped on the wire must match exactly. +-- +-- The LEFT JOIN pins j2 as the build side, so its consumer waits first: it +-- gives up at 700 ms while the coordinator sleeps for 1 s, its late first +-- chunk is then read -- and dropped -- while j1's consumer waits, and the +-- division errors. The second run re-executes the same plan shape with a +-- build side disjoint from the parked filter: claiming anything stale would +-- drop every joinable row, so count(j2.id) must be 1000, not 0. +SET anser.timeout_ms = 700; +CREATE TABLE anser_sf_b1 (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_sf_p1 (id int) DISTRIBUTED BY (id); +CREATE TABLE anser_sf_b2 (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_sf_p2 (id int) DISTRIBUTED BY (id); +INSERT INTO anser_sf_b1 SELECT g, 'b' || g FROM generate_series(1, 2000) g; +INSERT INTO anser_sf_p1 SELECT g FROM generate_series(1, 2000) g; +INSERT INTO anser_sf_b2 SELECT g, 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_sf_p2 SELECT g FROM generate_series(1, 2000) g; +ANALYZE anser_sf_b1; +ANALYZE anser_sf_p1; +ANALYZE anser_sf_b2; +ANALYZE anser_sf_p2; + +SELECT gp_inject_fault('anser_before_deliver', 'sleep', '', '', '', 1, 1, 1, dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; +SET anser.runtime_filter = on; +BEGIN; +SAVEPOINT anser_sf; +SELECT j1.id / (j1.id - j1.id) AS boom +FROM (SELECT p1.id FROM anser_sf_p1 p1 JOIN anser_sf_b1 b1 ON p1.id = b1.id) j1 +LEFT JOIN (SELECT p2.id FROM anser_sf_p2 p2 JOIN anser_sf_b2 b2 ON p2.id = b2.id) j2 + ON j1.id = j2.id; +ROLLBACK TO SAVEPOINT anser_sf; +DELETE FROM anser_sf_b2; +INSERT INTO anser_sf_b2 SELECT 1000 + g, 'b' || g FROM generate_series(1, 1000) g; +SELECT count(j2.id) AS reused_rows +FROM (SELECT p1.id FROM anser_sf_p1 p1 JOIN anser_sf_b1 b1 ON p1.id = b1.id) j1 +LEFT JOIN (SELECT p2.id FROM anser_sf_p2 p2 JOIN anser_sf_b2 b2 ON p2.id = b2.id) j2 + ON j1.id = j2.id; +COMMIT; +SELECT gp_inject_fault('anser_before_deliver', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; +DROP TABLE anser_sf_b1, anser_sf_p1, anser_sf_b2, anser_sf_p2; + +RESET anser.runtime_filter; +RESET anser.debug; +RESET anser.timeout_ms; +RESET optimizer; +RESET enable_nestloop; +RESET enable_mergejoin; diff --git a/gpcontrib/anser/sql/anser_multijoin.sql b/gpcontrib/anser/sql/anser_multijoin.sql new file mode 100644 index 00000000000..fcfda3e550c --- /dev/null +++ b/gpcontrib/anser/sql/anser_multijoin.sql @@ -0,0 +1,258 @@ +-- Anser runtime bloom filter: chains of 2..10 joins. +-- +-- Each table joins to the next on its own key pair -- t1.nxt = t2.own, then +-- t2.nxt = t3.own, and so on -- which is what a real chain looks like: no two +-- joins share a key, so no table's distribution can serve more than one of +-- them and every join needs a motion of its own. Joining a whole chain on one +-- column would make the joins co-located and would test the easy case only. +-- +-- This is also the shape the feature has to handle: the build side of the third +-- join is the result of the first two, and the key column for that join comes +-- from t2, so it reaches its base relation through a join rather than straight +-- through a Hash. +-- +-- What these cases assert is not that a filter was injected -- that is the +-- planner's choice and it differs between the two optimizers and with the +-- cluster width -- but that the answer is the same either way. A bloom filter +-- fails asymmetrically: a bit that should be set and is not silently removes +-- joinable rows, so "same rows with the filter on as with it off" is the +-- property worth asserting. Plan shapes are deliberately not asserted; an +-- expected file full of them would break on a three-segment cluster and pass on +-- a one-segment one without saying anything about correctness. +-- +-- Every query below reports its violations, so the expected output is empty +-- results and zeros. A failure names the chain that broke rather than leaving +-- a table of numbers to be compared by eye. + +SET enable_nestloop = off; +SET enable_mergejoin = off; + +-- A regress test must not inherit the production deadline. anser.timeout_ms +-- defaults to 10 s, which is a budget for an exchange that has gone wrong; a +-- test that waits it out has turned a delivery failure into a slow pass. +-- Pinned shorter here, so a filter that never arrives fails the run instead of +-- hiding in the clock. +SET anser.timeout_ms = 5000; + +-- own is what the previous table joins to, nxt is what this table joins to the +-- next one with. They hold the same value, which keeps the chain 1:1 and the +-- answer predictable -- 1000 rows summing to 500500, whatever the join count -- +-- while still being two different columns as far as the planner is concerned. +-- +-- The distribution key is deliberately never the whole story: a table +-- distributed by own is joined on nxt, and the ones distributed by payload are +-- joined on both. Every join therefore redistributes or broadcasts one side, +-- and the producer ends up in a different slice from its consumer. +-- +-- t1 is the smallest and every later table contains it, so the chain returns +-- t1 whatever order the planner picks. The widening sizes give it a reason to +-- hash the joined result rather than the next table, which is the shape that +-- exercises the descent through a join. +CREATE TABLE anser_mj_t1 (own int, nxt int, payload text) DISTRIBUTED BY (own); +CREATE TABLE anser_mj_t2 (own int, nxt int, payload text) DISTRIBUTED BY (payload); +CREATE TABLE anser_mj_t3 (own int, nxt int, payload text) DISTRIBUTED BY (own); +CREATE TABLE anser_mj_t4 (own int, nxt int, payload text) DISTRIBUTED BY (payload); +CREATE TABLE anser_mj_t5 (own int, nxt int, payload text) DISTRIBUTED BY (own); +CREATE TABLE anser_mj_t6 (own int, nxt int, payload text) DISTRIBUTED BY (payload); +CREATE TABLE anser_mj_t7 (own int, nxt int, payload text) DISTRIBUTED BY (own); +CREATE TABLE anser_mj_t8 (own int, nxt int, payload text) DISTRIBUTED BY (payload); +CREATE TABLE anser_mj_t9 (own int, nxt int, payload text) DISTRIBUTED BY (own); +CREATE TABLE anser_mj_t10 (own int, nxt int, payload text) DISTRIBUTED BY (payload); +CREATE TABLE anser_mj_t11 (own int, nxt int, payload text) DISTRIBUTED BY (own); + +INSERT INTO anser_mj_t1 SELECT g, g, 't1_' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_mj_t2 SELECT g, g, 't2_' || g FROM generate_series(1, 2000) g; +INSERT INTO anser_mj_t3 SELECT g, g, 't3_' || g FROM generate_series(1, 3000) g; +INSERT INTO anser_mj_t4 SELECT g, g, 't4_' || g FROM generate_series(1, 4000) g; +INSERT INTO anser_mj_t5 SELECT g, g, 't5_' || g FROM generate_series(1, 5000) g; +INSERT INTO anser_mj_t6 SELECT g, g, 't6_' || g FROM generate_series(1, 6000) g; +INSERT INTO anser_mj_t7 SELECT g, g, 't7_' || g FROM generate_series(1, 7000) g; +INSERT INTO anser_mj_t8 SELECT g, g, 't8_' || g FROM generate_series(1, 8000) g; +INSERT INTO anser_mj_t9 SELECT g, g, 't9_' || g FROM generate_series(1, 9000) g; +INSERT INTO anser_mj_t10 SELECT g, g, 't10_' || g FROM generate_series(1, 10000) g; +INSERT INTO anser_mj_t11 SELECT g, g, 't11_' || g FROM generate_series(1, 11000) g; + +ANALYZE anser_mj_t1; +ANALYZE anser_mj_t2; +ANALYZE anser_mj_t3; +ANALYZE anser_mj_t4; +ANALYZE anser_mj_t5; +ANALYZE anser_mj_t6; +ANALYZE anser_mj_t7; +ANALYZE anser_mj_t8; +ANALYZE anser_mj_t9; +ANALYZE anser_mj_t10; +ANALYZE anser_mj_t11; + +-- The same shape, minus every tenth key. The outer-join case below needs a +-- right side that does not cover its left, and none of the tables above can be +-- it: they are joined 1:1 on purpose, and thinning one would break the "1000 +-- rows summing to 500500 whatever the join count" invariant the chains rest on. +CREATE TABLE anser_mj_gap (own int, nxt int, payload text) DISTRIBUTED BY (own); +INSERT INTO anser_mj_gap SELECT g, g, 'gap_' || g + FROM generate_series(1, 3000) g WHERE g % 10 <> 0; +ANALYZE anser_mj_gap; + +-- Runs one chain twice, with the filter on and off, and reports whether the two +-- agree. The checksum is what makes the comparison mean something: equal row +-- counts would also come out of two different sets of rows, and a filter that +-- wrongly rejects is exactly the failure that changes which rows come back. +CREATE FUNCTION anser_mj_check(njoins int) +RETURNS TABLE (joins int, nrows bigint, own_sum bigint, agrees boolean) +LANGUAGE plpgsql AS $$ +DECLARE + sql text; + i int; + on_rows bigint; + on_sum bigint; + off_rows bigint; + off_sum bigint; +BEGIN + sql := 'SELECT count(*), coalesce(sum(anser_mj_t1.own), 0) FROM anser_mj_t1'; + FOR i IN 2 .. njoins + 1 LOOP + sql := sql || format(' JOIN anser_mj_t%s ON anser_mj_t%s.nxt = anser_mj_t%s.own', + i, i - 1, i); + END LOOP; + PERFORM set_config('anser.runtime_filter', 'on', false); + EXECUTE sql INTO on_rows, on_sum; + PERFORM set_config('anser.runtime_filter', 'off', false); + EXECUTE sql INTO off_rows, off_sum; + RETURN QUERY SELECT njoins, on_rows, on_sum, + (on_rows = off_rows AND on_sum = off_sum); +END; +$$; + +-- 2 through 10 joins, Postgres planner. Expected: no rows. +SET optimizer = off; +SELECT joins, nrows, own_sum, agrees + FROM generate_series(2, 10) g, anser_mj_check(g) + WHERE NOT agrees OR nrows <> 1000 OR own_sum <> 500500 + ORDER BY joins; + +-- The same under ORCA, which picks different join orders and slice widths. +SET optimizer = on; +SELECT joins, nrows, own_sum, agrees + FROM generate_series(2, 10) g, anser_mj_check(g) + WHERE NOT agrees OR nrows <> 1000 OR own_sum <> 500500 + ORDER BY joins; + +SET optimizer = off; + +-- A chain whose middle join is an outer join, and whose next join reads the +-- nullable side's key -- so the joined result carries NULLs the base relation +-- does not. +-- +-- Two things make that true of the plan and not just of the text. The right +-- side has to leave keys unmatched, which is what anser_mj_gap is for. And the +-- outer join has to survive planning: the join above it is strict in c.nxt, so +-- reduce_outer_joins() would otherwise turn this LEFT JOIN into an inner one +-- before any path is considered. OFFSET 0 keeps the subquery from being pulled +-- up, which keeps that qual a level away. +-- +-- No filter is injected here, and none can be. The price of keeping the outer +-- join is a SubqueryScan, and AnserResolveKeyScan descends through Hash, Motion +-- and HashJoin only. Nor is there a way around it: a NULL-keyed row reaches +-- the output only under an outer join, an outer join survives only behind a +-- subquery boundary, and Anser does not inject across one. So a filter meeting +-- a NULL key is not something this suite can arrange -- what is checked here is +-- that such a plan returns the same rows either way. +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_mj_lj_on AS + SELECT s.aown, s.cown, d.own AS down + FROM (SELECT a.own AS aown, c.own AS cown, c.nxt AS cnxt + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + LEFT JOIN anser_mj_gap c ON b.nxt = c.own + OFFSET 0) s + JOIN anser_mj_t4 d ON s.cnxt = d.own + DISTRIBUTED BY (aown); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_mj_lj_off AS + SELECT s.aown, s.cown, d.own AS down + FROM (SELECT a.own AS aown, c.own AS cown, c.nxt AS cnxt + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + LEFT JOIN anser_mj_gap c ON b.nxt = c.own + OFFSET 0) s + JOIN anser_mj_t4 d ON s.cnxt = d.own + DISTRIBUTED BY (aown); + +SELECT count(*) AS lj_rows FROM anser_mj_lj_on; +SELECT count(*) AS lj_only_on + FROM (SELECT * FROM anser_mj_lj_on EXCEPT ALL SELECT * FROM anser_mj_lj_off) d; +SELECT count(*) AS lj_only_off + FROM (SELECT * FROM anser_mj_lj_off EXCEPT ALL SELECT * FROM anser_mj_lj_on) d; + +-- A star rather than a chain: every join reads t1's key, so one table's +-- distribution serves them all and the build sides are base scans throughout. +-- This is the shape that worked before the descent went through joins, and it +-- has to keep working. +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_mj_star_on AS + SELECT a.own AS aown, e.payload AS epayload + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + JOIN anser_mj_t3 c ON a.nxt = c.own + JOIN anser_mj_t4 d ON a.nxt = d.own + JOIN anser_mj_t5 e ON a.nxt = e.own + DISTRIBUTED BY (aown); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_mj_star_off AS + SELECT a.own AS aown, e.payload AS epayload + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + JOIN anser_mj_t3 c ON a.nxt = c.own + JOIN anser_mj_t4 d ON a.nxt = d.own + JOIN anser_mj_t5 e ON a.nxt = e.own + DISTRIBUTED BY (aown); + +SELECT count(*) AS star_rows FROM anser_mj_star_on; +SELECT count(*) AS star_only_on + FROM (SELECT * FROM anser_mj_star_on EXCEPT ALL SELECT * FROM anser_mj_star_off) d; +SELECT count(*) AS star_only_off + FROM (SELECT * FROM anser_mj_star_off EXCEPT ALL SELECT * FROM anser_mj_star_on) d; + +-- A chain that returns nothing. A filter that wrongly accepts is only slow, +-- never wrong, so this is the one direction an empty result can still check. +SET anser.runtime_filter = on; +SELECT count(*) AS empty_rows + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + JOIN anser_mj_t3 c ON b.nxt = c.own + WHERE a.own < 0; + +-- The chains above assert that the filter changes no answer. That also holds +-- when the filter never arrives and every consumer fails open, which is how a +-- broken exchange passes a green test. This asserts the other half: a chain +-- whose third table is three times the size of the first has rows to prune, so +-- a filter that arrived removes some. +CREATE FUNCTION anser_mj_pruned(q text) RETURNS bigint +LANGUAGE plpgsql AS $$ +DECLARE + line text; + removed bigint := 0; +BEGIN + FOR line IN EXECUTE 'EXPLAIN (ANALYZE, TIMING OFF, COSTS OFF) ' || q LOOP + IF line ~ 'Rows Removed by (Bloom Filter|Pushdown Runtime Filter): ' THEN + removed := removed + coalesce(substring(line from ': *([0-9]+)')::bigint, 0); + END IF; + END LOOP; + RETURN removed; +END; +$$; +SET anser.runtime_filter = on; +SELECT 'the chain pruned nothing: no filter arrived, or none was injected' AS problem + WHERE anser_mj_pruned('SELECT count(*) FROM anser_mj_t1 a JOIN anser_mj_t2 b ON a.nxt = b.own JOIN anser_mj_t3 c ON b.nxt = c.own') = 0; + +DROP FUNCTION anser_mj_pruned(text); + +DROP FUNCTION anser_mj_check(int); +DROP TABLE anser_mj_t1, anser_mj_t2, anser_mj_t3, anser_mj_t4, anser_mj_t5, + anser_mj_t6, anser_mj_t7, anser_mj_t8, anser_mj_t9, anser_mj_t10, + anser_mj_t11, anser_mj_gap; + +RESET anser.runtime_filter; +RESET anser.timeout_ms; +RESET optimizer; +RESET enable_nestloop; +RESET enable_mergejoin; diff --git a/gpcontrib/anser/sql/anser_runtime_filter.sql b/gpcontrib/anser/sql/anser_runtime_filter.sql new file mode 100644 index 00000000000..88633aedb3b --- /dev/null +++ b/gpcontrib/anser/sql/anser_runtime_filter.sql @@ -0,0 +1,787 @@ +-- Anser runtime bloom filter: plan-tree integration (PR4). +-- +-- Requires the cluster to run with anser.enable=on so the gather/send +-- services are live. On a multi-segment cluster the build side redistributes, +-- exercising the Motion-directly-under-CustomScan case; on a single segment it +-- degrades to the leaf case. Either way the feature must (a) inject the +-- producer/consumer nodes and (b) never change query results. + + +-- Deterministic plan shape: force a hash join. +SET enable_nestloop = off; +SET enable_mergejoin = off; + +-- A regress test must not inherit the production deadline. anser.timeout_ms +-- defaults to 10 s, which is a budget for an exchange that has gone wrong; a +-- test that waits it out has turned a delivery failure into a slow pass. +-- Pinned shorter here, so a filter that never arrives fails the run instead of +-- hiding in the clock. +SET anser.timeout_ms = 5000; + +-- Trace every exchange step into the server logs for the whole file. LOG +-- level never reaches the client, so the expected output is unaffected; the +-- traces are for the CI logs when a run goes wrong. +SET anser.debug = on; + +-- build is the smaller (hashed / preserved) side, distributed so the join key +-- must be redistributed; probe is the larger side distributed by the join key. +CREATE TABLE anser_rf_build (id int, name text) DISTRIBUTED BY (name); +CREATE TABLE anser_rf_probe (id int, payload text) DISTRIBUTED BY (id); +INSERT INTO anser_rf_build SELECT g, 'b' || g FROM generate_series(1, 200) g; +INSERT INTO anser_rf_probe SELECT g, 'p' || g FROM generate_series(1, 2000) g; +ANALYZE anser_rf_build; +ANALYZE anser_rf_probe; + +-- Use the Postgres planner for a deterministic plan shape. +SET optimizer = off; + +-- With the filter on the plan carries an "Anser Bloom Producer" under the Hash +-- and an "Anser Bloom Consumer" above the probe scan (with the planned bloom +-- size); with it off neither node appears. (COSTS OFF keeps the output stable.) +SET anser.runtime_filter = on; +EXPLAIN (COSTS OFF) +SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; + +SET anser.runtime_filter = off; +EXPLAIN (COSTS OFF) +SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; + +-- Correctness: identical results with the filter on vs off (Postgres planner). +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_r_on AS + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + DISTRIBUTED BY (name); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_r_off AS + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + DISTRIBUTED BY (name); + +SELECT count(*) AS rows_on FROM anser_rf_r_on; +SELECT count(*) AS rows_off FROM anser_rf_r_off; +SELECT count(*) AS only_on + FROM (SELECT * FROM anser_rf_r_on EXCEPT ALL SELECT * FROM anser_rf_r_off) d; +SELECT count(*) AS only_off + FROM (SELECT * FROM anser_rf_r_off EXCEPT ALL SELECT * FROM anser_rf_r_on) d; + +-- Correctness must also hold under ORCA (whether or not it injects the nodes). +SET optimizer = on; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_r_orca AS + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + DISTRIBUTED BY (name); +SELECT count(*) AS only_orca + FROM (SELECT * FROM anser_rf_r_orca EXCEPT ALL SELECT * FROM anser_rf_r_off) d; +SELECT count(*) AS only_off_orca + FROM (SELECT * FROM anser_rf_r_off EXCEPT ALL SELECT * FROM anser_rf_r_orca) d; + +-- Pushdown mix: with gp_enable_runtime_filter_pushdown on, the consumer hands +-- the unioned filter to the probe SeqScan as an SK_BLOOM_FILTER scan key and +-- the scan (or the table AM) does the pruning. Results must still match the +-- filter-off run. +SET optimizer = off; +SET gp_enable_runtime_filter_pushdown = on; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_r_push AS + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + DISTRIBUTED BY (name); +SELECT count(*) AS only_push + FROM (SELECT * FROM anser_rf_r_push EXCEPT ALL SELECT * FROM anser_rf_r_off) d; +SELECT count(*) AS only_off_push + FROM (SELECT * FROM anser_rf_r_off EXCEPT ALL SELECT * FROM anser_rf_r_push) d; +RESET gp_enable_runtime_filter_pushdown; + +-- Counts the Anser nodes a plan carries. Comparing a filtered count with an +-- unfiltered one says nothing when no filter was injected: both runs then do +-- the same thing and agree. Several blocks below ask this as well, which is +-- why it is defined here rather than beside its first caller. +CREATE FUNCTION anser_rf_nodes(q text) RETURNS bigint +LANGUAGE plpgsql AS $$ +DECLARE + line text; + nodes bigint := 0; +BEGIN + FOR line IN EXECUTE 'EXPLAIN (COSTS OFF) ' || q LOOP + IF line ~ 'Anser Bloom' THEN + nodes := nodes + 1; + END IF; + END LOOP; + RETURN nodes; +END; +$$; + +-- Pushdown where the key's table attribute number and its position in the +-- scan's output targetlist disagree. The case above cannot see the +-- difference: anser_rf_probe's key is its first column and also the first +-- thing the scan emits, so the two numbers coincide. Here the key is the +-- probe table's *second* column and its *only* projected one, so a scan key +-- carrying the output position makes the scan test `payload` against a filter +-- built on `id` -- and since no payload is ever a build key, every row is +-- discarded. Both counts must be 200. +SET optimizer = on; +CREATE TABLE anser_rf_proj_build (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_rf_proj_probe (payload int, id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_proj_build SELECT g, 'b' || g FROM generate_series(1, 200) g; +INSERT INTO anser_rf_proj_probe SELECT -g, g FROM generate_series(1, 2000) g; +ANALYZE anser_rf_proj_build; +ANALYZE anser_rf_proj_probe; +SET gp_enable_runtime_filter_pushdown = on; +SET anser.runtime_filter = off; +SELECT count(p.id) AS proj_push_off + FROM anser_rf_proj_build b LEFT JOIN anser_rf_proj_probe p ON b.id = p.id; +SET anser.runtime_filter = on; +SELECT count(p.id) AS proj_push_on + FROM anser_rf_proj_build b LEFT JOIN anser_rf_proj_probe p ON b.id = p.id; +SELECT anser_rf_nodes('SELECT count(p.id) + FROM anser_rf_proj_build b LEFT JOIN anser_rf_proj_probe p ON b.id = p.id') > 0 AS proj_injected; + +-- The alias is not the column. This subquery renames `payload` to the name +-- `id` and the real `id` to `id2`, and joins on id2 -- so resolving the key by +-- name would land on `payload`, the one column guaranteed never to match a +-- filter built from the build side. Resolution runs position -> TargetEntry +-- -> Var -> varattno and never reads a name, so the rename is invisible to it. +-- Both counts must be 200. +SET anser.runtime_filter = off; +SELECT count(p.id2) AS rename_push_off + FROM anser_rf_proj_build b + LEFT JOIN (SELECT payload AS id, id AS id2 FROM anser_rf_proj_probe) p + ON b.id = p.id2; +SET anser.runtime_filter = on; +SELECT count(p.id2) AS rename_push_on + FROM anser_rf_proj_build b + LEFT JOIN (SELECT payload AS id, id AS id2 FROM anser_rf_proj_probe) p + ON b.id = p.id2; +SELECT anser_rf_nodes('SELECT count(p.id2) + FROM anser_rf_proj_build b + LEFT JOIN (SELECT payload AS id, id AS id2 FROM anser_rf_proj_probe) p + ON b.id = p.id2') > 0 AS rename_injected; + +-- A computed key, which Anser declines outright. The subquery has no ORDER BY, +-- LIMIT or OFFSET, so is_simple_subquery() pulls it up and the hash clause +-- becomes `b.id = p.id * 2`; anser_hashjoin_keys takes two plain Vars and +-- nothing else, so no nodes are injected at all. +-- +-- The comment here used to claim the opposite -- that the clause still reads as +-- a Var into the scan's output and only the pushdown is declined. That would +-- need the subquery to survive as a scan of its own, which asking for it +-- (OFFSET 0) does not buy either: AnserResolveKeyScan does not descend through +-- a SubqueryScan. The decision for a computed column is covered directly by +-- anser_test_pushdown_accepts; what is left to check here is that declining is +-- clean, so expr_injected is f and both counts are 100 -- only the 100 even +-- build ids match. +SET anser.runtime_filter = off; +SELECT count(p.k) AS expr_push_off + FROM anser_rf_proj_build b + LEFT JOIN (SELECT id * 2 AS k FROM anser_rf_proj_probe) p ON b.id = p.k; +SET anser.runtime_filter = on; +SELECT count(p.k) AS expr_push_on + FROM anser_rf_proj_build b + LEFT JOIN (SELECT id * 2 AS k FROM anser_rf_proj_probe) p ON b.id = p.k; +SELECT anser_rf_nodes('SELECT count(p.k) + FROM anser_rf_proj_build b + LEFT JOIN (SELECT id * 2 AS k FROM anser_rf_proj_probe) p ON b.id = p.k') > 0 AS expr_injected; +RESET gp_enable_runtime_filter_pushdown; +DROP TABLE anser_rf_proj_build, anser_rf_proj_probe; +SET optimizer = off; + + +-- Everything above checks that the nodes were injected and that the results did +-- not change. Both of those also hold when the filter never arrives and every +-- consumer fails open -- which is how a broken exchange passes a green test. +-- This is the assertion that the filter was used: the probe side holds 2000 +-- rows of which 200 join, so a filter that arrived removes the rest. Only the +-- Postgres planner is asserted, because its plan shape here is fixed; ORCA +-- chooses and the count would not be a stable thing to require. +CREATE FUNCTION anser_rf_pruned(q text) RETURNS bigint +LANGUAGE plpgsql AS $$ +DECLARE + line text; + removed bigint := 0; +BEGIN + FOR line IN EXECUTE 'EXPLAIN (ANALYZE, TIMING OFF, COSTS OFF) ' || q LOOP + IF line ~ 'Rows Removed by (Bloom Filter|Pushdown Runtime Filter): ' THEN + removed := removed + coalesce(substring(line from ': *([0-9]+)')::bigint, 0); + END IF; + END LOOP; + RETURN removed; +END; +$$; +SET optimizer = off; +SET anser.runtime_filter = on; +SELECT 'the filter pruned nothing: it never arrived, or it arrived empty' AS problem + WHERE anser_rf_pruned('SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id') = 0; +-- Nested statement guard: a statement run through SPI from a function body +-- goes through its whole Start/Run/End inside the *outer* query's Run, so the +-- inner ExecutorEnd fires while the outer query is still executing. Dropping +-- channels there takes the running query's filters with it, and every consumer +-- that has not read yet fails open -- the exchange still happens, the filter is +-- just never used. +-- +-- This needs an inner join, not the left join above: the consumer defers its +-- first tuple only for INNER (see defer_first), which is what lets the join +-- emit a row -- and so evaluate the function -- before the consumer reads. +CREATE FUNCTION anser_rf_nested(x int) RETURNS int +LANGUAGE plpgsql VOLATILE AS $$ +BEGIN + PERFORM 1; + RETURN x; +END; +$$; +SELECT 'an inner statement dropped the running query''s filter' AS problem + WHERE anser_rf_pruned('SELECT anser_rf_nested(p.id) FROM anser_rf_build b + JOIN anser_rf_probe p ON b.id = p.id') = 0; + +-- Narrow build slices must still publish. part_index is only diagnostic, so +-- a slice narrower than the cluster must not reject the only producer there +-- is: direct dispatch to a non-zero segment (segindex >= total_parts) or the +-- entry-DB (segindex = -1) both used to leave the channel a part short and +-- every consumer waiting out anser.timeout_ms. The block also asserts the +-- count, but the load-bearing assertion is that rows were pruned at all. +SET anser.timeout_ms = 100; +CREATE TABLE anser_rf_dd_build (id int, dk text, pad text) DISTRIBUTED BY (dk); +CREATE TABLE anser_rf_dd_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_dd_build SELECT g, 'k' || (g % 6), 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_rf_dd_probe SELECT g % 1000 + 1 FROM generate_series(1, 100000) g; +ANALYZE anser_rf_dd_build; +ANALYZE anser_rf_dd_probe; + +-- Check every distribution key, not a chosen one: on any cluster with more +-- than one segment at least one of them lands with segindex >= 1, which is +-- the shape that used to stall -- the direct-dispatched producer was refused +-- (segindex >= total_parts) and every consumer waited out anser.timeout_ms. +-- The discriminating observable is that rows were pruned at all; result +-- counts are identical either way, since a broken exchange fails open. +SET anser.runtime_filter = on; +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k0''') > 0 AS k0_pruned; +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k1''') > 0 AS k1_pruned; +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k2''') > 0 AS k2_pruned; +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k3''') > 0 AS k3_pruned; +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k4''') > 0 AS k4_pruned; +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k5''') > 0 AS k5_pruned; + +-- Entry-DB build side: a catalog scan runs on the entry-DB QE, whose +-- segindex is -1 -- 0xFFFFFFFF as a part index, the same rejection. +CREATE TABLE anser_rf_eb_probe (o oid) DISTRIBUTED BY (o); +INSERT INTO anser_rf_eb_probe + SELECT 'anser_rf_eb_probe'::regclass::oid FROM generate_series(1, 10) g + UNION ALL + SELECT 4000000000 + g FROM generate_series(1, 100000) g; +ANALYZE anser_rf_eb_probe; +-- The count uses only the matching rows, so it is exact: the table's own oid +-- exists in pg_class, ten times. +SELECT count(*) AS eb_rows + FROM (SELECT 'anser_rf_eb_probe'::regclass::oid AS o FROM generate_series(1, 10) g) p + JOIN pg_class c ON p.o = c.oid; +SELECT 'entry-DB producer did not publish (consumer failed open)' AS problem + WHERE anser_rf_pruned('SELECT count(*) FROM anser_rf_eb_probe p JOIN pg_class c ON p.o = c.oid') = 0; +DROP TABLE anser_rf_dd_build, anser_rf_dd_probe, anser_rf_eb_probe; +RESET anser.timeout_ms; + +DROP FUNCTION anser_rf_nested(int); +DROP FUNCTION anser_rf_pruned(text); + +-- Filter sizing follows the estimate, so exercising a production-size payload +-- with test-size data goes through a debug knob instead: force a 64 MB bitset +-- for a 200-row build side, and the full-size filter has to actually exchange +-- (the serialized part rides the wire to every consumer). +SET anser.debug_min_filter_bytes = 67108864; +SET anser.runtime_filter = on; +EXPLAIN (COSTS OFF) +SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; + +-- A plain EXPLAIN must not allocate the filter: the coordinator initializes +-- every slice with EXEC_FLAG_EXPLAIN_ONLY, so the producer must skip building +-- the bitset (state=NULL) while the plan still shows the planned size. The +-- debug log makes the decision observable. anser.debug is already on for the +-- whole file; client_min_messages is what brings the line to the client here. +SET client_min_messages = log; +EXPLAIN (COSTS OFF) +SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; +RESET client_min_messages; + +-- And the filter must still be correct: 200 build rows, each matching one +-- probe row. +SELECT count(*) AS rows_joined + FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; + +-- The floor must change the real filter, not just the plan: sizing raises the +-- declared element count to match the forced bitset size, so producer and +-- consumer really build a 64 MB filter instead of displaying one. +CREATE EXTENSION IF NOT EXISTS anser_test; +SELECT injected, total_elems, planned_bytes FROM anser_test_rf_size(200); +DROP EXTENSION anser_test; + +-- The same exchange in chunks: an 8 kB chunk turns the 64 MB filter into +-- thousands of NEXT round trips. The result must match the unfiltered run and +-- the query must simply finish -- a whole-filter push that outgrew the +-- connection buffers is exactly what used to wedge the session. Matching +-- results alone would not prove it: a consumer that gives up produces the +-- same rows, so the coordinator's counters must say every consumer was +-- served and none withdrew. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS anser_test; +-- end_ignore +SELECT anser_test_deliveries_completed() AS d_before \gset +SELECT anser_test_unsubscribes() AS u_before \gset +SET anser.debug_delivery_chunk_bytes = 8192; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_r_chunk AS + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + DISTRIBUTED BY (name); +SELECT anser_test_deliveries_completed() = :d_before + 3 AS all_delivered; +SELECT anser_test_unsubscribes() = :u_before AS nobody_gave_up; +SELECT count(*) AS only_chunk + FROM (SELECT * FROM anser_rf_r_chunk EXCEPT ALL SELECT * FROM anser_rf_r_off) d; +SELECT count(*) AS only_off_chunk + FROM (SELECT * FROM anser_rf_r_off EXCEPT ALL SELECT * FROM anser_rf_r_chunk) d; +DROP TABLE anser_rf_r_chunk; +DROP EXTENSION anser_test; +RESET anser.debug_delivery_chunk_bytes; + +-- A LIMIT can finish the query while a consumer is still waiting for its +-- filter: the producer is squelched before it publishes, the consumer must +-- bail out via QueryFinishPending and withdraw its subscription, and the +-- result must match the unfiltered run. +SELECT count(*) AS limited_rows FROM ( + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + LIMIT 10) q; + +-- An open cursor must be undisturbed by statements between its FETCHes: the +-- filter exchange completes inside the FETCH's ExecutorRun window, so a +-- sibling statement's ExecutorEnd sweep has nothing of the cursor's to wipe. +-- The sibling runs the same filtered join, so it really does create and sweep +-- its own channels while the cursor's gang sits suspended. The delivery +-- counter must also say every consumer was served -- because "came back +-- unfiltered" returns the same rows and would hide a broken exchange. (The +-- sweep's failure mode itself is a wait, not a wrong answer.) The cursor +-- query filters the build side to ids 1..3, and on a segment none of them +-- land on the hash is empty, the join skips the probe side, and that +-- consumer never runs -- so its count comes from the data, not from the +-- cluster width. The sibling has no such filter, so it serves all three. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS anser_test; +-- end_ignore +SELECT anser_test_deliveries_completed() AS d_before \gset +SELECT count(DISTINCT gp_segment_id) AS cursor_consumers + FROM anser_rf_probe WHERE id <= 3 \gset +SET optimizer = off; +SET anser.runtime_filter = on; +BEGIN; +DECLARE anser_rf_cur CURSOR FOR + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + WHERE b.id <= 3 ORDER BY b.name; +FETCH 1 anser_rf_cur; +-- The ExecutorEnd of this statement must leave the open cursor's state alone. +SELECT count(*) AS sibling_stmt FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; +FETCH ALL anser_rf_cur; +COMMIT; +SELECT anser_test_deliveries_completed() + = :d_before + 3 + :cursor_consumers AS cursor_delivered; +DROP EXTENSION anser_test; + +DROP TABLE anser_rf_r_push; +DROP TABLE anser_rf_r_on, anser_rf_r_off, anser_rf_r_orca; +DROP TABLE anser_rf_build, anser_rf_probe; + +-- Datatype guard: producer and consumer hash the raw Datum bytes, so injection +-- is restricted to keys where SQL equality is bitwise Datum equality. A +-- cross-type join (float4 vs float8 Datums for equal values differ) must not +-- be injected; results must match the filter-off run either way. +CREATE TABLE anser_rf_build_f (id float8, name text) DISTRIBUTED BY (name); +CREATE TABLE anser_rf_probe_f (id float4, payload text) DISTRIBUTED BY (id); +INSERT INTO anser_rf_build_f SELECT g, 'b' || g FROM generate_series(1, 200) g; +INSERT INTO anser_rf_probe_f SELECT g, 'p' || g FROM generate_series(1, 2000) g; +ANALYZE anser_rf_build_f; +ANALYZE anser_rf_probe_f; + +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_f_on AS + SELECT b.name, p.payload FROM anser_rf_build_f b JOIN anser_rf_probe_f p ON b.id = p.id + DISTRIBUTED BY (name); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_f_off AS + SELECT b.name, p.payload FROM anser_rf_build_f b JOIN anser_rf_probe_f p ON b.id = p.id + DISTRIBUTED BY (name); + +SELECT count(*) AS rows_on FROM anser_rf_f_on; +SELECT count(*) AS only_on + FROM (SELECT * FROM anser_rf_f_on EXCEPT ALL SELECT * FROM anser_rf_f_off) d; +SELECT count(*) AS only_off + FROM (SELECT * FROM anser_rf_f_off EXCEPT ALL SELECT * FROM anser_rf_f_on) d; + +DROP TABLE anser_rf_f_on, anser_rf_f_off, anser_rf_build_f, anser_rf_probe_f; + +-- Same-typed float keys are also excluded: -0.0 and 0.0 compare equal in SQL +-- but are not bitwise equal, so hashing raw Datums could prune a joinable row. +CREATE TABLE anser_rf_build_z (id float8, name text) DISTRIBUTED BY (name); +CREATE TABLE anser_rf_probe_z (id float8, payload text) DISTRIBUTED BY (id); +INSERT INTO anser_rf_build_z VALUES (1, 'b1'), (-0.0, 'bz'); +INSERT INTO anser_rf_probe_z VALUES (1, 'p1'), (0.0, 'pz'), (2, 'p2'); +ANALYZE anser_rf_build_z; +ANALYZE anser_rf_probe_z; + +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_z_on AS + SELECT b.name, p.payload FROM anser_rf_build_z b JOIN anser_rf_probe_z p ON b.id = p.id + DISTRIBUTED BY (name); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_z_off AS + SELECT b.name, p.payload FROM anser_rf_build_z b JOIN anser_rf_probe_z p ON b.id = p.id + DISTRIBUTED BY (name); + +SELECT count(*) AS rows_on FROM anser_rf_z_on; +SELECT count(*) AS only_on + FROM (SELECT * FROM anser_rf_z_on EXCEPT ALL SELECT * FROM anser_rf_z_off) d; +SELECT count(*) AS only_off + FROM (SELECT * FROM anser_rf_z_off EXCEPT ALL SELECT * FROM anser_rf_z_on) d; + +DROP TABLE anser_rf_z_on, anser_rf_z_off, anser_rf_build_z, anser_rf_probe_z; + +-- The exclusion has to hold through a domain. A domain inherits typlen and +-- typbyval from its base type, so it looks by-value, and its OID is not one of +-- the float OIDs the exclusion names -- a check on the declared type alone lets +-- it straight through and -0.0 would stop matching 0.0. Both sides are +-- distributed on their text column so the join needs a Motion and is a +-- candidate for injection; the key type is what has to refuse it. +CREATE DOMAIN anser_rf_f8 AS float8; +CREATE TABLE anser_rf_build_d (id anser_rf_f8, name text) DISTRIBUTED BY (name); +CREATE TABLE anser_rf_probe_d (id anser_rf_f8, payload text) DISTRIBUTED BY (payload); +INSERT INTO anser_rf_build_d VALUES (1, 'b1'), (-0.0, 'bz'); +INSERT INTO anser_rf_probe_d VALUES (1, 'p1'), (0.0, 'pz'), (2, 'p2'); +ANALYZE anser_rf_build_d; +ANALYZE anser_rf_probe_d; + +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_d_on AS + SELECT b.name, p.payload FROM anser_rf_build_d b JOIN anser_rf_probe_d p ON b.id = p.id + DISTRIBUTED BY (name); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_d_off AS + SELECT b.name, p.payload FROM anser_rf_build_d b JOIN anser_rf_probe_d p ON b.id = p.id + DISTRIBUTED BY (name); + +SELECT count(*) AS rows_on FROM anser_rf_d_on; +SELECT count(*) AS only_on + FROM (SELECT * FROM anser_rf_d_on EXCEPT ALL SELECT * FROM anser_rf_d_off) d; +SELECT count(*) AS only_off + FROM (SELECT * FROM anser_rf_d_off EXCEPT ALL SELECT * FROM anser_rf_d_on) d; + +DROP TABLE anser_rf_d_on, anser_rf_d_off, anser_rf_build_d, anser_rf_probe_d; +DROP DOMAIN anser_rf_f8; + +-- Parameterized rescan guard: a build side whose rows depend on a parameter an +-- enclosing nested loop reassigns yields a different key set per outer tuple, +-- and neither node rebuilds on rescan -- the producer stays published, the +-- consumer keeps the filter it already has. The second iteration would be +-- probed against the first iteration's keys, dropping every row that joins. +-- So the plan pass must refuse the join outright. +-- +-- OFFSET 0 keeps the subquery from being pulled up, so it stays a nested loop +-- with a correlated inner side. Each group's ids are disjoint, which is what +-- makes a stale filter visible: with the first group's filter still in place +-- the second group matches nothing. +CREATE TABLE anser_rf_nl_outer (g int) DISTRIBUTED REPLICATED; +CREATE TABLE anser_rf_nl_build (g int, id int) DISTRIBUTED BY (g); +CREATE TABLE anser_rf_nl_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_nl_outer VALUES (1), (2); +INSERT INTO anser_rf_nl_build SELECT 1, g FROM generate_series(1, 100) g; +INSERT INTO anser_rf_nl_build SELECT 2, g FROM generate_series(101, 200) g; +INSERT INTO anser_rf_nl_probe SELECT g FROM generate_series(1, 20000) g; +ANALYZE anser_rf_nl_outer; +ANALYZE anser_rf_nl_build; +ANALYZE anser_rf_nl_probe; + +-- Both groups must report 100, with the filter on and off alike, and the plan +-- must carry no Anser node at all. +SET anser.runtime_filter = off; +SELECT o.g, s.n AS nl_off +FROM anser_rf_nl_outer o +CROSS JOIN LATERAL (SELECT count(*) n FROM anser_rf_nl_probe p + JOIN anser_rf_nl_build b ON p.id = b.id + WHERE b.g = o.g OFFSET 0) s +ORDER BY o.g; +SET anser.runtime_filter = on; +SELECT o.g, s.n AS nl_on +FROM anser_rf_nl_outer o +CROSS JOIN LATERAL (SELECT count(*) n FROM anser_rf_nl_probe p + JOIN anser_rf_nl_build b ON p.id = b.id + WHERE b.g = o.g OFFSET 0) s +ORDER BY o.g; +SELECT anser_rf_nodes('SELECT o.g, s.n FROM anser_rf_nl_outer o +CROSS JOIN LATERAL (SELECT count(*) n FROM anser_rf_nl_probe p + JOIN anser_rf_nl_build b ON p.id = b.id + WHERE b.g = o.g OFFSET 0) s') AS nodes_in_parameterized_plan; + +-- The same two tables joined without the correlation are still injected into: +-- the guard must refuse a parameterized build side, not every build side. +SELECT anser_rf_nodes('SELECT count(*) FROM anser_rf_nl_probe p + JOIN anser_rf_nl_build b ON p.id = b.id + WHERE b.g = 1') AS nodes_when_not_parameterized; + +DROP FUNCTION anser_rf_nodes(text); +DROP TABLE anser_rf_nl_outer, anser_rf_nl_build, anser_rf_nl_probe; + + + +-- --------------------------------------------------------------------------- +-- Parallel execution. +-- +-- A parallel slice runs numsegments * parallel_workers processes and every one +-- of them publishes its own part, so the coordinator must wait for that many +-- parts rather than for one per segment. Undercounting completes the channel +-- early and delivers a filter missing build keys; a bloom filter short of keys +-- gives false negatives, which silently drops joinable rows. So the assertion +-- that matters is not the plan shape but that results are *identical* with the +-- filter on and off -- at more than one worker count, and under both planners, +-- which disagree here: for one query the Postgres planner may give a serial +-- build slice where GPORCA gives a parallel one. +-- +-- The build side is distributed by v and joined on id, so it is redistributed: +-- Anser only injects across a Motion, and a co-located join -- which is what +-- this used to use -- is now skipped outright, making the whole section +-- vacuous. The cost is that only GPORCA still parallelises it; the Postgres +-- planner marks Motion paths parallel-unsafe, so its two blocks below are +-- reduced to a filter-on/filter-off equivalence check on a serial slice. +-- They are kept because that equivalence is worth asserting on its own. +-- --------------------------------------------------------------------------- +CREATE TABLE anser_rf_par_build (id int, v text) DISTRIBUTED BY (v); +CREATE TABLE anser_rf_par_probe (id int, pad text) DISTRIBUTED BY (id); +INSERT INTO anser_rf_par_build SELECT g, 'b' || g FROM generate_series(1, 500) g; +INSERT INTO anser_rf_par_probe SELECT g, 'p' || g FROM generate_series(1, 20000) g; +ANALYZE anser_rf_par_build; +ANALYZE anser_rf_par_probe; + +-- Force a parallel plan on tables this small, the way PostgreSQL's own tests +-- do; without this the scans are far too cheap to be worth splitting. +SET enable_parallel = on; +SET min_parallel_table_scan_size = 0; +SET parallel_setup_cost = 0; +SET parallel_tuple_cost = 0; + +-- Postgres planner, serial baseline +SET optimizer = off; +SET max_parallel_workers_per_gather = 0; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_par_on AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_par_off AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SELECT (SELECT count(*) FROM anser_rf_par_on) AS rows_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_on + EXCEPT ALL + SELECT * FROM anser_rf_par_off) d) AS only_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_off + EXCEPT ALL + SELECT * FROM anser_rf_par_on) d) AS only_off; +DROP TABLE anser_rf_par_on, anser_rf_par_off; + +-- Postgres planner, parallel +SET optimizer = off; +SET max_parallel_workers_per_gather = 4; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_par_on AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_par_off AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SELECT (SELECT count(*) FROM anser_rf_par_on) AS rows_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_on + EXCEPT ALL + SELECT * FROM anser_rf_par_off) d) AS only_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_off + EXCEPT ALL + SELECT * FROM anser_rf_par_on) d) AS only_off; +DROP TABLE anser_rf_par_on, anser_rf_par_off; + +-- GPORCA, serial baseline +SET optimizer = on; +SET max_parallel_workers_per_gather = 0; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_par_on AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_par_off AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SELECT (SELECT count(*) FROM anser_rf_par_on) AS rows_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_on + EXCEPT ALL + SELECT * FROM anser_rf_par_off) d) AS only_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_off + EXCEPT ALL + SELECT * FROM anser_rf_par_on) d) AS only_off; +DROP TABLE anser_rf_par_on, anser_rf_par_off; + +-- GPORCA, parallel +SET optimizer = on; +SET max_parallel_workers_per_gather = 4; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_par_on AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_par_off AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SELECT (SELECT count(*) FROM anser_rf_par_on) AS rows_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_on + EXCEPT ALL + SELECT * FROM anser_rf_par_off) d) AS only_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_off + EXCEPT ALL + SELECT * FROM anser_rf_par_on) d) AS only_off; +DROP TABLE anser_rf_par_on, anser_rf_par_off; + +DROP TABLE anser_rf_par_build, anser_rf_par_probe; +RESET enable_parallel; +RESET min_parallel_table_scan_size; +RESET parallel_setup_cost; +RESET parallel_tuple_cost; +RESET max_parallel_workers_per_gather; + +-- A probe side under a WindowAgg must not be filtered: the window is computed +-- over the whole input, so a consumer on the base scan below it would change +-- the result. The resolver must refuse the descent (the node whitelist +-- applies to the probe-side top too) and inject nothing. +SET optimizer = off; +SET anser.runtime_filter = on; +CREATE TABLE anser_rf_win (k int8, pad int) DISTRIBUTED BY (pad); +INSERT INTO anser_rf_win SELECT i % 1000, i FROM generate_series(1, 100000) i; +ANALYZE anser_rf_win; +-- A temp table is scanned on the coordinator, so the build side runs in the +-- coordinator's slice while the probe side runs on the segments: the slices +-- differ and the join would inject if the WindowAgg did not refuse it. k=5 +-- exists in the table (k = i % 1000, a hundred times), so the row matches and +-- the window total must be the full 100000 -- the windowed side returns every +-- row with the total attached, hence the max(). +CREATE TEMP TABLE anser_rf_dim (k int8) DISTRIBUTED BY (k); +INSERT INTO anser_rf_dim VALUES (5); +SELECT max(s.total) AS window_total +FROM anser_rf_dim d +LEFT JOIN (SELECT k, count(*) OVER () AS total FROM anser_rf_win) s + ON s.k = d.k; +DROP TABLE anser_rf_dim; +DROP TABLE anser_rf_win; + +-- IS NOT DISTINCT FROM below an outer join: ORCA puts a plain `=` in +-- hashclauses and the null-safety test in hashqualclauses, so the join passes +-- the equality gate, and the consumer used to land on the nullable side of +-- the lower LEFT JOIN. Every row it dropped came back NULL-extended and then +-- matched the build side's NULL under INDF -- 99997 rows instead of 0. The +-- resolver must now refuse the descent into the nullable side, so no filter +-- is injected and the count stays 0. +SET optimizer = on; +SET gp_enable_runtime_filter = off; +SET anser.runtime_filter = on; +CREATE TABLE anser_rf_s (x int, y int) DISTRIBUTED BY (x); +CREATE TABLE anser_rf_r (x int, k int) DISTRIBUTED BY (x); +CREATE TABLE anser_rf_b (k int) DISTRIBUTED RANDOMLY; +INSERT INTO anser_rf_s SELECT g, g FROM generate_series(1, 100000) g; +INSERT INTO anser_rf_r SELECT g, g + 1000000 FROM generate_series(1, 100000) g; +INSERT INTO anser_rf_b VALUES (NULL); +INSERT INTO anser_rf_b SELECT g FROM generate_series(1, 10) g; +ANALYZE anser_rf_s; +ANALYZE anser_rf_r; +ANALYZE anser_rf_b; +SELECT count(*) AS indf_rows FROM (anser_rf_s s LEFT JOIN anser_rf_r r ON s.x = r.x) + JOIN anser_rf_b b ON r.k IS NOT DISTINCT FROM b.k; + +-- Plain two-table INDF remains sound to filter: the bloom holds the build +-- side's non-NULL keys and NULL probe keys pass through, so matches and the +-- NULL row must all survive -- 1 and 2 are in b (1..10), NULL matches b's. +CREATE TABLE anser_rf_m (k int) DISTRIBUTED RANDOMLY; +INSERT INTO anser_rf_m VALUES (1), (2), (NULL); +SELECT count(*) AS indf_simple_rows FROM anser_rf_m m + JOIN anser_rf_b b ON m.k IS NOT DISTINCT FROM b.k; +DROP TABLE anser_rf_m, anser_rf_s, anser_rf_r, anser_rf_b; + +-- A computed probe key must refuse the filter: ORCA evaluates the cast in +-- the scan's projection (Output: (big)::oid), so the consumer would hash the +-- zero-extended computed oid while the producer hashes the sign-extended +-- stored oid -- every oid >= 2^31 mismatches, and only false positives pass +-- (3 rows instead of 50). The SeqScan shortcut must apply the same +-- plain-Var check as the resolver, so no consumer is injected. +CREATE TABLE anser_rf_p (id int, big int8) DISTRIBUTED BY (id); +CREATE TABLE anser_rf_o (o oid) DISTRIBUTED RANDOMLY; +INSERT INTO anser_rf_p SELECT g, 3000000000 + g FROM generate_series(1, 100000) g; +INSERT INTO anser_rf_o SELECT (3000000000 + g)::oid FROM generate_series(1, 50) g; +ANALYZE anser_rf_p; +ANALYZE anser_rf_o; +SELECT count(*) AS cast_rows + FROM (SELECT id, big::oid AS o FROM anser_rf_p) p + JOIN anser_rf_o b ON b.o = p.o; +DROP TABLE anser_rf_p, anser_rf_o; +RESET gp_enable_runtime_filter; +-- A filter over another filter must resolve the child's INDEX_VAR outputs +-- in the child's namespace, not copy them into a self-referencing namespace. +CREATE TABLE anser_stack_probe (k1 int, k2 int) DISTRIBUTED RANDOMLY; +CREATE TABLE anser_stack_b1 (k int) DISTRIBUTED RANDOMLY; +CREATE TABLE anser_stack_b2 (k int) DISTRIBUTED RANDOMLY; +INSERT INTO anser_stack_probe SELECT i % 100, i % 50 FROM generate_series(1, 10000) i; +INSERT INTO anser_stack_b1 SELECT i FROM generate_series(0, 99) i; +INSERT INTO anser_stack_b2 SELECT i FROM generate_series(0, 49) i; +ANALYZE anser_stack_probe; +ANALYZE anser_stack_b1; +ANALYZE anser_stack_b2; +DO $$ +DECLARE + opt text; + plan jsonb; +BEGIN + PERFORM set_config('anser.runtime_filter', 'on', true); + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + EXECUTE 'EXPLAIN (VERBOSE, COSTS OFF, FORMAT JSON) + SELECT p.k1, p.k2 FROM anser_stack_probe p + JOIN anser_stack_b1 b1 ON p.k1 = b1.k + JOIN anser_stack_b2 b2 ON p.k2 = b2.k' INTO plan; + IF NOT EXISTS ( + WITH RECURSIVE walk(node) AS ( + SELECT plan->0->'Plan' + UNION ALL + SELECT child FROM walk CROSS JOIN LATERAL + jsonb_array_elements(coalesce(node->'Plans', '[]')) child + ) SELECT FROM walk + WHERE node->>'Custom Plan Provider' = 'Anser Bloom Consumer' + AND node->'Plans'->0->>'Custom Plan Provider' = 'Anser Bloom Consumer') THEN + RAISE EXCEPTION 'stacked-consumer EXPLAIN fixture changed (optimizer=%): %', opt, plan; + END IF; + END LOOP; +END +$$; +DROP TABLE anser_stack_probe, anser_stack_b1, anser_stack_b2; +-- Resource and log knobs are superuser-only: an unprivileged role must not +-- force 64 MB filters on every segment or fill the server log. runtime_filter +-- and timeout_ms only shape the caller's own queries, so they stay +-- user-settable. +CREATE ROLE anser_nopriv LOGIN; +SET ROLE anser_nopriv; +SET anser.debug_min_filter_bytes = 67108864; +SET anser.max_info_size = 100000000; +SET anser.debug = on; +SET anser.runtime_filter = on; +SET anser.timeout_ms = 100; +RESET ROLE; +DROP ROLE anser_nopriv; + +RESET anser.runtime_filter; +RESET anser.debug_min_filter_bytes; +RESET anser.debug; +RESET anser.timeout_ms; +RESET optimizer; +RESET enable_nestloop; +RESET enable_mergejoin; + diff --git a/gpcontrib/anser/sql/anser_test.sql b/gpcontrib/anser/sql/anser_test.sql new file mode 100644 index 00000000000..de3bca72922 --- /dev/null +++ b/gpcontrib/anser/sql/anser_test.sql @@ -0,0 +1,284 @@ +CREATE EXTENSION anser_test; + +-- Bloom payload protocol: serialize one part and read it back. +SELECT anser_test_bloom_roundtrip('bf_roundtrip', 42); + +-- In-place fold: same-size union mutates the accumulator; a mismatched size is +-- rejected. This is the coordinator's only combine path (the first part is +-- kept verbatim, every later part folds into it). +SELECT anser_test_bloom_fold_inplace() AS fold_inplace_ok; + +-- Safety regression: the consumer rebuilds the filter from its own parameters +-- and requires the received bitset to be exactly the expected size (and the +-- header magic to match); truncated/oversized/corrupt parts are rejected. +SELECT anser_test_bloom_rejects_mismatch() AS reject_mismatch; + +-- Producer and consumer driven end to end in one backend: publish a part, let +-- the coordinator side merge it, then receive and query the filter. +SELECT anser_test_node_roundtrip(168); + +-- --------------------------------------------------------------------------- +-- Sizing, and the decision not to bother. +-- +-- Anser can decline to build a filter at four points, and these cases walk the +-- boundary of each. Declining is always safe: it costs the query its filter, +-- never its correctness. The payload cap is pinned here because every size +-- below follows from it. +-- --------------------------------------------------------------------------- +SET anser.max_info_size = 68157440; + +-- 1. The planner's gate -- the only one that costs nothing, since nothing is +-- injected and no consumer is left waiting. A 64 MB bitset is 536870912 bits, +-- so 134217728 estimated keys is exactly 4 bits/key and one more key is not. +-- A zero or negative estimate is treated as one key, not as "no filter"; the +-- 1 MB bitset floor then raises the declared count to match, so every estimate +-- the floor covers shows the same 524288 elements. +SELECT t.est_rows, s.injected, s.total_elems, s.planned_bytes +FROM (VALUES (0), (-5), (1), (1000), (1000000), (10000000), + (134217728), (134217729), (150000000), (1000000000)) + AS t(est_rows), + LATERAL anser_test_rf_size(t.est_rows) AS s; + +-- 2. Filter construction. bloom_create floors every bitset at 1 MB and that +-- floor overrides the work_mem cap, so a cap below "1 MB + header" can only +-- yield a filter too large to send -- which is why a flat 1 MB cap is refused +-- while 1048592 (the floor plus exactly one header) is accepted and serializes +-- to precisely the cap. fits_cap is the invariant that must never be false. +SELECT t.what, t.elems, t.cap, s.built, s.bits, s.serialized, + round(s.bits_per_key::numeric, 3) AS bits_per_key, + (NOT s.built OR s.serialized <= t.cap) AS fits_cap +FROM (VALUES + ('no keys', 0::bigint, 1048640::bigint), + ('negative keys', -1, 1048640), + ('cap equals the header', 32, 16), + ('cap one byte over the header', 32, 17), + ('cap is 1 MB, no header room', 32, 1048576), + ('cap is 1 MB plus a header', 32, 1048592), + ('cap one byte short of that', 32, 1048591), + ('a single key', 1, 1048640), + ('exactly 4 bits/key', 2097152, 1048640), + ('one key too many', 2097153, 1048640), + ('exactly 4 bits/key at 2 MB', 4194304, 2097216), + ('one key too many at 2 MB', 4194305, 2097216), + ('a billion keys', 1000000000, 68157440)) + AS t(what, elems, cap), + LATERAL anser_test_bloom_shape(t.elems, t.cap) AS s; + +-- 3. The coordinator's gate on a merged payload. The fill is fixed exactly by +-- setting whole bytes rather than by inserting keys, which would be both slow +-- and only statistically precise. The rule is "reject above 95%", so a payload +-- sitting exactly on the limit is still delivered. Malformed framing is not +-- worth delivering either, since it cannot be a filter at all. +SELECT t.what, t.bitset_bytes, t.bytes_set, t.damage, + anser_test_worth_delivering(t.bitset_bytes, t.bytes_set, t.damage) + AS worth_delivering +FROM (VALUES + ('empty', 1000, 0, ''), + ('half full', 1000, 500, ''), + ('just under the limit', 1000, 949, ''), + ('exactly on the limit', 1000, 950, ''), + ('just over the limit', 1000, 951, ''), + ('completely full', 1000, 1000, ''), + ('no bitset at all', 0, 0, ''), + ('corrupt magic', 1000, 0, 'magic'), + ('corrupt version', 1000, 0, 'version'), + ('zero part count', 1000, 0, 'parts'), + ('null payload', 1000, 0, 'null')) + AS t(what, bitset_bytes, bytes_set, damage); + +-- 4. The producer driven end to end on the coordinator-local path: build, +-- insert, publish, consume. "no-filter:cancelled" is the case that matters +-- most -- construction declined, so the cancel goes out before the build side +-- is scanned and no consumer waits for its timeout. The last row is the one +-- the planner cannot predict: a filter sized for 32 keys that receives three +-- million, which saturates and so publishes a cancel instead of a payload. +SELECT t.what, + anser_test_producer_decision(t.elems, t.cap, t.n_keys) AS decision +FROM (VALUES + ('sparse filter', 32::bigint, 1048640::bigint, 100), + ('cap with no header room', 32, 1048576, 100), + ('no keys declared', 0, 1048640, 0), + ('one key past the floor', 2097153, 1048640, 0), + ('exactly 4 bits/key', 2097152, 1048640, 100000), + ('estimate of 32, three million inserted', 32, 1048640, 3000000)) + AS t(what, elems, cap, n_keys); + +-- --------------------------------------------------------------------------- +-- The wire protocol. +-- +-- Not exhaustive; a fuzzer would be the right tool for that. These cover the +-- properties that are cheap to check and expensive to lose: a fixed-width +-- header with the fields where they are documented, a payload that cannot be +-- mistaken for framing, a length cross-check that rejects a message which does +-- not add up, and a checksum that rejects one altered byte anywhere it covers. +-- --------------------------------------------------------------------------- + +-- The bytes, exactly. Session/command/condition are 42/7/3 and part is 1 of 3, +-- so these are golden values: any change to the layout, the field widths or +-- what the checksum covers shows up here as a diff. Note the fourth row -- a +-- cancelled message carries no body even though one was passed, and its +-- checksum must be computed over what is actually sent. +-- +-- That the message comes back as text is itself the check that it is NUL-free, +-- which a NOTIFY payload has to be: the fifth row's body is 00 ff 00. +SELECT t.what, length(m.msg) AS len, m.msg +FROM (VALUES + ('part with a 3-byte body', 'P', 'B', 0, '\x616263'::bytea), + ('subscription, no body', 'S', '-', 0, '\x'::bytea), + ('next request, no body', 'N', '-', 0, '\x'::bytea), + ('cancelled: body suppressed', 'P', 'B', 1, '\x616263'::bytea), + ('body of NUL and 0xff bytes', 'P', 'B', 0, '\x00ff00'::bytea)) + AS t(what, kind, ptype, flags, body), + LATERAL (SELECT anser_test_wire_format(t.kind::"char", t.ptype::"char", + t.flags, 'anser_rf_3', t.body) + AS msg) m; + +-- Alter exactly one byte (or one length) and see what the reader does. +-- +-- The split matters: "malformed" is rejected by framing, before a byte is +-- allocated or a channel created, while "checksum-mismatch" got past framing +-- and was caught by the CRC. Corrupting the kind byte is the interesting one +-- -- the message stays perfectly well-formed, and only the checksum notices, +-- which is why the checksum covers the header and not just the payload. +SELECT t.tamper, + anser_test_wire_roundtrip('anser_rf_3', '\x616263'::bytea, t.tamper, + 'B'::"char", 'P'::"char") AS outcome +FROM (VALUES (''), ('truncate'), ('append'), ('bodylen'), ('tag'), ('kind'), + ('type'), ('crc'), ('key'), ('body')) AS t(tamper); + +-- A NEXT request is a message of its own kind: no body, and part_index carries +-- the offset the consumer wants next. The parser must accept it and every +-- field must survive. +SELECT anser_test_wire_roundtrip('anser_rf_3', '\x'::bytea, '', '-'::"char", + 'N'::"char") AS next_roundtrip; + +-- Keys and bodies that must survive the trip unchanged. "ok" means every +-- field and the decoded body came back identical, so each of these is a case +-- where a naive framing would have gone wrong: the key travels as raw bytes +-- and is taken by length, which is what lets it contain a newline, a space, or +-- the protocol's own tag without any escaping. +SELECT t.what, + anser_test_wire_roundtrip(t.key, t.body, '', 'B'::"char", 'P'::"char") AS outcome +FROM (VALUES + ('plain key', 'anser_rf_3' , '\x616263'::bytea), + ('key containing a newline', E'rf:a\nb' , '\x616263'::bytea), + ('key that looks like a header', 'anser4 P B 0000000000', '\x616263'::bytea), + ('key with spaces and quotes', 'rf:"a b".c' , '\x616263'::bytea), + ('key at the 63-byte limit', repeat('k', 63) , '\x616263'::bytea), + ('empty key', '' , '\x616263'::bytea), + ('empty body', 'anser_rf_3' , '\x'::bytea), + ('body of a single NUL', 'anser_rf_3' , '\x00'::bytea), + ('body of framing-hostile bytes', 'anser_rf_3' , '\x00ff0a0d5c22'::bytea), + ('1 KB body', 'anser_rf_3' , decode(repeat('00ff', 512), 'hex'))) + AS t(what, key, body); + +-- The QD -> QE checksum. The value itself does not matter; which inputs +-- change it does. Every routing field, the chunk's coordinates and the key +-- are always covered, and the body only for a payload type that asks for it -- +-- the other_body_exempt column is that deliberate exemption, not an oversight. +SELECT + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 2, 7, 3, 0, 1000, 0, 'k', '\x01') AS condition_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 8, 3, 0, 1000, 0, 'k', '\x01') AS session_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 4, 0, 1000, 0, 'k', '\x01') AS command_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 1, 1000, 0, 'k', '\x01') AS flags_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 2000, 0, 'k', '\x01') AS total_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 64, 'k', '\x01') AS offset_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'j', '\x01') AS key_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x02') AS bloom_body_covered, + anser_test_push_crc('-'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + = anser_test_push_crc('-'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x02') AS other_body_exempt, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('-'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') AS type_covered; + +-- 5. The runtime-filter pushdown decision. A scan key is numbered by the +-- table's attributes, while the consumer is numbered by the scan's output +-- targetlist -- the slot it probes has already been projected. So the whole +-- job of the decision is that mapping, plus refusing every shape where there +-- is no mapping to make. sk_attno is what the key would carry; NULL means +-- refused, and the consumer then probes the slot itself (always correct, just +-- slower). In the tlist spec a positive value is a plain Var on that table +-- attno; 0 is a whole-row Var, -1 a system column, -2 a computed column, -3 a +-- Var of another relation, -4 a cast wrapping a Var. +SELECT t.what, t.tlist, t.attno, s.accepts, s.sk_attno +FROM (VALUES + ('all columns in order', '{1,2,3}'::int4[], 2, true, true), + ('projecting scan, one column', '{2}', 1, true, true), + ('reordered, first of two', '{3,1}', 1, true, true), + ('reordered, second of two', '{3,1}', 2, true, true), + ('position past the targetlist', '{2}', 2, true, true), + ('position zero', '{2}', 0, true, true), + ('negative position', '{2}', -1, true, true), + ('computed column', '{-2}', 1, true, true), + ('cast wrapping a column', '{-4}', 1, true, true), + ('whole-row reference', '{0}', 1, true, true), + ('system column', '{-1}', 1, true, true), + ('column of another relation', '{-3}', 1, true, true), + ('filter_in_seqscan off', '{2}', 1, false, true), + ('not a SeqScan', '{2}', 1, true, false) + ) AS t(what, tlist, attno, filter_in_seqscan, as_seqscan), + LATERAL anser_test_pushdown_accepts(t.tlist, t.attno, t.filter_in_seqscan, + t.as_seqscan) AS s; + +-- 6. The walk that finds where to push. With nothing to descend through it +-- must agree with the decision and name the node itself; a node that refuses +-- yields no target at all rather than a target with a meaningless attno. The +-- descent through stacked nodes needs live CustomScanStates, so it is covered +-- end to end by anser_runtime_filter instead. +SELECT t.what, s.found_self, s.sk_attno +FROM (VALUES + ('accepting scan is its own target', '{2}'::int4[], 1, true), + ('refused scan has no target', '{-2}', 1, true), + ('non-scan has no target', '{2}', 1, false) + ) AS t(what, tlist, attno, as_seqscan), + LATERAL anser_test_pushdown_target(t.tlist, t.attno, true, + t.as_seqscan) AS s; + +-- 7. How many producers a slice runs. This number is the channel's +-- completion criterion, so over-counting costs every consumer the full +-- anser.timeout_ms -- the channel never reaches the count it waits for and +-- nobody is told -- while under-counting completes it early and hands out a +-- filter missing a segment's keys, which drops rows that join. So each of +-- FillSliceGangInfo's four formulas is pinned here, not inferred from one. +-- +-- numsegments is only meaningful for a reader or writer gang; the columns are +-- still filled for the other types to show they are ignored. +SELECT t.what, t.gang, t.nseg, t.workers, t.direct, + anser_test_slice_producers(t.gang, t.nseg, t.workers, t.direct, 0) + AS producers +FROM (VALUES + ('coordinator slice', 'unallocated', 3, 0, NULL::int4[]), + ('coordinator slice, parallel', 'unallocated', 3, 4, NULL), + ('entry db reader', 'entrydb', 3, 0, NULL), + ('singleton reader', 'singleton', 3, 0, NULL), + ('singleton reader, parallel', 'singleton', 3, 4, NULL), + ('reader, serial', 'reader', 3, 0, NULL), + ('reader, parallel', 'reader', 3, 4, NULL), + ('writer, serial', 'writer', 3, 0, NULL), + ('direct dispatch, one segment', 'reader', 3, 0, '{1}'), + ('direct dispatch, two, serial', 'reader', 3, 0, '{0,2}'), + ('direct dispatch, two, par 4', 'reader', 3, 4, '{0,2}'), + ('direct dispatch, nowhere', 'reader', 3, 4, '{}'), + ('no segments', 'reader', 0, 4, NULL), + ('negative segments', 'reader', -1, 4, NULL) + ) AS t(what, gang, nseg, workers, direct); + +-- The slice has to be identifiable at all; anything else is zero, which the +-- injection pass reads as "unknown" and declines on. +SELECT t.what, anser_test_slice_producers('reader', 3, 4, NULL, t.idx) + AS producers +FROM (VALUES + ('slice 0 of 1 exists', 0), + ('slice 1 of 1 does not', 1), + ('negative slice index', -1) + ) AS t(what, idx); + +RESET anser.max_info_size; +DROP EXTENSION anser_test; diff --git a/gpcontrib/anser/src/anserbloomconsume.c b/gpcontrib/anser/src/anserbloomconsume.c new file mode 100644 index 00000000000..1f3706f27e9 --- /dev/null +++ b/gpcontrib/anser/src/anserbloomconsume.c @@ -0,0 +1,206 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserbloomconsume.c + * Standalone Anser Bloom filter consumer executor helper. + * + * IDENTIFICATION + * gpcontrib/anser/src/anserbloomconsume.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "anser.h" +#include "anserbloom.h" +#include "anserfilter.h" +#include "anserpayload.h" +#include "ansersideband.h" +#include "cdb/cdbvars.h" +#include "miscadmin.h" +#include "tcop/dest.h" +#include "tcop/tcopprot.h" + +/* + * State for one Bloom filter consumer. Consumes the merged payload for a + * channel exactly once (consumed), either from the coordinator's channel table + * (local path) or from the dispatch sideband connection. cancelled records + * that the producer side aborted instead of delivering the payload. + */ +struct AnserBloomFilterConsumeState +{ + AnserChannelKey channel_key; + bloom_filter *filter; + int64 total_elems; /* filter sizing, shared with the producer */ + Size max_payload_bytes; + uint64 seed; + uint32 expected_parts; /* producers the plan says to wait for */ + uint32 received_parts; /* the same count, as the payload declares it */ + bool consumed; + bool cancelled; +}; + +static bool ExecAnserBloomFilterConsumeSideband(AnserBloomFilterConsumeState *state, + long timeout_ms); +static bool ExecAnserBloomFilterConsumeFinish(AnserBloomFilterConsumeState *state, + void *payload, Size payload_len); + +AnserBloomFilterConsumeState * +ExecInitAnserBloomFilterConsume(const AnserChannelKey *channel_key, + int64 total_elems, Size max_payload_bytes, + uint32 expected_parts) +{ + AnserBloomFilterConsumeState *state; + + if (channel_key == NULL || expected_parts == 0) + return NULL; + + state = palloc0(sizeof(AnserBloomFilterConsumeState)); + state->channel_key = *channel_key; + state->total_elems = total_elems; + state->max_payload_bytes = max_payload_bytes; + state->seed = AnserBloomSeed(channel_key->condition_key); + state->expected_parts = expected_parts; + return state; +} + +bool +ExecAnserBloomFilterConsume(AnserBloomFilterConsumeState *state, + long registration_timeout_ms) +{ + if (state == NULL) + return false; + + if (state->consumed) + return state->filter != NULL; + + return ExecAnserBloomFilterConsumeSideband(state, registration_timeout_ms); +} + +/* + * Dispatch-connection consume path. + * + * On a segment this blocks on our own dispatch socket until the coordinator + * pushes the merged filter; on the coordinator the merge happened in this very + * process, so there is nothing to wait for and we just read it. + */ +static bool +ExecAnserBloomFilterConsumeSideband(AnserBloomFilterConsumeState *state, + long timeout_ms) +{ + void *payload = NULL; + Size payload_len = 0; + bool cancelled = false; + bool got; + + /* + * Which of the two paths a process takes is decided by Gp_role alone, and + * a process that takes the wrong one fails open without ever reaching the + * coordinator: the local path finds no channel on a segment, and the + * sideband path finds no frontend on the coordinator. Logged with the + * facts the choice rests on, because a consumer that reports no filter + * cannot otherwise be told apart from one that asked and was refused. + */ + ANSER_DEBUG("anser: seg%d consumer cond=%u taking the %s path (role=%d writer=%d port=%s dest=%d expected_parts=%u timeout=%ld)", + GpIdentity.segindex, state->channel_key.condition_id, + Gp_role == GP_ROLE_EXECUTE ? "sideband" : "local", + (int) Gp_role, Gp_is_writer ? 1 : 0, + MyProcPort != NULL ? "ok" : "NULL", + (int) whereToSendOutput, + state->expected_parts, timeout_ms); + + if (Gp_role == GP_ROLE_EXECUTE) + got = AnserSidebandConsumeWait(&state->channel_key, ANSER_PAYLOAD_BLOOM, + &payload, &payload_len, &cancelled, + timeout_ms); + else + got = AnserDispatchLocalConsume(&state->channel_key, ANSER_PAYLOAD_BLOOM, + &payload, &payload_len, &cancelled); + + if (!got || cancelled) + { + if (payload != NULL) + pfree(payload); + state->cancelled = cancelled; + state->consumed = true; + return false; + } + + return ExecAnserBloomFilterConsumeFinish(state, payload, payload_len); +} + +/* + * Turn a merged payload into this consumer's filter. + * + * The coordinator unions every segment's part into one before delivery, so a + * single chunk is deserialized rather than N. The header's total_parts is how + * many producers the channel was waiting for -- every part carries the same + * number and folding leaves it alone -- so it is the payload's own account of + * the count this consumer was planned with, not a tally of what arrived. How + * many parts were folded is known only to the coordinator, which does not + * deliver until it has them all. + */ +static bool +ExecAnserBloomFilterConsumeFinish(AnserBloomFilterConsumeState *state, + void *payload, Size payload_len) +{ + uint32 part_index = 0; + uint32 declared_parts = 0; + + state->filter = AnserBloomDeserializePart(payload, payload_len, + state->total_elems, + state->max_payload_bytes, + state->seed, + &part_index, &declared_parts); + state->received_parts = (state->filter != NULL) ? declared_parts : 0; + + if (payload != NULL) + pfree(payload); + state->consumed = true; + return state->filter != NULL; +} + +bloom_filter * +ExecAnserBloomFilterConsumerGetFilter(AnserBloomFilterConsumeState *state) +{ + return state != NULL ? state->filter : NULL; +} + +uint32 +ExecAnserBloomFilterConsumerReceivedParts(AnserBloomFilterConsumeState *state) +{ + return state != NULL ? state->received_parts : 0; +} + +bool +ExecAnserBloomFilterConsumerWasCancelled(AnserBloomFilterConsumeState *state) +{ + return state != NULL && state->cancelled; +} + +void +ExecEndAnserBloomFilterConsume(AnserBloomFilterConsumeState *state) +{ + if (state == NULL) + return; + + if (state->filter != NULL) + bloom_free(state->filter); + pfree(state); +} diff --git a/gpcontrib/anser/src/anserbloomproduce.c b/gpcontrib/anser/src/anserbloomproduce.c new file mode 100644 index 00000000000..1bf5d584439 --- /dev/null +++ b/gpcontrib/anser/src/anserbloomproduce.c @@ -0,0 +1,219 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserbloomproduce.c + * Standalone Anser Bloom filter producer executor helper. + * + * IDENTIFICATION + * gpcontrib/anser/src/anserbloomproduce.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "anser.h" +#include "anserbloom.h" +#include "anserfilter.h" +#include "anserpayload.h" +#include "ansersideband.h" +#include "cdb/cdbvars.h" + +/* + * State for a single bloom filter producer: the target channel, the filter + * being built, and this producer's identity within total_parts. published + * and cancelled guard against double publication and drive teardown. + */ +struct AnserBloomFilterProduceState +{ + AnserChannelKey channel_key; + bloom_filter *filter; + uint32 part_index; + uint32 total_parts; + bool published; + bool cancelled; +}; + +/* + * Publish one part. + * + * A segment sends it to the coordinator over the dispatch connection; a + * coordinator-local producer hands it to the same per-query channel table the + * coordinator merges into, since it has no connection to itself. total_parts + * doubles as expected_producers: each producer contributes exactly one part. + */ +static bool +AnserProducePublishPart(AnserBloomFilterProduceState *state, + const void *payload, Size payload_len, bool cancelled) +{ + if (Gp_role == GP_ROLE_EXECUTE) + return AnserSidebandPublish(&state->channel_key, ANSER_PAYLOAD_BLOOM, + state->part_index, state->total_parts, + payload, payload_len, cancelled); + + return AnserDispatchLocalPublish(&state->channel_key, ANSER_PAYLOAD_BLOOM, + state->part_index, state->total_parts, + payload, payload_len, cancelled); +} + +AnserBloomFilterProduceState * +ExecInitAnserBloomFilterProduce(const AnserChannelKey *channel_key, + int64 total_elems, + Size max_payload_bytes, + uint32 part_index, + uint32 total_parts) +{ + AnserBloomFilterProduceState *state; + uint64 seed; + + if (channel_key == NULL || total_parts == 0 || part_index >= total_parts) + return NULL; + + state = palloc0(sizeof(AnserBloomFilterProduceState)); + state->channel_key = *channel_key; + state->part_index = part_index; + state->total_parts = total_parts; + seed = AnserBloomSeed(channel_key->condition_key); + state->filter = AnserBloomCreate(total_elems, max_payload_bytes, seed); + + return state; +} + +void +ExecAnserBloomFilterProduceAddDatum(AnserBloomFilterProduceState *state, + Datum value, bool isnull) +{ + /* + * A NULL filter means AnserBloomCreate declined to build one -- too many + * keys for the payload cap. There is nothing to add to, and the node has + * already published the cancel. + */ + if (state == NULL || state->filter == NULL || state->published || isnull) + return; + + bloom_add_element(state->filter, (unsigned char *) &value, sizeof(Datum)); +} + +/* Does this producer have a filter to fill? False means "already hopeless". */ +bool +ExecAnserBloomFilterProduceHasFilter(AnserBloomFilterProduceState *state) +{ + return state != NULL && state->filter != NULL; +} + +bool +ExecAnserBloomFilterProducePublish(AnserBloomFilterProduceState *state) +{ + Size payload_size; + Size payload_len = 0; + void *payload; + bool ok; + + if (state == NULL || state->published) + { + ANSER_DEBUG("anser: publish skipped (%s)", + state == NULL ? "no producer state" : "already published"); + return false; + } + + if (state->cancelled || state->filter == NULL) + { + ANSER_DEBUG("anser: publishing a cancel (%s)", + state->cancelled ? "producer cancelled" : "no filter"); + state->published = true; + return AnserProducePublishPart(state, NULL, 0, true); + } + + /* + * Too saturated to be worth anything? Cancel rather than send. + * + * This is the check the planner cannot make: it decided the filter was + * worth building from a row estimate, and estimates are wrong. A filter + * whose bits are nearly all set matches nearly every probe row, so shipping + * it would buy the consumers a hash and k probes per row in exchange for + * almost no rows eliminated -- pure overhead on both sides. + */ + if (bloom_false_positive_rate(state->filter) > ANSER_BLOOM_MAX_FPR) + { + ANSER_DEBUG("anser: publishing a cancel: filter is %.1f%% full, est. FPR %.1f%% above the %.0f%% limit", + bloom_prop_bits_set(state->filter) * 100.0, + bloom_false_positive_rate(state->filter) * 100.0, + ANSER_BLOOM_MAX_FPR * 100.0); + state->published = true; + return AnserProducePublishPart(state, NULL, 0, true); + } + + ANSER_DEBUG("anser: filter is %.1f%% full, est. FPR %.2f%%", + bloom_prop_bits_set(state->filter) * 100.0, + bloom_false_positive_rate(state->filter) * 100.0); + + /* + * Serialize as a self-contained single part (index 0 of 1). The coordinator + * stores the first part verbatim and OR-folds each later part, bumping the + * merged header's fold count, so the final count reflects how many parts were + * unioned. state->part_index / state->total_parts identify this producer to + * the channel (expected_producers), not the on-wire part layout. + */ + payload_size = AnserBloomSerializedSize(state->filter); + payload = palloc(payload_size); + ok = AnserBloomSerializePart(state->filter, + 0, + 1, + payload, + payload_size, + &payload_len); + if (ok) + ok = AnserProducePublishPart(state, payload, payload_len, false); + else + ANSER_DEBUG("anser: publish skipped: could not serialize part (size=%zu)", + payload_size); + + pfree(payload); + state->published = true; + return ok; +} + +bool +ExecAnserBloomFilterProduceCancel(AnserBloomFilterProduceState *state) +{ + if (state == NULL || state->published) + return false; + + state->cancelled = true; + state->published = true; + return AnserProducePublishPart(state, NULL, 0, true); +} + +/* + * Free the producer state. + * + * Deliberately does NOT publish a cancel for an unpublished producer: whether + * silence means "ran and was abandoned" or "never ran at all" is knowable only + * to the node, and the two need opposite handling (see anser_produce_end in + * anserplanexec.c). + */ +void +ExecEndAnserBloomFilterProduce(AnserBloomFilterProduceState *state) +{ + if (state == NULL) + return; + + if (state->filter != NULL) + bloom_free(state->filter); + pfree(state); +} diff --git a/gpcontrib/anser/src/anserdispatch.c b/gpcontrib/anser/src/anserdispatch.c new file mode 100644 index 00000000000..d6479cec871 --- /dev/null +++ b/gpcontrib/anser/src/anserdispatch.c @@ -0,0 +1,1275 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserdispatch.c + * Coordinator side of the dispatch-connection transport. + * + * Everything here runs in the QD backend, reached from cdbdisp_notify_hook + * while the dispatcher drains QE messages -- which happens from inside the + * interconnect wait loop, so this code is on the query's critical path. It + * must stay cheap and must not throw for anything recoverable: an error here + * lands in a running query, whereas losing a filter only costs us an + * unfiltered scan. + * + * Because producer merge and consumer delivery both happen in this one + * process, the accumulator is an ordinary palloc'd buffer. There is no shared + * memory, no DSM segment to attach, and no separate worker to hand data to; + * parts are folded in place as they arrive, so only the final fold is on the + * critical path. + * + * Nothing here knows what a part contains. Merging is delegated to the payload + * type's fold() (anserpayload.h), and the only other thing this file asks about + * a type is whether its body is covered by the message checksum -- which is why + * it does not include anserfilter.h at all. + * + * A note on libpq linkage. The connections we write to were created by the + * copy of libpq that is statically linked into the postgres binary, and every + * symbol listed in libpq's exports.txt is deliberately made *local* in that + * binary (see the version-script hack in src/backend/Makefile) precisely so a + * module cannot end up driving one connection through two copies of libpq. + * So this file must not call the public PQ* API: linking libpq.so to obtain it + * would create exactly the mixture that hack exists to prevent. The internal + * pq* writers are not in exports.txt and so remain global in the backend, + * which is how pqPutMsgStart() and friends resolve here; the two accessors we + * would otherwise want are inlined below, straight off the struct. + * + * IDENTIFICATION + * gpcontrib/anser/src/anserdispatch.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include + +#include "libpq-fe.h" +#include "libpq-events.h" +#include "libpq-int.h" + +#include "anser.h" +#include "anserpayload.h" +#include "ansersideband.h" +#include "cdb/cdbconn.h" +#include "cdb/cdbdisp.h" +#include "cdb/cdbdispatchresult.h" +#include "cdb/cdbvars.h" +#include "storage/latch.h" +#include "utils/faultinjector.h" +#include "common/base64.h" +#include "lib/stringinfo.h" +#include "lib/ilist.h" +#include "nodes/pg_list.h" +#include "portability/instr_time.h" +#include "utils/hsearch.h" +#include "utils/memutils.h" +#include "utils/wait_event.h" + +/* + * One in-flight chunked delivery to one consumer. The merged payload is sent + * one chunk per GP_SIDEBAND_MESSAGE, and the next chunk goes out only when the + * consumer asks for it with a NEXT -- so no more than one chunk, always + * smaller than the connection's send buffer, is ever in flight, and a consumer + * that walks away between chunks leaves nothing torn behind. + */ +typedef struct AnserDispDelivery +{ + dlist_node active_node; + struct AnserDispChannel *channel; + PGconn *conn; + int segindex; /* for logs */ + int chunk_size; /* chosen once; see anser_disp_chunk_size() */ + uint32 next_offset; /* sent so far; a NEXT must ask for exactly this */ + bool started; /* chunk 0 has been sent */ +} AnserDispDelivery; + +/* + * One channel's merge state, living for the duration of the query that created + * it. Keyed by AnserChannelKey, which must be the first field. + */ +typedef struct AnserDispChannel +{ + AnserChannelKey key; + const AnserPayloadOps *ops; /* what this carries; NULL until a part says */ + char *payload; /* merged part, or NULL before the first one */ + Size payload_len; + int parts_received; + int expected_parts; /* 0 until a part tells us; from total_parts */ + bool cancelled; + bool complete; /* every expected part folded, or cancelled */ + List *deliveries; /* AnserDispDelivery, one per subscribed QE */ +} AnserDispChannel; + +static HTAB *AnserDispChannels = NULL; + +/* + * The chunk must fit an empty send buffer, so one pqFlushNonBlocking() places + * it whole and outCount returns to zero. SO_SNDBUF is halved because Linux + * reports it doubled (the kernel's bookkeeping share included); the floor + * keeps a tiny or unreadable buffer from turning the delivery into thousands + * of round trips. anser.debug_delivery_chunk_bytes overrides the choice, for + * tests that want exactly that. + */ +#define ANSER_CHUNK_MAX (1024 * 1024) +#define ANSER_CHUNK_MIN (8 * 1024) + +/* + * Connections holding a partially written chunk. With chunked delivery this + * is only the insurance case: a chunk is sized to fit an empty send buffer, + * and anser_disp_send_chunk() gives the socket a bounded moment to drain + * before recording the connection here. Reaching this list therefore means + * the peer has not read for a full second -- it is gone or wedged, and + * AnserDispatchFlushPending() poisons the connection before the + * gang is reused. List nodes live in TopMemoryContext: the list must survive + * until ExecutorEnd regardless of the context a delivery ran in. + * + * Connections are registered with libpq before Anser retains any pointer. + * PGEVT_CONNDESTROY removes every reference before PQfinish frees a PGconn, + * including when an InitPlan releases its gangs before ExecutorEnd. A reset + * drops the old stream's deliveries too. No dispatcher/core hook is needed. + */ +static List *AnserDispPendingPush = NIL; + +/* Intrusive index: teardown must not allocate or start a dynahash scan. */ +static dlist_head AnserDispDeliveries = DLIST_STATIC_INIT(AnserDispDeliveries); + +/* + * Withdrawals honoured since the backend started. Only a test reads it: an + * unsubscribe leaves no other trace, because the channel it touches is gone + * by the time the query returns. Counting them is how we tell "the consumer + * withdrew and the coordinator acted on it" from "the filter happened to + * arrive in time", which look identical from SQL. + */ +static int64 AnserDispUnsubscribes = 0; + +/* + * Deliveries that reached their last chunk. Only a test reads it: a + * completed delivery leaves no other trace, and a test that wants to know the + * filter really arrived -- rather than the consumer quietly giving up and + * running unfiltered, which produces the same rows -- reads the difference. + */ +static int64 AnserDispDeliveriesCompleted = 0; +static MemoryContext AnserDispContext = NULL; + +static AnserDispChannel *anser_disp_lookup(const AnserChannelKey *key, bool create); +static void anser_disp_apply_part(AnserDispChannel *chan, + const AnserPayloadOps *ops, + const void *payload, Size payload_len, + int total_parts, bool cancelled); +static void anser_disp_deliver(AnserDispChannel *chan); +static int anser_disp_chunk_size(PGconn *conn); +static AnserDispDelivery *anser_disp_delivery_find(AnserDispChannel *chan, + PGconn *conn); +static void anser_disp_delivery_drop(AnserDispChannel *chan, + AnserDispDelivery *d); +static void anser_disp_send_chunk(AnserDispChannel *chan, AnserDispDelivery *d); + +/* + * PQstatus() / PQerrorMessage() equivalents. See the linkage note above for + * why these are not the real thing; libpq-int.h gives us the full struct. + */ +static inline bool +anser_conn_ok(const PGconn *conn) +{ + return conn != NULL && conn->status == CONNECTION_OK; +} + +static inline const char * +anser_conn_error(const PGconn *conn) +{ + if (conn == NULL) + return "connection pointer is NULL"; + if (PQExpBufferBroken(&conn->errorMessage)) + return "out of memory"; + return conn->errorMessage.data; +} + +/* PQsocket() equivalent; same linkage note as above. */ +static inline int +anser_conn_socket(const PGconn *conn) +{ + return conn->sock; +} + +/* + * How many withdrawals this backend has honoured. For tests; see the counter. + */ +int64 +AnserDispatchUnsubscribeCount(void) +{ + return AnserDispUnsubscribes; +} + +/* + * How many deliveries this backend has completed. For tests; see the counter. + */ +int64 +AnserDispatchDeliveriesCompleted(void) +{ + return AnserDispDeliveriesCompleted; +} + +/* + * Per-query state lives in its own context so it can be dropped wholesale. + * Channels are keyed by (session, command, condition), so entries from an + * earlier command in the same transaction are distinct and simply unused + * until the reset. + */ +static void +anser_disp_init(void) +{ + HASHCTL hctl; + + if (AnserDispChannels != NULL) + return; + + AnserDispContext = AllocSetContextCreate(TopMemoryContext, + "Anser dispatch transport", + ALLOCSET_DEFAULT_SIZES); + + MemSet(&hctl, 0, sizeof(hctl)); + hctl.keysize = sizeof(AnserChannelKey); + hctl.entrysize = sizeof(AnserDispChannel); + hctl.hcxt = AnserDispContext; + + AnserDispChannels = hash_create("Anser dispatch channels", 32, &hctl, + HASH_ELEM | HASH_BLOBS | HASH_CONTEXT); +} + +void +AnserDispatchReset(void) +{ + /* + * Drop the pending-delivery list too, but only the list nodes: the + * connections it names may already be dead on the abort path, and + * dereferencing them is reserved to cleanup while their gang is live (see the + * invariant at the declaration). A delivery dropped here unflushed is + * the cleanupQE guard's problem, which poisons the connection at recycle. + */ + list_free(AnserDispPendingPush); + AnserDispPendingPush = NIL; + + if (AnserDispChannels == NULL) + return; + + ANSER_DEBUG("anser: QD reset drops %ld channel(s)", + hash_get_num_entries(AnserDispChannels)); + + hash_destroy(AnserDispChannels); + AnserDispChannels = NULL; + MemoryContextDelete(AnserDispContext); + AnserDispContext = NULL; + dlist_init(&AnserDispDeliveries); +} + +/* + * Drop the finished channels of one command, leaving every other query's + * alone. + * + * AnserDispatchReset() is wholesale, so it can only run once nothing else is + * executing. Inside SPI that is not until the outermost statement ends, which + * holds every inner query's merged filter -- at least anser.max_info_size each + * -- for the whole call. The key carries the command count, so a query can + * take its own channels with it instead of waiting for that sweep. + * + * Only channels that are complete and have no delivery in flight are taken, + * because the caller cannot always prove the count is its own: gp_command_count + * names the newest query descriptor, not the one that is ending, and the two + * differ whenever a portal outlives the statements that ran after it. Closing + * such a cursor from inside a live query would otherwise hand us that live + * query's count. A channel that is complete and already delivered costs at + * most a consumer that had not subscribed yet, which fails open; an exchange + * still in flight is never touched, and a channel with a delivery in flight + * keeps its payload until the sweep. + * + * The flip side is that a channel that never completed -- a squelched producer, + * say -- still waits for the sweep. + */ +void +AnserDispatchResetCommand(int command_count) +{ + HASH_SEQ_STATUS status; + AnserDispChannel *chan; + + if (AnserDispChannels == NULL) + return; + + hash_seq_init(&status, AnserDispChannels); + while ((chan = (AnserDispChannel *) hash_seq_search(&status)) != NULL) + { + if (chan->key.gp_command_count != command_count) + continue; + if (!chan->complete || chan->deliveries != NIL) + continue; + + ANSER_DEBUG("anser: QD dropping finished channel cond=%u ccnt=%d", + chan->key.condition_id, chan->key.gp_command_count); + + /* + * AnserDispContext outlives this, so free what the entry owns rather + * than the context. Deleting the entry the scan just handed back is + * explicitly allowed; see hash_seq_search(). + */ + if (chan->payload != NULL) + pfree(chan->payload); + + if (hash_search(AnserDispChannels, &chan->key, + HASH_REMOVE, NULL) == NULL) + elog(ERROR, "anser: dispatch channel hash table corrupted"); + } +} + +/* + * Poison torn deliveries before the connections can be reused. Teardown may + * run while handling another error, so it must not wait, allocate or report + * another error. timeout_ms remains in the public API for compatibility. + */ +void +AnserDispatchFlushPending(long timeout_ms) +{ + ListCell *lc; + + (void) timeout_ms; + + foreach(lc, AnserDispPendingPush) + { + PGconn *conn = (PGconn *) lfirst(lc); + + if (conn->outCount > 0) + conn->status = CONNECTION_BAD; + } + list_free(AnserDispPendingPush); + AnserDispPendingPush = NIL; +} + +/* list_delete_ptr can allocate in DEBUG_LIST_MEMORY_USAGE builds. Teardown + * needs the ordinary in-place deletion even in those builds. */ +static List * +anser_disp_list_forget(List *list, void *value) +{ + int i; + + for (i = 0; i < list_length(list); ++i) + { + if (lfirst(&list->elements[i]) != value) + continue; + if (list->length == 1) + { + list_free(list); + return NIL; + } + memmove(&list->elements[i], &list->elements[i + 1], + (list->length - i - 1) * sizeof(ListCell)); + --list->length; + break; + } + return list; +} + +/* libpq invokes this while conn is still alive, including on error cleanup. + * Never allocate, log, wait, or traverse dynahash in the destruction path. */ +static void +anser_disp_forget_conn(PGconn *conn) +{ + dlist_mutable_iter iter; + + AnserDispPendingPush = anser_disp_list_forget(AnserDispPendingPush, conn); + dlist_foreach_modify(iter, &AnserDispDeliveries) + { + AnserDispDelivery *d = dlist_container(AnserDispDelivery, active_node, iter.cur); + + if (d->conn == conn) + anser_disp_delivery_drop(d->channel, d); + } +} + +static int +anser_disp_conn_event(PGEventId event, void *info, void *pass_through) +{ + switch (event) + { + case PGEVT_CONNDESTROY: + anser_disp_forget_conn(((PGEventConnDestroy *) info)->conn); + break; + case PGEVT_CONNRESET: + anser_disp_forget_conn(((PGEventConnReset *) info)->conn); + break; + default: + break; + } + return 1; +} + +/* + * Register using the same PGEvent layout and libc allocation as + * PQregisterEventProc. Cloudberry hides public PQ* symbols in postgres; + * linking a second libpq to operate on the backend's connections is unsafe. + * Anser already requires the matching libpq-int.h ABI for its message writer. + * Registration happens during normal notify processing, never cleanup. On + * allocation failure refuse the subscription, which then times out unfiltered. + */ +static bool +anser_disp_track_conn(PGconn *conn) +{ + int i; + char *name; + PGEvent *event; + + if (conn == NULL) + return false; + for (i = 0; i < conn->nEvents; ++i) + if (conn->events[i].proc == anser_disp_conn_event) + return true; + + name = strdup("Anser dispatch delivery"); + if (name == NULL) + return false; + if (conn->nEvents >= conn->eventArraySize) + { + int capacity; + PGEvent *events; + + if (conn->eventArraySize > INT_MAX / 2) + { + free(name); + return false; + } + capacity = conn->eventArraySize ? conn->eventArraySize * 2 : 8; + events = realloc(conn->events, (size_t) capacity * sizeof(PGEvent)); + if (events == NULL) + { + free(name); + return false; + } + conn->events = events; + conn->eventArraySize = capacity; + } + event = &conn->events[conn->nEvents++]; + event->proc = anser_disp_conn_event; + event->name = name; + event->passThrough = NULL; + event->data = NULL; + event->resultInitialized = false; + return true; +} + +static AnserDispChannel * +anser_disp_lookup(const AnserChannelKey *key, bool create) +{ + AnserDispChannel *chan; + bool found; + + anser_disp_init(); + + chan = (AnserDispChannel *) hash_search(AnserDispChannels, key, + create ? HASH_ENTER : HASH_FIND, + &found); + if (chan == NULL) + return NULL; + + if (create && !found) + { + /* hash_search only fills the key; initialize the rest. */ + chan->ops = NULL; + chan->payload = NULL; + chan->payload_len = 0; + chan->parts_received = 0; + chan->expected_parts = 0; + chan->cancelled = false; + chan->complete = false; + chan->deliveries = NIL; + } + + return chan; +} + +/* + * cdbdisp_notify_hook: is this one of ours, and if so, handle it. + * + * Returns false for any notify on another channel so the dispatcher can carry + * on with its own handling. A malformed payload is consumed (it is addressed + * to us) but otherwise ignored: the affected consumers will time out and run + * unfiltered. + */ +bool +AnserDispatchNotifyHandler(struct CdbDispatchResult *dispatchResult, + struct pgNotify *notify) +{ + PGnotify *n = (PGnotify *) notify; + CdbDispatchResult *dr = (CdbDispatchResult *) dispatchResult; + int sender = (dr != NULL && dr->segdbDesc != NULL) + ? dr->segdbDesc->segindex : -99; + AnserWireMsg msg; + const AnserPayloadOps *ops; + AnserDispChannel *chan; + MemoryContext oldcxt; + + if (n == NULL || n->relname == NULL || + strcmp(n->relname, ANSER_NOTIFY_CHANNEL) != 0) + return false; + + if (n->extra == NULL || !AnserWireParse(n->extra, &msg)) + { + elog(LOG, "anser: ignoring malformed message from a segment (len=%zu)", + n->extra != NULL ? strlen(n->extra) : (size_t) 0); + return true; + } + + /* + * Resolve what the message carries before doing anything with it. An + * unregistered type means a producer was taught to send something this + * coordinator does not know how to merge -- almost certainly a payload type + * added to anserpayload.h but not to AnserPayloadTable. + */ + ops = AnserPayloadLookup(msg.payload_type); + if (ops == NULL) + { + elog(LOG, "anser: unknown payload type '%c' from seg%d (cond=%u); ignoring", + msg.payload_type, sender, msg.key.condition_id); + return true; + } + + /* + * A message with no body can be checked here, before anything is + * allocated; one with a body is checked in the PART branch below, once it + * has been decoded. + */ + if (msg.body_len == 0 && !AnserWireCheckCrc(&msg, NULL, 0)) + { + elog(LOG, "anser: ignoring corrupted message from seg%d (cond=%u kind=%c)", + sender, msg.key.condition_id, msg.kind); + return true; + } + + anser_disp_init(); + oldcxt = MemoryContextSwitchTo(AnserDispContext); + + /* + * NEXT names a channel that must already exist: one that does not was + * dropped with its query, and the request is simply late. + */ + if (msg.kind == ANSER_WIRE_KIND_NEXT) + { + PGconn *conn = dr->segdbDesc->conn; + AnserDispDelivery *d; + + chan = anser_disp_lookup(&msg.key, false); + d = chan != NULL ? anser_disp_delivery_find(chan, conn) : NULL; + + /* + * A NEXT for a delivery we do not have, or for an offset we already + * passed, is a late duplicate: the delivery it belongs to is over or + * never started. Skipping it costs nothing -- the consumer's deadline + * handles the rest. + */ + if (d == NULL || (uint32) msg.part_index != d->next_offset) + { + ANSER_DEBUG("anser: QD ignoring NEXT cond=%u from seg%d (offset %d, channel %s)", + msg.key.condition_id, sender, msg.part_index, + chan == NULL ? "gone" : "mismatched"); + } + else + anser_disp_send_chunk(chan, d); + + MemoryContextSwitchTo(oldcxt); + return true; + } + + chan = anser_disp_lookup(&msg.key, true); + if (chan == NULL) + { + MemoryContextSwitchTo(oldcxt); + return true; + } + + if (msg.kind == ANSER_WIRE_KIND_SUBSCRIBE) + { + PGconn *conn = dr->segdbDesc->conn; + AnserDispDelivery *d; + + if (!anser_disp_track_conn(conn)) + { + MemoryContextSwitchTo(oldcxt); + return true; + } + + /* + * A second subscribe from the same connection restarts its delivery + * from the beginning -- it cannot mean anything else, because a + * consumer subscribes once per wait. + */ + d = anser_disp_delivery_find(chan, conn); + if (d != NULL) + anser_disp_delivery_drop(chan, d); + + d = (AnserDispDelivery *) palloc0(sizeof(AnserDispDelivery)); + d->conn = conn; + d->segindex = sender; + d->chunk_size = anser_disp_chunk_size(conn); + d->next_offset = 0; + d->started = false; + chan->deliveries = lappend(chan->deliveries, d); + d->channel = chan; + dlist_push_head(&AnserDispDeliveries, &d->active_node); + + /* + * A consumer can subscribe after the channel is already complete -- + * producers on other segments may well have finished first -- so + * start its delivery immediately in that case. + */ + ANSER_DEBUG("anser: QD subscribe cond=%u from seg%d (channel %s)", + msg.key.condition_id, sender, + chan->complete ? "complete, delivering now" : "still collecting"); + if (chan->complete) + anser_disp_send_chunk(chan, d); + } + else if (msg.kind == ANSER_WIRE_KIND_UNSUBSCRIBE) + { + PGconn *conn = dr->segdbDesc->conn; + AnserDispDelivery *d = anser_disp_delivery_find(chan, conn); + + /* + * The consumer ran out of time and is no longer reading for us. + * Dropping the delivery stops the flow at the chunk boundary: at most + * one whole chunk is in flight, and the reader discards it. + */ + ANSER_DEBUG("anser: QD unsubscribe cond=%u from seg%d (%d deliver(ies) before)", + msg.key.condition_id, sender, + list_length(chan->deliveries)); + if (d != NULL) + anser_disp_delivery_drop(chan, d); + AnserDispUnsubscribes++; + } + else if (msg.kind == ANSER_WIRE_KIND_PART) + { + char *raw = NULL; + int raw_len = 0; + + if (!(msg.flags & ANSER_WIRE_F_CANCELLED) && msg.body_len > 0) + { + int maxlen = pg_b64_dec_len(msg.body_len); + + raw = palloc(maxlen); + raw_len = pg_b64_decode(msg.body, msg.body_len, raw, maxlen); + + /* + * Undecodable, oversized or corrupted: cancel the channel rather + * than guess. Consumers then run unfiltered, which is slower but + * correct -- folding in a part we cannot vouch for risks clearing a + * bit that should be set, and a filter missing a key silently drops + * joinable rows. + */ + if (raw_len < 0 || raw_len > gp_anser_max_info_size || + !AnserWireCheckCrc(&msg, raw, (Size) raw_len)) + { + elog(LOG, "anser: unusable part for condition %u from seg%d; cancelling channel", + msg.key.condition_id, sender); + pfree(raw); + raw = NULL; + raw_len = 0; + msg.flags |= ANSER_WIRE_F_CANCELLED; + } + } + + anser_disp_apply_part(chan, ops, raw, (Size) raw_len, msg.total_parts, + (msg.flags & ANSER_WIRE_F_CANCELLED) != 0); + ANSER_DEBUG("anser: QD %s part cond=%u from seg%d (says part %d of %d) %d/%d bytes=%d -> %s", + ops->name, msg.key.condition_id, sender, msg.part_index, + msg.total_parts, chan->parts_received, + chan->expected_parts, raw_len, + chan->cancelled ? "cancelled" : + chan->complete ? "complete" : "collecting"); + if (raw != NULL) + pfree(raw); + } + + if (chan->complete) + anser_disp_deliver(chan); + + MemoryContextSwitchTo(oldcxt); + return true; +} + +/* + * Fold one part into the channel's accumulator. + * + * The first part is kept verbatim and becomes the accumulator; later parts are + * merged into it in place by the payload type's fold(), so no part is ever + * copied twice and the accumulator is never reallocated. Nothing here knows + * what the bytes are -- for a bloom filter the fold is a bitwise OR, for a row + * count it would be a sum, and this function reads the same either way. + */ +static void +anser_disp_apply_part(AnserDispChannel *chan, const AnserPayloadOps *ops, + const void *payload, Size payload_len, int total_parts, + bool cancelled) +{ + if (chan->cancelled) + return; /* already dead; nothing to do */ + + if (chan->ops == NULL) + chan->ops = ops; + else if (chan->ops != ops) + { + /* + * Two producers disagree about what this channel carries. Merging + * across types is meaningless, so give up on the channel: consumers run + * unfiltered, which is always correct. + */ + elog(LOG, "anser: cond=%u carries both '%s' and '%s'; cancelling channel", + chan->key.condition_id, chan->ops->name, ops->name); + chan->cancelled = true; + chan->complete = true; + chan->payload = NULL; + chan->payload_len = 0; + return; + } + + if (total_parts > chan->expected_parts) + { + /* + * Take the largest count any producer claims. Letting a later part + * lower it would complete the channel early and deliver a filter + * missing another segment's keys -- a false negative, which drops + * joinable rows. Disagreement means the producers computed their + * slice width differently and is worth seeing. + */ + if (chan->expected_parts != 0) + elog(LOG, "anser: cond=%u producer count changed %d -> %d", + chan->key.condition_id, chan->expected_parts, total_parts); + chan->expected_parts = total_parts; + } + + if (cancelled) + { + chan->cancelled = true; + chan->complete = true; + chan->payload = NULL; + chan->payload_len = 0; + return; + } + + if (payload == NULL || payload_len == 0) + { + /* An empty part still counts toward completion. */ + chan->parts_received++; + } + else if (chan->payload == NULL) + { + chan->payload = palloc(payload_len); + memcpy(chan->payload, payload, payload_len); + chan->payload_len = payload_len; + chan->parts_received++; + } + else if (ops->fold != NULL && + ops->fold(chan->payload, chan->payload_len, payload, payload_len)) + { + chan->parts_received++; + } + else + { + /* + * The parts cannot be merged -- for a bloom filter, sizes or filter + * parameters disagree. That should not happen (every part on a channel + * is built from the same plan parameters), but if it does the only safe + * answer is to give up on the channel. A type with no fold() reaches + * here too, which is right: it should never have carried a body. + */ + elog(LOG, "anser: incompatible '%s' part for condition %u; cancelling channel", + ops->name, chan->key.condition_id); + chan->cancelled = true; + chan->complete = true; + chan->payload = NULL; + chan->payload_len = 0; + return; + } + + if (chan->expected_parts > 0 && chan->parts_received >= chan->expected_parts) + { + chan->complete = true; + + /* + * Every part is in, so this is the first and last chance to judge the + * merged result. A type that declines it here saves each consumer both + * the delivery and the per-row probing it would have paid for. + */ + if (chan->payload != NULL && ops->worth_delivering != NULL && + !ops->worth_delivering(chan->payload, chan->payload_len)) + { + elog(LOG, "anser: merged '%s' payload for condition %u is not worth delivering; cancelling channel", + ops->name, chan->key.condition_id); + chan->cancelled = true; + chan->payload = NULL; + chan->payload_len = 0; + } + } +} + +/* Start every delivery that has not started yet; deliveries stay on the + * channel until their last chunk. */ +static void +anser_disp_deliver(AnserDispChannel *chan) +{ + /* + * Hold the delivery back, so a test can let every consumer reach its + * deadline first and then be delivered to anyway. That ordering is what + * puts a filter on a connection nobody is reading, and it cannot be + * arranged with data: the channel completes when the last part lands, + * which is normally while the consumers are still waiting for it. + */ + SIMPLE_FAULT_INJECTOR("anser_before_deliver"); + + ANSER_DEBUG("anser: QD delivering cond=%u to %d subscriber(s)", + chan->key.condition_id, list_length(chan->deliveries)); + + /* + * send_chunk() can drop the delivery it is given (write failure), so this + * cannot be a foreach(): restart from the first unstarted delivery each + * time. Every call either starts or removes one, so it terminates. + */ + for (;;) + { + ListCell *lc; + AnserDispDelivery *d = NULL; + + foreach(lc, chan->deliveries) + { + AnserDispDelivery *e = (AnserDispDelivery *) lfirst(lc); + + if (!e->started) + { + d = e; + break; + } + } + if (d == NULL) + break; + anser_disp_send_chunk(chan, d); + } +} + +/* The delivery of one connection, or NULL. */ +static AnserDispDelivery * +anser_disp_delivery_find(AnserDispChannel *chan, PGconn *conn) +{ + ListCell *lc; + + foreach(lc, chan->deliveries) + { + AnserDispDelivery *d = (AnserDispDelivery *) lfirst(lc); + + if (d->conn == conn) + return d; + } + return NULL; +} + +/* Forget one delivery: the consumer is gone or the delivery is done. */ +static void +anser_disp_delivery_drop(AnserDispChannel *chan, AnserDispDelivery *d) +{ + chan->deliveries = anser_disp_list_forget(chan->deliveries, d); + dlist_delete(&d->active_node); + pfree(d); +} + +/* + * The chunk size for one connection, chosen once when the delivery starts. + * The debug GUC wins so a test can force hundreds of round trips; otherwise + * halve the send buffer (see the constants' comment) and clamp into range. + */ +static int +anser_disp_chunk_size(PGconn *conn) +{ + int sndbuf = 0; + socklen_t len = sizeof(sndbuf); + int size; + + if (gp_anser_debug_delivery_chunk_bytes > 0) + return Max(gp_anser_debug_delivery_chunk_bytes, ANSER_CHUNK_MIN); + + /* + * conn can be NULL here: the dispatcher NULLs it after PQfinish on the + * error/FTS paths and treats that as a dead connection + * (cdbconn_isBadConnection), so do not rely on the notify handler only + * seeing live ones. + */ + if (conn != NULL && conn->sock != PGINVALID_SOCKET && + getsockopt(conn->sock, SOL_SOCKET, SO_SNDBUF, &sndbuf, &len) == 0 && + sndbuf > 0) + size = sndbuf / 2; + else + size = ANSER_CHUNK_MIN; + + return Min(ANSER_CHUNK_MAX, Max(ANSER_CHUNK_MIN, size)); +} + +/* + * Write the next chunk of one delivery as one GP_SIDEBAND_MESSAGE. + * + * The chunk is sized to fit an empty send buffer, so the flush below places + * it whole and nothing is left queued. The insurance for a wrong estimate is + * a bounded wait -- one second, no CHECK_FOR_INTERRUPTS and no dispatcher + * calls, because this runs inside the dispatcher's notify processing -- after + * which the connection goes on the pending list for AnserDispatchFlushPending() + * and the delivery is over: the consumer's own deadline ends its wait. + * + * Delivery is per consumer: a write that fails costs that one segment its + * filter and leaves the others alone. + */ +static void +anser_disp_send_chunk(AnserDispChannel *chan, AnserDispDelivery *d) +{ + PGconn *conn = d->conn; + int flags = chan->cancelled ? ANSER_WIRE_F_CANCELLED : 0; + uint32 total_len = chan->cancelled ? 0 : (uint32) chan->payload_len; + uint32 offset = chan->cancelled ? 0 : d->next_offset; + int chunk_len = chan->cancelled ? 0 : + (int) Min((uint32) d->chunk_size, total_len - offset); + int keylen = (int) strlen(chan->key.condition_key); + const char *chunk = chunk_len > 0 ? chan->payload + offset : NULL; + char paytype; + pg_crc32c crc; + bool first = !d->started; + bool last; + + if (!anser_conn_ok(conn)) + goto out; + + /* + * A channel that was cancelled before any part arrived has no type. That + * is fine: the delivery carries no body, and a consumer only checks the + * type of a body it actually received. + */ + paytype = chan->ops != NULL ? chan->ops->code : ANSER_PAYLOAD_NONE; + + crc = AnserWirePushCrc(paytype, chan->key.condition_id, + chan->key.gp_session_id, chan->key.gp_command_count, + (uint32) flags, total_len, offset, + chan->key.condition_key, keylen, + chunk, chunk_len); + + /* + * Raw binary: pqPutnchar performs no encoding conversion, so unlike the + * QE -> QD direction this needs no base64. These calls only grow the + * output buffer, so nothing here can block. + */ + if (pqPutMsgStart(GP_SIDEBAND_MESSAGE, conn) < 0 || + pqPutInt((int) paytype, 4, conn) < 0 || + pqPutInt((int) crc, 4, conn) < 0 || + pqPutInt((int) chan->key.condition_id, 4, conn) < 0 || + pqPutInt((int) chan->key.gp_session_id, 4, conn) < 0 || + pqPutInt((int) chan->key.gp_command_count, 4, conn) < 0 || + pqPutInt(flags, 4, conn) < 0 || + pqPutInt((int) total_len, 4, conn) < 0 || + pqPutInt((int) offset, 4, conn) < 0 || + pqPutInt(keylen, 4, conn) < 0 || + pqPutnchar(chan->key.condition_key, keylen, conn) < 0 || + pqPutInt(chunk_len, 4, conn) < 0 || + (chunk_len > 0 && pqPutnchar(chunk, chunk_len, conn) < 0)) + { + elog(LOG, "anser: could not deliver filter for condition %u: %s", + chan->key.condition_id, anser_conn_error(conn)); + goto out; + } + + pqPutMsgEndNoAutoFlush(conn); + if (pqFlushNonBlocking(conn) < 0) + { + elog(LOG, "anser: could not deliver filter for condition %u: %s", + chan->key.condition_id, anser_conn_error(conn)); + goto out; + } + + if (conn->outCount > 0) + { + /* + * The chunk was meant to fit the buffer whole. It did not, so give + * the peer a bounded moment to drain: it just asked for this chunk, + * so it is alive and reading. Over the deadline the connection is + * torn and goes on the pending list. + */ + instr_time start; + instr_time now; + + INSTR_TIME_SET_CURRENT(start); + + while (conn->outCount > 0) + { + if (pqFlushNonBlocking(conn) < 0) + break; + if (conn->outCount == 0) + break; + INSTR_TIME_SET_CURRENT(now); + INSTR_TIME_SUBTRACT(now, start); + if (INSTR_TIME_GET_MILLISEC(now) >= 1000) + break; + + (void) WaitLatchOrSocket(MyLatch, + WL_LATCH_SET | WL_SOCKET_WRITEABLE | + WL_TIMEOUT | WL_EXIT_ON_PM_DEATH, + anser_conn_socket(conn), 100L /* ms */, + PG_WAIT_EXTENSION); + ResetLatch(MyLatch); + } + + if (conn->outCount > 0 && + !list_member_ptr(AnserDispPendingPush, conn)) + { + MemoryContext savecxt = MemoryContextSwitchTo(TopMemoryContext); + + AnserDispPendingPush = lappend(AnserDispPendingPush, conn); + MemoryContextSwitchTo(savecxt); + } + if (conn->outCount > 0) + { + elog(LOG, "anser: cond=%u chunk did not flush in 1 s, %d bytes queued; ending the delivery", + chan->key.condition_id, conn->outCount); + goto out; + } + } + + d->started = true; + d->next_offset = offset + (uint32) chunk_len; + + /* Log only the first and last chunk; a delivery is hundreds of them. */ + last = chan->cancelled || d->next_offset >= total_len; + if (first || last) + ANSER_DEBUG("anser: QD pushed cond=%u type=%c to seg%d chunk=[%u,%u) of %u cancelled=%d chunk_size=%d", + chan->key.condition_id, paytype, d->segindex, + offset, d->next_offset, total_len, + chan->cancelled ? 1 : 0, d->chunk_size); + if (last) + { + /* + * Counted only when a filter really travelled: a cancelled delivery + * tells the consumer to run unfiltered, so it must not look like an + * arrival to a test. + */ + if (!chan->cancelled) + AnserDispDeliveriesCompleted++; + goto out; + } + return; + +out: + anser_disp_delivery_drop(chan, d); +} + +/* + * Parse a QE -> QD payload: fixed-width header, then the condition key, then + * the base64 body. ansersideband.h documents the layout and the reasoning; the + * checks here are what make it trustworthy: + * + * - the tag must match, so a message from a different format is not + * misinterpreted as this one; + * - the header is a constant length, so the key and the body start at offsets + * that no payload byte can influence; + * - the two lengths must account for the message exactly, which catches a + * truncated or over-long message before any of it is used; + * - the CRC is verified once the body has been decoded, in the caller. + * + * The payload type is carried through as the raw byte; resolving it against the + * registry is the caller's job, so that an unregistered type is reported as + * exactly that rather than as a framing error. + */ +bool +AnserWireParse(const char *msg, AnserWireMsg *out) +{ + Size msglen = strlen(msg); + char kind; + char paytype; + uint32 ssid, + ccnt, + condid, + part, + total, + flags, + keylen, + bodylen, + crc; + + if (msglen < ANSER_WIRE_HDR_LEN) + return false; + if (strncmp(msg, ANSER_WIRE_TAG " ", sizeof(ANSER_WIRE_TAG)) != 0) + return false; + + if (sscanf(msg, ANSER_WIRE_HDR_SCANF, + &kind, &paytype, &ssid, &ccnt, &condid, &part, &total, &flags, + &keylen, &bodylen, &crc) != 11) + return false; + + if (kind != ANSER_WIRE_KIND_PART && + kind != ANSER_WIRE_KIND_SUBSCRIBE && + kind != ANSER_WIRE_KIND_UNSUBSCRIBE && + kind != ANSER_WIRE_KIND_NEXT) + return false; + if (keylen >= ANSER_CONDITION_KEY_SIZE) + return false; + if (msglen != ANSER_WIRE_HDR_LEN + (Size) keylen + (Size) bodylen) + return false; + + MemSet(out, 0, sizeof(*out)); + out->wire = msg; + out->kind = kind; + out->payload_type = paytype; + out->key.gp_session_id = (int) ssid; + out->key.gp_command_count = (int) ccnt; + out->key.condition_id = condid; + memcpy(out->key.condition_key, msg + ANSER_WIRE_HDR_LEN, keylen); + out->key.condition_key[keylen] = '\0'; + out->key_len = (int) keylen; + out->part_index = (int) part; + out->total_parts = (int) total; + out->flags = (int) flags; + out->body = msg + ANSER_WIRE_HDR_LEN + keylen; + out->body_len = (int) bodylen; + out->crc = crc; + + return true; +} + +/* + * Verify a parsed message against its checksum. + * + * 'body' is the decoded body, which is what the producer checksummed -- so + * where it is covered this validates the base64 round trip as well. Pass + * NULL/0 for a message that has no body, and note that a type which opts out of + * checksumming its body still has its header and key checked. + */ +bool +AnserWireCheckCrc(const AnserWireMsg *msg, const void *body, Size body_len) +{ + pg_crc32c crc; + + INIT_CRC32C(crc); + COMP_CRC32C(crc, msg->wire, ANSER_WIRE_CRC_OFFSET); + COMP_CRC32C(crc, msg->key.condition_key, msg->key_len); + if (body != NULL && body_len > 0 && + AnserPayloadChecksumsBody(msg->payload_type)) + COMP_CRC32C(crc, body, body_len); + FIN_CRC32C(crc); + + return (uint32) crc == msg->crc; +} + +/* + * Coordinator-local producer. + * + * A producer running on the QD has no dispatch connection to itself, so it + * folds straight into the same channel table the hook uses. + */ +bool +AnserDispatchLocalPublish(const AnserChannelKey *channel_key, char payload_type, + uint32 part_index, uint32 total_parts, + const void *payload, Size payload_len, + bool cancelled) +{ + const AnserPayloadOps *ops; + AnserDispChannel *chan; + MemoryContext oldcxt; + + if (channel_key == NULL) + return false; + if (!cancelled && payload_len > (Size) gp_anser_max_info_size) + cancelled = true; + + ops = AnserPayloadLookup(payload_type); + if (ops == NULL) + { + elog(LOG, "anser: unknown payload type '%c' for condition %u; ignoring", + payload_type, channel_key->condition_id); + return false; + } + + anser_disp_init(); + oldcxt = MemoryContextSwitchTo(AnserDispContext); + + chan = anser_disp_lookup(channel_key, true); + if (chan != NULL) + { + anser_disp_apply_part(chan, ops, payload, payload_len, + (int) total_parts, cancelled); + ANSER_DEBUG("anser: QD local %s part cond=%u (part %u of %u) %d/%d bytes=%zu -> %s", + ops->name, channel_key->condition_id, part_index, total_parts, + chan->parts_received, chan->expected_parts, payload_len, + chan->cancelled ? "cancelled" : + chan->complete ? "complete" : "collecting"); + if (chan->complete) + anser_disp_deliver(chan); + } + + MemoryContextSwitchTo(oldcxt); + return chan != NULL; +} + +/* + * Coordinator-local consumer. + * + * Does not wait: on the coordinator the producer side of the join has already + * run by the time the probe side asks for the filter, so either the channel is + * complete or it never will be (a squelched producer, say) and we fail open. + */ +bool +AnserDispatchLocalConsume(const AnserChannelKey *channel_key, char payload_type, + void **payload, Size *payload_len, bool *cancelled) +{ + AnserDispChannel *chan; + + if (payload != NULL) + *payload = NULL; + if (payload_len != NULL) + *payload_len = 0; + if (cancelled != NULL) + *cancelled = false; + + if (channel_key == NULL || AnserDispChannels == NULL) + return false; + + chan = anser_disp_lookup(channel_key, false); + if (chan == NULL || !chan->complete) + return false; + + if (chan->cancelled || chan->payload == NULL) + { + if (cancelled != NULL) + *cancelled = true; + return false; + } + + /* Same check the segment path makes on delivery; see anser_inbox_take. */ + if (chan->ops == NULL || chan->ops->code != payload_type) + { + elog(WARNING, "anser: condition %u holds payload type '%c', expected '%c'", + channel_key->condition_id, + chan->ops != NULL ? chan->ops->code : '?', payload_type); + if (cancelled != NULL) + *cancelled = true; + return false; + } + + if (payload != NULL) + { + *payload = palloc(chan->payload_len); + memcpy(*payload, chan->payload, chan->payload_len); + } + if (payload_len != NULL) + *payload_len = chan->payload_len; + + return true; +} diff --git a/gpcontrib/anser/src/anserfilter.c b/gpcontrib/anser/src/anserfilter.c new file mode 100644 index 00000000000..1441bb0275a --- /dev/null +++ b/gpcontrib/anser/src/anserfilter.c @@ -0,0 +1,376 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserfilter.c + * Bloom-filter payload helpers for Anser channels. + * + * IDENTIFICATION + * gpcontrib/anser/src/anserfilter.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "anser.h" +#include "anserfilter.h" +#include "common/hashfn.h" +#include "port/pg_bitutils.h" + +static bool AnserBloomValidateHeader(const AnserBloomPartHeader *header, + Size payload_len); + +/* + * Map Anser's payload budget to bloom_create's work_mem (KB): the space left for + * the bitset after the part header. Callers must have checked that + * max_payload_bytes leaves room for a header. + */ +static int +AnserBloomWorkMemKb(Size max_payload_bytes) +{ + return (int) ((max_payload_bytes - sizeof(AnserBloomPartHeader)) / 1024); +} + +uint64 +AnserBloomSeed(const char *condition_key) +{ + if (condition_key == NULL) + return 0; + + return hash_bytes_extended((const unsigned char *) condition_key, + strlen(condition_key), 0); +} + +/* + * Build an empty bloom filter for a channel from the caller's parameters. + * + * The producer builds its filter here; the consumer rebuilds an identical one + * from the received bitset (AnserBloomDeserializePart -> bloom_create_from_bitset) + * using the SAME (total_elems, max_payload_bytes, seed) -- carried in the plan + * node's custom_private and derived from the shared condition key -- so every + * segment and the consumer realize a byte-for-byte identical filter shape. This + * is why the serialized part header does not need to carry the bitset parameters: + * the reconstructing side already knows them. + * + * Sizing is bloom_create's, but the decision to keep the result is ours. Two + * ways it can come back unusable: + * + * - Too large to send. bloom_create ends with Max(1 MB, bitset), a floor + * that overrides the work_mem cap, so a payload cap below 1 MB yields a + * bitset that cannot be shipped. + * - Too thin to help. bloom_create has no minimum density (unlike + * bloom_create_aggresive, which refuses below 1.6 bits/key), so a large + * enough total_elems against a fixed cap gets you a filter that matches + * almost everything. + * + * Both are judged from the filter's own accessors rather than by re-deriving + * bloom_create's arithmetic here: my_bloom_power() and the floor are private to + * lib/bloomfilter.c, and a copy of them would go quietly out of date on a + * kernel rebase. The cost of learning the answer this way is one palloc0 that + * is immediately freed -- against a build-side scan, which is what returning + * NULL here avoids, that is nothing. + */ +bloom_filter * +AnserBloomCreate(int64 total_elems, Size max_payload_bytes, uint64 seed) +{ + bloom_filter *filter; + Size serialized; + double bits_per_key; + + /* + * These were an Assert on the grounds that internal callers always pass a + * payload cap with room for a header. They are checks because the caller + * is a plan node, and its parameters arrived from the coordinator in + * custom_private -- not somewhere an assertion belongs. The cap also + * cannot merely be >= the header: AnserBloomWorkMemKb subtracts the header + * from a Size, so a smaller cap would underflow to an enormous work_mem. + */ + if (total_elems <= 0 || max_payload_bytes <= sizeof(AnserBloomPartHeader)) + return NULL; + + filter = bloom_create(total_elems, AnserBloomWorkMemKb(max_payload_bytes), + seed); + serialized = AnserBloomSerializedSize(filter); + bits_per_key = (double) bloom_total_bits(filter) / (double) total_elems; + + if (serialized > max_payload_bytes) + { + ANSER_DEBUG("anser: not building a filter: smallest bitset serializes to %zu bytes, cap is %zu", + serialized, max_payload_bytes); + bloom_free(filter); + return NULL; + } + + if (bits_per_key < ANSER_BLOOM_MIN_BITS_PER_KEY) + { + ANSER_DEBUG("anser: not building a filter for %ld key(s) in %zu bytes: %.2f bits/key, below the %.1f floor", + (long) total_elems, max_payload_bytes, bits_per_key, + ANSER_BLOOM_MIN_BITS_PER_KEY); + bloom_free(filter); + return NULL; + } + + return filter; +} + +/* + * Is this serialized payload still selective enough to be worth delivering? + * + * Counts the set bits in the wire form, so the coordinator can ask it of a + * merged accumulator without rebuilding a filter -- which is the point, because + * the union of N parts is denser than any one of them. Three parts at 60% fill + * OR together to as much as 94%, so the producers' own checks do not protect + * consumers from the merged result; this is the check that does. + * + * Fill, not false positive rate: the serialized part carries no hash count, so + * fill is all there is to go on here. ANSER_BLOOM_MERGED_MAX_FILL is set + * accordingly -- high enough that it cannot reject a filter that is still good + * at a large k, which makes it a backstop rather than a tuning knob. + */ +bool +AnserBloomPartWorthSending(const void *payload, Size payload_len) +{ + const char *bits; + Size bitset_bytes; + uint64 bits_set; + double fill; + + if (!AnserBloomLooksLikePart(payload, payload_len)) + return false; + + bitset_bytes = payload_len - sizeof(AnserBloomPartHeader); + if (bitset_bytes == 0) + return false; + + bits = (const char *) payload + sizeof(AnserBloomPartHeader); + bits_set = pg_popcount(bits, (int) bitset_bytes); + fill = (double) bits_set / (double) (bitset_bytes * BITS_PER_BYTE); + + if (fill > ANSER_BLOOM_MERGED_MAX_FILL) + { + ANSER_DEBUG("anser: merged filter is %.1f%% full, above the %.0f%% limit; not worth delivering", + fill * 100.0, ANSER_BLOOM_MERGED_MAX_FILL * 100.0); + return false; + } + + return true; +} + +Size +AnserBloomSerializedSize(const bloom_filter *filter) +{ + if (filter == NULL) + return 0; + + return sizeof(AnserBloomPartHeader) + bloom_bitset_bytes(filter); +} + +/* + * Does this payload look like a serialized bloom part? Used by the in-place fold + * to confirm both the accumulator and the incoming payload are well-formed parts + * before OR-ing their bitsets. A false positive is effectively impossible: a + * part must carry the ABF1 magic, a known version, and sane part counts. + */ +bool +AnserBloomLooksLikePart(const void *payload, Size payload_len) +{ + if (payload == NULL || payload_len < sizeof(AnserBloomPartHeader)) + return false; + + return AnserBloomValidateHeader((const AnserBloomPartHeader *) payload, + payload_len); +} + +/* + * Fold an incoming part into an accumulator part IN PLACE. + * + * When a merged part and the incoming part are the same serialized size (they + * share bitset params derived from the condition key), the union is a pure + * bitwise OR of the two bitsets -- no reallocation. This mutates `acc` + * directly, so the buffer must be one the caller owns; the coordinator's is + * the channel payload, which lives in its own backend-private memory and is + * only ever touched from the dispatcher loop. + * + * The header is left alone, total_parts included. It names how many producers + * the channel is waiting for, which every part on the channel agrees on and + * folding does not change; the count of parts already folded is the + * coordinator's business and it keeps that in the channel, not in the payload. + * + * Returns true only when the in-place union applied. It returns false -- and + * leaves `acc` untouched (all checks run before any write) -- when the union + * cannot be done by raw OR: sizes differ, either side is not a valid part, or + * the filter parameters disagree. Since every part on a channel shares the same + * (condition-key-derived) parameters and therefore the same serialized size, the + * first part is stored verbatim and every later part folds in here; a false + * return means a malformed/mismatched payload and the caller cancels the channel. + */ +bool +AnserBloomFoldPartInPlace(void *acc, Size acc_len, + const void *part, Size part_len) +{ + unsigned char *abits; + const unsigned char *pbits; + Size bitset_bytes; + Size i; + + /* Only a same-sized, valid part-vs-part union can be done by raw OR. */ + if (acc_len != part_len || + !AnserBloomLooksLikePart(acc, acc_len) || + !AnserBloomLooksLikePart(part, part_len)) + return false; + + /* + * Equal serialized size is sufficient: every part on a channel is built by + * bloom_create from the same (condition-key-derived) parameters, so equal + * length implies an identical bitset shape. The bitset is whatever follows + * the header, so its length is the payload length minus the header. + */ + bitset_bytes = acc_len - sizeof(AnserBloomPartHeader); + abits = (unsigned char *) acc + sizeof(AnserBloomPartHeader); + pbits = (const unsigned char *) part + sizeof(AnserBloomPartHeader); + + for (i = 0; i < bitset_bytes; i++) + abits[i] |= pbits[i]; + + return true; +} + +/* + * Serialize one filter as a wire part: an AnserBloomPartHeader followed by the + * raw bitset. Fails (returns false) on bogus arguments or a too-small buffer. + */ +bool +AnserBloomSerializePart(const bloom_filter *filter, uint32 part_index, + uint32 total_parts, void *buffer, Size buffer_size, + Size *payload_len) +{ + AnserBloomPartHeader header; + Size bitset_bytes; + Size total_len; + + if (payload_len != NULL) + *payload_len = 0; + + if (filter == NULL || buffer == NULL || total_parts == 0 || + part_index >= total_parts) + return false; + + bitset_bytes = bloom_bitset_bytes(filter); + total_len = sizeof(AnserBloomPartHeader) + bitset_bytes; + if (buffer_size < total_len) + return false; + + MemSet(&header, 0, sizeof(header)); + header.magic = ANSER_BLOOM_PART_MAGIC; + header.version = ANSER_BLOOM_PART_VERSION; + header.part_index = part_index; + header.total_parts = total_parts; + + memcpy(buffer, &header, sizeof(header)); + memcpy((char *) buffer + sizeof(header), bloom_bitset_data(filter), + bitset_bytes); + + if (payload_len != NULL) + *payload_len = total_len; + return true; +} + +/* + * Rebuild the filter from a received merged part. + * + * The bitset parameters are NOT taken from the wire header: the caller passes + * the same (total_elems, max_payload_bytes, seed) used to produce the filter -- + * it holds them in the plan node, so both ends agree by construction. We rebuild + * the empty filter from those, then load the received bitset into it. The wire + * header is still validated (magic/version/counts) and, crucially, the received + * bitset length must exactly match the size the local parameters imply; any + * mismatch (version/parameter skew, truncation) returns NULL so the consumer + * fails open rather than loading a wrongly-shaped bitset. part_index/total_parts + * are surfaced from the header for diagnostics. + */ +bloom_filter * +AnserBloomDeserializePart(const void *payload, Size payload_len, + int64 total_elems, Size max_payload_bytes, uint64 seed, + uint32 *part_index, uint32 *total_parts) +{ + const AnserBloomPartHeader *header; + bloom_filter *filter; + + if (part_index != NULL) + *part_index = 0; + if (total_parts != NULL) + *total_parts = 0; + + if (payload == NULL || payload_len < sizeof(AnserBloomPartHeader)) + return NULL; + + header = (const AnserBloomPartHeader *) payload; + if (!AnserBloomValidateHeader(header, payload_len)) + return NULL; + + if (max_payload_bytes <= sizeof(AnserBloomPartHeader)) + return NULL; + if (total_elems < 1) + total_elems = 1; + + /* + * Build the filter straight from the received bitset, sized by our own + * parameters. bloom_create_from_bitset returns NULL unless the received + * length is exactly the size those parameters imply, so the fail-open + * described above happens here. A filter is thus only ever populated at + * construction and, from then on, only grown by add/union -- never re-set. + */ + filter = bloom_create_from_bitset(total_elems, + AnserBloomWorkMemKb(max_payload_bytes), + seed, + (const unsigned char *) payload + + sizeof(AnserBloomPartHeader), + payload_len - sizeof(AnserBloomPartHeader)); + if (filter == NULL) + return NULL; + + if (part_index != NULL) + *part_index = header->part_index; + if (total_parts != NULL) + *total_parts = header->total_parts; + return filter; +} + +/* + * Validate the wire framing of a part header. The bitset parameters are not + * carried on the wire (both ends rebuild the filter from the shared plan + * parameters), so this only checks the framing: magic/version, a sane fold + * count, and that the payload carries a header plus at least some bitset. The + * authoritative size check -- that the received bitset matches the size the local + * parameters imply -- is done in AnserBloomDeserializePart. + */ +static bool +AnserBloomValidateHeader(const AnserBloomPartHeader *header, Size payload_len) +{ + if (header == NULL) + return false; + + if (header->magic != ANSER_BLOOM_PART_MAGIC || + header->version != ANSER_BLOOM_PART_VERSION) + return false; + + if (header->total_parts == 0 || header->part_index >= header->total_parts) + return false; + + return payload_len > sizeof(AnserBloomPartHeader); +} diff --git a/gpcontrib/anser/src/anserinit.c b/gpcontrib/anser/src/anserinit.c new file mode 100644 index 00000000000..c738e22bb75 --- /dev/null +++ b/gpcontrib/anser/src/anserinit.c @@ -0,0 +1,439 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserinit.c + * Module entry point: GUCs and the core hooks Anser hangs off. + * + * Anser is a shared_preload_libraries extension. Everything it needs from the + * server is reached through an existing extensibility point: + * + * planner_hook runtime-filter injection + * RegisterCustomScanMethods the injected plan nodes + * cdbdisp_notify_hook parts arriving from segments + * ExecutorRun/Finish_hook knowing whether a query is still executing + * ExecutorEnd_hook dropping a query's channels + * + * It must be preloaded, because a segment backend deserializing a dispatched + * plan has no opportunity to load the library first. + * + * IDENTIFICATION + * gpcontrib/anser/src/anserinit.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "access/xact.h" +#include "anser.h" +#include "anserplan.h" +#include "ansersideband.h" +#include "cdb/cdbdisp.h" +#include "cdb/cdbvars.h" +#include "executor/executor.h" +#include "libpq/libpq.h" +#include "miscadmin.h" +#include "optimizer/planner.h" +#include "utils/guc.h" + +PG_MODULE_MAGIC; + +void _PG_init(void); + +bool gp_anser_enable = false; +bool gp_anser_runtime_filter = false; +bool gp_anser_debug = false; +int gp_anser_max_info_size = 64 * 1024 * 1024 + 1024 * 1024; +int gp_anser_timeout_ms = 10000; +int gp_anser_debug_min_filter_bytes = 0; +int gp_anser_debug_delivery_chunk_bytes = 0; + +static void anser_define_gucs(void); +static PlannedStmt *anser_planner(Query *parse, const char *query_string, + int cursorOptions, ParamListInfo boundParams, + OptimizerOptions *optimizer_options); + +static planner_hook_type prev_planner_hook = NULL; +static ExecutorRun_hook_type prev_ExecutorRun_hook = NULL; +static ExecutorFinish_hook_type prev_ExecutorFinish_hook = NULL; +static ExecutorEnd_hook_type prev_ExecutorEnd_hook = NULL; +static cdbdisp_notify_hook_type prev_cdbdisp_notify_hook = NULL; + +/* + * How many queries are executing right now. A query's channels may only be + * dropped when nothing is left running, and the count has to be taken over + * execution rather than over ExecutorEnd: a statement run through SPI from a + * function body goes through its whole Start/Run/End inside the *outer* + * query's Run, so by the time the inner ExecutorEnd fires the outer one has + * not entered its own End yet. Counting ends alone makes that inner End look + * outermost, and dropping channels there takes the running query's filters + * with it -- the exchange still happens, but every consumer then fails open. + * + * Nested execution is nested inside Run (and inside Finish, where AFTER + * triggers run), which is why those are the two that count. ExecutorEnd only + * reads the result: a query's own End always runs after its own Run returned, + * so an outermost query still sees zero. + */ +static int anser_exec_nesting = 0; + +static bool anser_notify_hook(struct CdbDispatchResult *dispatchResult, + struct pgNotify *notify); +static void anser_executor_run(QueryDesc *queryDesc, ScanDirection direction, + uint64 count, bool execute_once); +static void anser_executor_finish(QueryDesc *queryDesc); +static void anser_executor_end(QueryDesc *queryDesc); +static void anser_xact_callback(XactEvent event, void *arg); +static void anser_subxact_callback(SubXactEvent event, SubTransactionId mySubid, + SubTransactionId parentSubid, void *arg); + +void +_PG_init(void) +{ + anser_define_gucs(); + + /* + * Only a preloaded library can be relied on to have installed its hooks in + * every backend. Loaded any other way, Anser stays inert: the GUCs exist + * (so a stray setting is not an error) but nothing is wired up. + */ + if (!process_shared_preload_libraries_in_progress) + return; + + prev_planner_hook = planner_hook; + planner_hook = anser_planner; + + /* + * Channels live in backend memory on both ends, so they need a point to be + * dropped. ExecutorEnd covers the normal path; the transaction callbacks + * catch queries that end by erroring -- the subtransaction one because a + * statement aborted inside a savepoint gets no ExecutorEnd (PortalCleanup + * skips it for a failed portal), and its parked deliveries would otherwise + * sit in the inbox until the top-level statement ends. + */ + prev_ExecutorRun_hook = ExecutorRun_hook; + ExecutorRun_hook = anser_executor_run; + prev_ExecutorFinish_hook = ExecutorFinish_hook; + ExecutorFinish_hook = anser_executor_finish; + prev_ExecutorEnd_hook = ExecutorEnd_hook; + ExecutorEnd_hook = anser_executor_end; + RegisterXactCallback(anser_xact_callback, NULL); + RegisterSubXactCallback(anser_subxact_callback, NULL); + + /* + * Handle Anser notifies arriving from QEs on the dispatch connections. + * Installed unconditionally: it is inert until a segment sends one, and a + * QE that never dispatches never calls it. + */ + prev_cdbdisp_notify_hook = cdbdisp_notify_hook; + cdbdisp_notify_hook = anser_notify_hook; + + /* + * The producer and consumer nodes travel to the segments inside dispatched + * plans, so every backend must be able to resolve their CustomScan methods + * by name. Registering here covers QD and QE alike. + */ + AnserRegisterRuntimeFilterMethods(); +} + +/* + * The subsystem's GUCs. All are "anser.*"-qualified because they belong to a + * loadable module; the C variables keep their gp_anser_ names. + */ +static void +anser_define_gucs(void) +{ + DefineCustomBoolVariable("anser.enable", + "Enables the Anser adaptive information sharing subsystem.", + "When disabled, the plan pass never injects anything and no filters are exchanged.", + &gp_anser_enable, + false, + PGC_SIGHUP, + 0, + NULL, NULL, NULL); + + DefineCustomBoolVariable("anser.runtime_filter", + "Enables injection of Anser runtime bloom filters into plans.", + "Requires anser.enable; the plan pass is a no-op otherwise.", + &gp_anser_runtime_filter, + false, + PGC_USERSET, + GUC_EXPLAIN, + NULL, NULL, NULL); + + /* + * GUC_GPDB_NEED_SYNC on everything a QE reads. A custom GUC gets + * GUC_GPDB_NO_SYNC by default -- gpdb_assign_sync_flag() says so in as many + * words for "the third-part libraries gucs introduced by customer" + * (guc_gp.c) -- and a QE created after the SET then never receives the + * value. The QEs alive at the time do get it, because the SET itself is + * dispatched to them as a command, so the setting appears to work right up + * until a query needs a wider gang and the new processes fall back to the + * compiled-in default. That is not a setting anyone can reason about. + */ + /* + * Resource and log-noise knobs are superuser-only: an unprivileged role + * must not be able to force large allocations on every segment or fill the + * server log. runtime_filter and timeout_ms stay PGC_USERSET -- they only + * shape the caller's own queries, like any other planner GUC. + */ + DefineCustomBoolVariable("anser.debug", + "Logs each step of the Anser filter exchange.", + "Traces publish, merge, delivery and receive in the log of the process each happens in.", + &gp_anser_debug, + false, + PGC_SUSET, + GUC_GPDB_NEED_SYNC, + NULL, NULL, NULL); + + DefineCustomIntVariable("anser.max_info_size", + "Sets the maximum byte size of one Anser information record.", + "Caps the serialized payload a channel may carry, and with it the effective bloom-filter size. The default holds a full 64 MB bitset plus its serialized-part header. Both ends read it -- the coordinator to size the filter and to bound what it accepts, a segment to bound what it sends and receives -- so it is synced to the segments; changing it on a live session is still best avoided, because a gang busy with an open cursor does not receive the new value.", + &gp_anser_max_info_size, + 64 * 1024 * 1024 + 1024 * 1024, 1, + /* + * Not INT_MAX: every payload is held whole in a + * single palloc on each end -- the coordinator's + * merged payload and the consumer's assembly + * buffer, plus the base64 copy of a part on the + * producer -- so it must stay inside + * MaxAllocSize. A larger setting buys a + * configuration that cannot work, and used to + * overflow the cap computed from it into a + * negative int. Refusing it at SET is the honest + * place to say so. + */ + (int) (PQ_LARGE_MESSAGE_LIMIT - ANSER_SIDEBAND_FRAME_ROOM), + PGC_SUSET, + GUC_GPDB_NEED_SYNC, + NULL, NULL, NULL); + + DefineCustomIntVariable("anser.timeout_ms", + "Sets how long an Anser consumer waits for its filter.", + "On expiry the consumer runs unfiltered. What the wait covers depends on the plan: where the probe side also travels through a Motion the consumer sits in the probe scan's own slice and runs alongside the build, so it waits out the whole build side; where the probe side is local the hash is already built by the time the consumer runs, leaving only the coordinator's fold and delivery. The default is a budget for an exchange that has gone wrong -- a squelched producer never publishes at all -- rather than a measurement of a healthy one.", + &gp_anser_timeout_ms, + 10000, 0, INT_MAX, + PGC_USERSET, + GUC_UNIT_MS | GUC_GPDB_NEED_SYNC, + NULL, NULL, NULL); + + /* + * Read on the coordinator at plan time only, and the size it forces + * travels in the plan -- so no NEED_SYNC: a QE never consults it. + */ + DefineCustomIntVariable("anser.debug_min_filter_bytes", + "Sets a floor on the bloom-filter bitset size, for testing.", + "0 (the default) sizes filters from the plan estimate; anything larger forces at least that many bytes, still capped by anser.max_info_size. A bigger filter over the same keys stays a superset of them, so this can only cost memory and wire time, never correctness.", + &gp_anser_debug_min_filter_bytes, + 0, 0, INT_MAX, + PGC_SUSET, + 0, + NULL, NULL, NULL); + + /* + * Read on the coordinator at delivery time only, and the choice never + * travels -- a QE reassembles whatever chunk sizes arrive -- so no + * NEED_SYNC: a QE never consults it. + */ + DefineCustomIntVariable("anser.debug_delivery_chunk_bytes", + "Sets the delivery chunk size, for testing.", + "0 (the default) sizes a chunk from the connection's send buffer; anything larger forces exactly that chunk size (the built-in floor still applies). Small values turn one delivery into hundreds of NEXT round trips.", + &gp_anser_debug_delivery_chunk_bytes, + 0, 0, INT_MAX, + PGC_SUSET, + 0, + NULL, NULL, NULL); + + MarkGUCPrefixReserved("anser"); +} + +/* + * Plan the query as usual, then hand the finished tree to the runtime-filter + * pass. Wrapping the hook this way covers both optimizers, because ORCA is + * dispatched from inside standard_planner(). + * + * Planning counts towards anser_exec_nesting for the same reason execution + * does: constant folding an immutable function can run a query through SPI, + * and that query would otherwise reach anser_executor_end believing it is the + * outermost one -- and take the wholesale reset with it. One counter covering + * both phases is what PostgreSQL settled on for the same problem in 76db9cb6368. + */ +static PlannedStmt * +anser_planner(Query *parse, const char *query_string, int cursorOptions, + ParamListInfo boundParams, OptimizerOptions *optimizer_options) +{ + PlannedStmt *result; + + anser_exec_nesting++; + PG_TRY(); + { + if (prev_planner_hook) + result = prev_planner_hook(parse, query_string, cursorOptions, + boundParams, optimizer_options); + else + result = standard_planner(parse, query_string, cursorOptions, + boundParams, optimizer_options); + + AnserApplyRuntimeFilters(result); + } + PG_FINALLY(); + { + anser_exec_nesting--; + } + PG_END_TRY(); + + return result; +} + +/* + * Dispatch a QE notify to Anser, then to whoever held the hook before us. + * + * cdbdisp_notify_hook is a single pointer, so an extension that overwrites it + * silently swallows every notify the other one was waiting for. Anser's + * handler already declines anything that is not addressed to it -- it returns + * false for any channel other than "anser_rf" -- so all that is missing is the + * fallthrough. Returning false from here means nobody claimed the notify and + * the dispatcher should handle it itself. + */ +static bool +anser_notify_hook(struct CdbDispatchResult *dispatchResult, + struct pgNotify *notify) +{ + if (AnserDispatchNotifyHandler(dispatchResult, notify)) + return true; + + if (prev_cdbdisp_notify_hook) + return prev_cdbdisp_notify_hook(dispatchResult, notify); + + return false; +} + +/* Count a query as executing for as long as it is running. */ +static void +anser_executor_run(QueryDesc *queryDesc, ScanDirection direction, + uint64 count, bool execute_once) +{ + anser_exec_nesting++; + PG_TRY(); + { + if (prev_ExecutorRun_hook) + prev_ExecutorRun_hook(queryDesc, direction, count, execute_once); + else + standard_ExecutorRun(queryDesc, direction, count, execute_once); + } + PG_FINALLY(); + { + anser_exec_nesting--; + } + PG_END_TRY(); +} + +/* The same, for the phase that runs AFTER triggers. */ +static void +anser_executor_finish(QueryDesc *queryDesc) +{ + anser_exec_nesting++; + PG_TRY(); + { + if (prev_ExecutorFinish_hook) + prev_ExecutorFinish_hook(queryDesc); + else + standard_ExecutorFinish(queryDesc); + } + PG_FINALLY(); + { + anser_exec_nesting--; + } + PG_END_TRY(); +} + +/* + * Drop the transport's per-query state. + * + * The channel key carries gp_command_count, so a query can take its own + * channels with it. That is what keeps a PL/pgSQL body from accumulating one + * merged filter per statement until the outermost one ends: inside SPI, + * anser_exec_nesting never comes back to zero, so the sweep below does not run. + * + * The global is this query's own count whenever nothing was planned or + * executed since CreateQueryDesc() assigned it -- true for the inner queries + * that do the accumulating. Where it has moved on (a query that itself ran + * nested ones, a PL/pgSQL cursor whose portal outlived other statements) the + * count handed over belongs to somebody else, possibly to a query that is + * still running: AnserDispatchResetCommand() therefore takes only channels + * whose exchange is over, and leaves the rest to the sweep. + * + * See anser_exec_nesting for the sweep: it is wholesale, so a query ending + * while another is still running must not trigger it. + */ +static void +anser_executor_end(QueryDesc *queryDesc) +{ + /* + * Poison whatever is still pending. Connection destruction already + * forgets its own references through libpq events, so this only sees + * live connections -- normally the main plan's, which standard_ExecutorEnd + * releases after this returns. + */ + AnserDispatchResetCommand(gp_command_count); + AnserDispatchFlushPending(gp_anser_timeout_ms); + + if (prev_ExecutorEnd_hook) + prev_ExecutorEnd_hook(queryDesc); + else + standard_ExecutorEnd(queryDesc); + + if (anser_exec_nesting == 0) + AnserSidebandResetAll(); +} + +static void +anser_xact_callback(XactEvent event, void *arg) +{ + /* + * Deliberately does not touch anser_exec_nesting. PG_FINALLY unwinds it + * on the error path, and XACT_EVENT_ABORT is only reached from + * AbortTransaction() at a statement boundary, by which point every + * executor frame is gone -- so there is nothing here to correct. Forcing + * it to zero would instead risk driving it negative if a frame ever did + * outlive the callback, and a negative count never compares equal to zero + * again: the resets below would stop happening for the rest of the + * session. auto_explain and pg_stat_statements keep their counters the + * same way, on PG_FINALLY alone. + */ + if (event == XACT_EVENT_ABORT || event == XACT_EVENT_PARALLEL_ABORT) + AnserSidebandResetAll(); +} + +/* + * The same, one transaction level down. A statement that errors inside a + * savepoint or a PL/pgSQL exception block is unwound without an ExecutorEnd: + * AtSubAbort_Portals runs PortalCleanup, which skips the executor for a failed + * portal (portalcmds.c). Anything it parked in the QE inbox -- up to + * gp_anser_max_info_size a piece -- would otherwise survive until the + * top-level statement ends, and a PL/pgSQL loop with an exception block could + * pile them up without bound. The command-count match makes such an entry + * unclaimable, so this is memory hygiene, not correctness; only the inbox is + * dropped, the dispatch channels belong to the top-level statement. + */ +static void +anser_subxact_callback(SubXactEvent event, SubTransactionId mySubid, + SubTransactionId parentSubid, void *arg) +{ + if (event == SUBXACT_EVENT_ABORT_SUB) + AnserSidebandResetInbox(); +} diff --git a/gpcontrib/anser/src/anserpayload.c b/gpcontrib/anser/src/anserpayload.c new file mode 100644 index 00000000000..2926a15a3e4 --- /dev/null +++ b/gpcontrib/anser/src/anserpayload.c @@ -0,0 +1,108 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserpayload.c + * The registry of payload types. + * + * One row per kind of runtime information a channel can carry. See + * anserpayload.h for what a row means and how to add one. + * + * IDENTIFICATION + * gpcontrib/anser/src/anserpayload.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "anserfilter.h" +#include "anserpayload.h" + +/* + * Indexed by the wire byte itself, so a lookup is one load and registering a + * type is one line (the pattern syscache.c:154 uses for its cache + * descriptors). Unregistered slots are all-zero, which is the answer we want + * for every question asked of them: no fold, no checksum, no name. + * + * Only two of the 256 slots are used, so this costs 8 KB of read-only data to + * hold mostly zeroes. That is the whole point: the alternative ways to reach a + * descriptor in constant time -- a second array mapping code to row, or a + * switch -- are extra code that has to be kept in step with the table, and the + * 8 KB buys them all away. A linear search over a two-row table would be just + * as fast in practice; what it would not be is impossible to get wrong. + */ +static const AnserPayloadOps AnserPayloadTable[PG_UINT8_MAX + 1] = +{ + /* + * A subscription: header and key only. It has no body to fold or to + * checksum, but its header still is checksummed -- that is what protects + * the condition key it is asking to be served on. + */ + [(unsigned char) ANSER_PAYLOAD_NONE] = { + ANSER_PAYLOAD_NONE, "none", false, NULL, NULL + }, + + /* + * A bloom filter part. AnserBloomFoldPartInPlace already has exactly the + * fold contract -- it validates both sides before touching the accumulator + * and ORs the bitsets -- so it is used directly rather than wrapped, as is + * the fill check for worth_delivering. + */ + [(unsigned char) ANSER_PAYLOAD_BLOOM] = { + ANSER_PAYLOAD_BLOOM, "bloom", true, AnserBloomFoldPartInPlace, + AnserBloomPartWorthSending + } +}; + +/* + * Resolve a code, or NULL if nothing has registered it. + * + * The NULL matters: 'code' reaches us straight off the wire, so "no such type" + * is a case that has to be reportable rather than silently defaulted. It is + * also the diagnostic that catches a payload type given a code in the header + * but no row in the table. + */ +const AnserPayloadOps * +AnserPayloadLookup(char code) +{ + /* + * Cast before indexing. 'char' is signed on x86, and the code is an + * untrusted byte: without this, a 0x80-0xff code from a corrupted or + * hostile message would index the table at a negative offset. + */ + const AnserPayloadOps *ops = &AnserPayloadTable[(unsigned char) code]; + + if (ops->code == '\0') + return NULL; + + /* The index is authoritative; a row filed under the wrong code is a typo. */ + Assert(ops->code == code); + + return ops; +} + +bool +AnserPayloadChecksumsBody(char code) +{ + /* + * No presence check: an unregistered code lands on a zeroed slot, and + * "false" is exactly right for it -- there is nothing to checksum that we + * would know how to use anyway. + */ + return AnserPayloadTable[(unsigned char) code].checksum_body; +} diff --git a/gpcontrib/anser/src/anserplan.c b/gpcontrib/anser/src/anserplan.c new file mode 100644 index 00000000000..80c5c74d812 --- /dev/null +++ b/gpcontrib/anser/src/anserplan.c @@ -0,0 +1,979 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserplan.c + * Post-planning transformation that injects Anser runtime bloom-filter + * producer/consumer CustomScan nodes into a finished plan tree. + * + * The pass runs once from planner() (after both the Postgres planner and ORCA, + * and after set_plan_references / the cdbllize slice passes), recognizes one + * supported join shape, and inserts a producer above the base scan the join's + * build key comes from and a consumer above the base scan its probe key comes + * from. Either side may be reached through the joins before it, so a chain of + * joins gets a filter per join, and two joins reaching the same relation stack + * their filters on it. See anserplan.h for the rationale. + * + * IDENTIFICATION + * gpcontrib/anser/src/anserplan.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "anser.h" +#include "anserfilter.h" +#include "anserplan.h" +#include "cdb/cdbvars.h" +#include "catalog/pg_type.h" +#include "nodes/nodeFuncs.h" +#include "nodes/pg_list.h" +#include "utils/acl.h" +#include "utils/fmgroids.h" +#include "utils/lsyscache.h" +#include "utils/typcache.h" + +/* Runtime-filter bloom size bounds (realized bitset bytes). */ +#define ANSER_RF_MIN_BYTES (1024 * 1024) /* bloom_create's 1 MB floor */ +#define ANSER_RF_MAX_BYTES (64 * 1024 * 1024) +#define ANSER_RF_HEADER_ROOM 64 + +/* Per-statement state for the injection pass. */ +typedef struct AnserInjectCtx +{ + PlannedStmt *stmt; /* for slices[]: see AnserSliceProducers */ + int slice_index; /* slice the subtree being walked runs in */ + Bitmapset *nestloop_params; /* PARAM_EXECs an enclosing nested loop + * reassigns per outer tuple; see + * anser_inject_walk */ + uint32 next_condition_id; + int next_plan_node_id; + List *consumer_keys; /* condition_keys already given a consumer node; + * enforces one consumer per channel (see + * anser_try_inject) */ +} AnserInjectCtx; + +static int anser_max_plan_node_id(Plan *plan); +static bool anser_hashjoin_keys(HashJoin *hj, AttrNumber *inner_attno, + AttrNumber *outer_attno); +static void anser_try_inject(HashJoin *hj, AnserInjectCtx *ctx); +static void anser_inject_walk(Plan *plan, AnserInjectCtx *ctx); + +void +AnserApplyRuntimeFilters(PlannedStmt *stmt) +{ + /* + * Opt-in and coordinator-only: the pass runs on the QD where the whole + * PlannedStmt is available, and only when the operator has enabled the + * Anser subsystem and the runtime-filter feature. Anything else is left + * completely untouched. + */ + if (!gp_anser_enable || !gp_anser_runtime_filter) + return; + if (Gp_role != GP_ROLE_DISPATCH) + return; + if (stmt == NULL || stmt->commandType != CMD_SELECT || stmt->planTree == NULL) + return; + + { + /* + * Zeroed rather than filled field by field. The fields below say what + * they mean and are kept for that, but a pointer field added later and + * missed here would hold whatever was on the stack -- and the first + * thing done with nestloop_params is bms_copy(), which reads a word + * count out of it and allocates that much. + */ + AnserInjectCtx ctx = {0}; + int maxid; + + /* + * Injected nodes need plan_node_ids unique across the whole statement. + * set_plan_references already numbered every existing node, so continue + * past the current maximum (planTree + subplans). + * + * We walk the tree because neither planner's id counter survives to + * this hook (the Postgres planner counts in a standard_planner() + * local; ORCA counts inside its DXL translation context) and + * PlannedStmt carries no max-id field -- walking is the only + * planner-agnostic option, and cheap at this hook point. + * + * We take the max, not the node count: "count == next free id" + * assumes dense numbering, which ORCA's CIdGenerator and third-party + * planner_hooks do not promise. max + 1 is correct under any + * assignment scheme. + */ + maxid = AnserMaxPlanNodeId(stmt); + + ctx.stmt = stmt; + ctx.slice_index = 0; /* the root runs in the coordinator's slice */ + ctx.next_condition_id = 0; + ctx.next_plan_node_id = maxid + 1; + ctx.consumer_keys = NIL; + ctx.nestloop_params = NULL; /* no loop encloses the root */ + + anser_inject_walk(stmt->planTree, &ctx); + } +} + +/* Include separately stored SubPlans as well as the main tree. */ +int +AnserMaxPlanNodeId(PlannedStmt *stmt) +{ + int maxid = anser_max_plan_node_id(stmt->planTree); + ListCell *lc; + + foreach(lc, stmt->subplans) + maxid = Max(maxid, anser_max_plan_node_id((Plan *) lfirst(lc))); + return maxid; +} + +/* + * Largest plan_node_id in a plan subtree. + * + * This has to see every node in the subtree, because the result is the + * high-water mark injected nodes are numbered from: an id it misses is an id we + * hand out a second time. So unlike anser_inject_walk, which deliberately only + * follows the shapes it injects into, this descends every child list there is. + * Getting that wrong corrupts a plan the pass would otherwise have left alone -- + * an Append (a partitioned table, a UNION) is enough to trigger it. + * + * InitPlans need no arm here: a SubPlan only references stmt->subplans by index, + * and AnserApplyRuntimeFilters walks that list separately. + */ +static int +anser_max_plan_node_id(Plan *plan) +{ + int m; + ListCell *lc; + + if (plan == NULL) + return 0; + + m = plan->plan_node_id; + m = Max(m, anser_max_plan_node_id(outerPlan(plan))); + m = Max(m, anser_max_plan_node_id(innerPlan(plan))); + + switch (nodeTag(plan)) + { + case T_Append: + foreach(lc, ((Append *) plan)->appendplans) + m = Max(m, anser_max_plan_node_id((Plan *) lfirst(lc))); + break; + case T_MergeAppend: + foreach(lc, ((MergeAppend *) plan)->mergeplans) + m = Max(m, anser_max_plan_node_id((Plan *) lfirst(lc))); + break; + case T_Sequence: + foreach(lc, ((Sequence *) plan)->subplans) + m = Max(m, anser_max_plan_node_id((Plan *) lfirst(lc))); + break; + case T_BitmapAnd: + foreach(lc, ((BitmapAnd *) plan)->bitmapplans) + m = Max(m, anser_max_plan_node_id((Plan *) lfirst(lc))); + break; + case T_BitmapOr: + foreach(lc, ((BitmapOr *) plan)->bitmapplans) + m = Max(m, anser_max_plan_node_id((Plan *) lfirst(lc))); + break; + case T_SubqueryScan: + m = Max(m, anser_max_plan_node_id(((SubqueryScan *) plan)->subplan)); + break; + case T_CustomScan: + foreach(lc, ((CustomScan *) plan)->custom_plans) + m = Max(m, anser_max_plan_node_id((Plan *) lfirst(lc))); + break; + default: + break; + } + + return m; +} + +/* + * Compute the bloom sizing to hand both the producer and consumer helpers, from + * the estimated build cardinality. Both call AnserBloomCreate (== bloom_create) + * with the SAME (total_elems, max_payload) so they realize an identical filter; + * we mirror bloom_create's own math here so `planned_bytes` (shown in EXPLAIN) + * equals the realized bitset: target ~2 bytes/element, floor at 1 MB, cap at the + * server payload budget, round DOWN to a power of two. + */ +bool +AnserRuntimeFilterSize(double est_rows, int64 *total_elems, int64 *max_payload, + int64 *planned_bytes) +{ + int64 cap_bytes; + int64 elems; + int64 target_bytes; + int64 realized; + + /* Largest bitset that fits the server payload cap, and our own ceiling. */ + cap_bytes = Min((int64) ANSER_RF_MAX_BYTES, + (int64) gp_anser_max_info_size - ANSER_RF_HEADER_ROOM); + if (cap_bytes < ANSER_RF_MIN_BYTES) + return false; /* cap too small to hold even a floor-sized filter */ + + /* + * The element count stays the honest estimate; only the *size* is clamped. + * + * It is tempting to clamp elems instead -- it is the number that drives the + * size, so bounding it bounds the bitset in one step. That is a trap. + * total_elems is also what optimal_k() sizes the hash count from, so + * understating it understates k: a 128M-row build side reported as 33.5M + * gets k=10, when the optimum for 128M keys in a 512 Mbit filter is k=3. + * Since the producer inserts all 128M keys regardless of what we wrote + * down, the result is a filter with a 38% false positive rate where 15% was + * available. Lowering the declared count never makes a filter fit; it only + * relabels it, and then misconfigures it. + * + * INT32 is the real bound on elems: custom_private carries it as an Integer + * node. The density check below refuses anything remotely near that. + */ + elems = (est_rows > 0.0) ? (int64) est_rows : 1; + if (elems < 1) + elems = 1; + if (elems > PG_INT32_MAX) + elems = PG_INT32_MAX; + + target_bytes = Min(cap_bytes, Max((int64) ANSER_RF_MIN_BYTES, elems * 2)); + realized = ANSER_RF_MIN_BYTES; + while ((realized << 1) <= target_bytes) + realized <<= 1; + + /* + * Debug/test knob: force a filter of at least this size regardless of the + * estimate. 0 (the default) means the estimate rules. A bigger filter + * built from the same rows stays a superset of the key set, so this can + * only cost memory and wire time, never correctness. Still capped by the + * payload budget: a floor above it cannot ship. + */ + if (gp_anser_debug_min_filter_bytes > 0 && + realized < (int64) gp_anser_debug_min_filter_bytes) + { + while (realized < (int64) gp_anser_debug_min_filter_bytes && + (realized << 1) <= cap_bytes) + realized <<= 1; + } + + /* + * The filter is built from total_elems (bloom_create sizes from the + * element count, not from planned_bytes), so a floor on realized alone + * only inflates the EXPLAIN number: a 200-row build still produces a 1 MB + * filter whatever the knob says. Raise the element count to match the + * forced size, so producer and consumer really build it. The extra hash + * functions cost only probe time, and the larger bitset only lowers the + * false-positive rate -- a 64 MB filter over 200 keys is overkill, never + * wrong. realized is a power of two, so elems = realized / 2 sizes the + * bitset to exactly realized. + */ + if (realized > elems * 2) + elems = realized / 2; + + /* + * Refuse a join whose build side cannot be usefully summarized within the + * payload cap. + * + * Refusing here is the cheapest possible outcome: no producer, no consumer, + * no channel, and nothing for a consumer to wait on and time out against. + * The runtime checks in anserfilter.c exist for when this estimate turns + * out to be wrong, not instead of this one. + */ + if (realized * (double) BITS_PER_BYTE / (double) elems + < ANSER_BLOOM_MIN_BITS_PER_KEY) + return false; + + *total_elems = elems; + *max_payload = cap_bytes + ANSER_RF_HEADER_ROOM; + *planned_bytes = realized; + return true; +} + +/* + * The key types we may filter on: by-value types for which SQL equality is + * provably bitwise equality of the stored bytes, so that hashing the raw + * Datum on both sides agrees with the equality operator. Producer and + * consumer hash raw bytes, so a type with a non-canonical representation + * would hash two different ways under one equality operator -- a false + * negative that drops a joinable row. Floats are the canonical + * counterexample (-0.0 and 0.0 compare equal but are not bitwise equal); + * everything else that does not make this guarantee is refused by omission. + * Domains resolve through getBaseType() before landing here. + */ +static bool +anser_rf_key_type_supported(Oid keytype) +{ + switch (keytype) + { + case BOOLOID: + case CHAROID: + case INT2OID: + case INT4OID: + case INT8OID: + case OIDOID: + case XIDOID: + case CIDOID: + case DATEOID: + case TIMEOID: + case TIMESTAMPOID: + case TIMESTAMPTZOID: + case MONEYOID: + case PG_LSNOID: + case XID8OID: + return true; + default: + return false; + } +} + +/* + * Match a single-column equijoin over plain (by-value) Vars and return the + * inner (build) and outer (probe) key attnos. After set_plan_references the + * operands are INNER_VAR / OUTER_VAR references into the join's child tlists. + */ +static bool +anser_hashjoin_keys(HashJoin *hj, AttrNumber *inner_attno, AttrNumber *outer_attno) +{ + OpExpr *op; + Node *l; + Node *r; + Var *outer_var; + Var *inner_var; + Oid keytype; + TypeCacheEntry *tce; + + if (list_length(hj->hashclauses) != 1) + return false; + op = (OpExpr *) linitial(hj->hashclauses); + if (!IsA(op, OpExpr) || list_length(op->args) != 2) + return false; + + l = (Node *) linitial(op->args); + r = (Node *) lsecond(op->args); + while (l != NULL && IsA(l, RelabelType)) + l = (Node *) ((RelabelType *) l)->arg; + while (r != NULL && IsA(r, RelabelType)) + r = (Node *) ((RelabelType *) r)->arg; + if (l == NULL || r == NULL || !IsA(l, Var) || !IsA(r, Var)) + return false; + + if (((Var *) l)->varno == OUTER_VAR && ((Var *) r)->varno == INNER_VAR) + { + outer_var = (Var *) l; + inner_var = (Var *) r; + } + else if (((Var *) l)->varno == INNER_VAR && ((Var *) r)->varno == OUTER_VAR) + { + outer_var = (Var *) r; + inner_var = (Var *) l; + } + else + return false; + + /* + * Producer and consumer hash the raw Datum bytes, which is only correct + * when SQL equality coincides with bitwise Datum equality of the key. + * That requires the same type on both sides -- cross-type equijoins (e.g. + * float4 = float8, date = timestamp) hash different bit patterns for equal + * values -- and the type on the whitelist (see + * anser_rf_key_type_supported), which is where the bitwise-equality claim + * is made. Anything looser could prune rows that actually join -- a wrong + * answer, not a slow one. + */ + if (inner_var->vartype != outer_var->vartype) + return false; + + keytype = getBaseType(inner_var->vartype); + if (!anser_rf_key_type_supported(keytype)) + return false; + + /* + * Finally, the clause has to be the type's own equality rather than just + * some hashable operator spelled "=". This does not prove bitwise + * equality -- float8's default equality is float8eq, which is why floats + * need the exclusion above -- but it keeps an operator with semantics of + * its own from being read as if it were the canonical one. + */ + tce = lookup_type_cache(keytype, TYPECACHE_EQ_OPR); + if (!OidIsValid(tce->eq_opr) || op->opno != tce->eq_opr) + return false; + + *inner_attno = inner_var->varattno; + *outer_attno = outer_var->varattno; + return true; +} + +/* + * Node types the descent may pass through: the single-child passthroughs + * (Hash, Motion), the joins whose sides carry the column up (HashJoin), and + * our own filter nodes. Anything else -- WindowAgg, Limit, Agg, Result, an + * Append or SetOp whose column comes from several relations at once -- ends + * the descent: the superset argument for landing the filter on the base scan + * (anser_try_inject) only holds for joins and passthroughs; an operator whose + * output depends on the whole input (a window over it, an aggregate of it, + * its first N rows) would be computed over the filtered rows instead. + */ +static bool +anser_rf_passthrough_node(const Plan *plan) +{ + return IsA(plan, Hash) || IsA(plan, Motion) || IsA(plan, HashJoin) || + AnserIsRuntimeFilterScan(plan); +} + +/* + * Is this side of the join the nullable one? On that side an outer join + * extends non-matching rows with NULLs, so a pruned row comes back as a + * NULL-extended row rather than disappearing -- and a downstream condition + * can tell the difference (IS NOT DISTINCT FROM matches NULL to NULL, so + * what was a no-match becomes a match). The preserved side is safe to + * filter: its rows survive identically either way. + */ +static bool +anser_rf_nullable_side(JoinType jointype, bool inner_side) +{ + switch (jointype) + { + case JOIN_INNER: + return false; + case JOIN_LEFT: + return inner_side; + case JOIN_RIGHT: + return !inner_side; + default: + /* FULL, ANTI, LASJ variants and anything future: block both. */ + return true; + } +} + +/* + * Require the scan's own targetlist entry for the column to be a plain Var + * of the scanned relation, never a computed expression (ORCA emits those in + * a Seq Scan's Output). Producer and consumer hash the raw Datum, and a + * tuple-fetched value and an expression-computed one need not agree in the + * upper bytes for 4-byte types: fetch_att sign-extends (Int32GetDatum, + * tupmacs.h) while e.g. ObjectIdGetDatum zero-extends, so an oid >= 2^31 + * would hash two different ways under one equality operator -- a false + * negative that drops a joinable row. A plain Var guarantees both sides + * read the column through fetch_att, where the representation is fixed. + */ +static bool +anser_rf_scan_column_is_var(Plan *scan, AttrNumber attno) +{ + TargetEntry *tle; + Var *var; + + if (scan == NULL || !IsA(scan, SeqScan) || + attno < 1 || attno > list_length(scan->targetlist)) + return false; + tle = (TargetEntry *) list_nth(scan->targetlist, attno - 1); + if (tle == NULL || !IsA(tle->expr, Var)) + return false; + var = (Var *) tle->expr; + return var->varno == ((Scan *) scan)->scanrelid; +} + +/* + * Follow one side of a join down to the base SeqScan its key column comes + * from, mapping the key attno through each targetlist on the way. Used for + * both sides: the producer goes above the scan the build key comes from, the + * consumer above the scan the probe key comes from. + * + * Wrapping the base scan (rather than an intermediate node) keeps the injected + * CustomScan's custom_scan_tlist made of base-relation Vars, which (a) deparses + * cleanly in EXPLAIN and (b) is the proven-safe "leaf child" case for MPP + * slice/gang setup. (a) is not a preference: after set_plan_references an + * intermediate node's targetlist holds OUTER_VAR/INNER_VAR Vars, and a + * CustomScan keeps its child in custom_plans rather than in lefttree, so + * copying such a targetlist into custom_scan_tlist leaves EXPLAIN resolving an + * OUTER_VAR against a node with no outer plan -- get_variable() raises "bogus + * varno" (ruleutils.c) and the plan cannot be printed at all. + * + * The descent goes through HashJoins as well as the single-child passthroughs + * (Hash, Motion), which is what lets a chain of joins be filtered: by the third + * join one side is the result of the first two, and the key column still traces + * back to one base relation through it. A filter built that way holds the + * relation's whole key column rather than the subset the earlier joins would + * have left -- a superset, so no row that could join is ever rejected, but less + * selective than filtering on the intermediate result itself. Filtering on the + * intermediate would mean wrapping it, which (a) above rules out. + * + * It also goes through our own nodes, so the second join to reach a relation + * stacks its filter under the first one's rather than replacing it. + * + * On success *parent_out is the node holding the base scan -- pass it to + * anser_relink_child(), which knows that ours hold it in custom_plans and + * everything else in the outer link. A SeqScan is only accepted through one of + * those two: the inner child of a HashJoin is always a Hash. + */ +bool +AnserResolveKeyScan(Plan *top, AttrNumber key_attno, int slice_index, + bool for_consumer, + Plan **parent_out, Plan **scan_out, + AttrNumber *attno_out, int *slice_out) +{ + Plan *node = top; + AttrNumber attno = key_attno; + + /* + * top itself must be descendable: the loop below checks only child, and + * on the first iteration node IS top, which without this a WindowAgg, + * Limit, Agg or Result above the probe scan would be walked straight + * past. + */ + if (top == NULL || !anser_rf_passthrough_node(top)) + return false; + + for (;;) + { + TargetEntry *tle; + Var *var; + Plan *child; + bool via_outer; + + if (node == NULL || + attno < 1 || attno > list_length(node->targetlist)) + return false; + + tle = (TargetEntry *) list_nth(node->targetlist, attno - 1); + if (tle == NULL || !IsA(tle->expr, Var)) + return false; + var = (Var *) tle->expr; + + /* + * Which child supplies this column. A join is the only node here with + * two of them, and only a HashJoin is descended through -- an Append or + * a SetOp would make the column come from several relations at once, + * and then no single scan holds the keys the filter would claim. + * + * One of our own nodes is a passthrough with an identity targetlist, so + * its columns are its child's columns in the same order and the attno + * carries over unchanged. Descending through it is what lets a second + * join filter a relation a first join already filters. + */ + if (var->varno == OUTER_VAR) + { + child = outerPlan(node); + via_outer = true; + } + else if (var->varno == INNER_VAR && IsA(node, HashJoin)) + { + child = innerPlan(node); + via_outer = false; + } + else if (var->varno == INDEX_VAR && AnserIsRuntimeFilterScan(node)) + { + child = (Plan *) linitial(((CustomScan *) node)->custom_plans); + via_outer = true; + } + else + return false; + + if (child == NULL) + return false; + + /* + * The consumer must never land below an outer join's nullable side: + * pruning there does not just remove the row, it NULL-extends it, and + * a downstream condition can tell that from "no row" (IS NOT DISTINCT + * FROM matches NULL to NULL, so a no-match becomes a match). The + * producer may descend there: a bloom built over the whole base + * column is a superset of what the outer join emits, and a superset + * is all it needs to be. + */ + if (for_consumer && IsA(node, HashJoin) && + anser_rf_nullable_side(((HashJoin *) node)->join.jointype, + !via_outer)) + return false; + + if (via_outer && IsA(child, SeqScan)) + { + /* + * The scan's own entry for the column must be a plain Var, never + * a computed expression (see anser_rf_scan_column_is_var). + */ + if (!anser_rf_scan_column_is_var(child, var->varattno)) + return false; + + *parent_out = node; + *scan_out = child; + *attno_out = var->varattno; + *slice_out = slice_index; + return true; + } + if (!anser_rf_passthrough_node(child)) + return false; + + /* + * Descending through a Motion moves us into its sending slice, which + * is where the producer will end up -- and whose width decides how + * many parts the coordinator has to wait for. + */ + if (IsA(child, Motion)) + slice_index = ((Motion *) child)->motionID; + + node = child; + attno = var->varattno; + } +} + +/* + * Put `child` where the scan the descent ended on used to be. The link is the + * outer one for every node the descent walks except one of ours, which holds + * its child in custom_plans -- that is the case where a second join filters a + * relation a first join already filters, and the new node goes below the old + * one so that both see every row. + */ +static void +anser_relink_child(Plan *parent, Plan *child) +{ + if (AnserIsRuntimeFilterScan(parent)) + linitial(((CustomScan *) parent)->custom_plans) = child; + else + outerPlan(parent) = child; +} + +/* + * If this HashJoin is the supported shape, inject a producer above the base + * scan its build key comes from and a consumer above the base scan its probe + * key comes from. + */ +static void +anser_try_inject(HashJoin *hj, AnserInjectCtx *ctx) +{ + Plan *hash = innerPlan(hj); /* build side */ + Plan *probe = outerPlan(hj); /* probe side */ + Plan *build_parent; + Plan *build_scan; + Plan *probe_parent; + Plan *probe_scan; + AttrNumber inner_attno; + AttrNumber outer_attno; + AttrNumber build_attno; + AttrNumber probe_attno; + int build_slice; + int probe_slice; + int n_producers; + int64 total_elems; + int64 max_payload; + int64 planned_bytes; + uint32 condition_id; + char condition_key[ANSER_CONDITION_KEY_SIZE]; + CustomScan *producer; + CustomScan *consumer; + ListCell *lc; + + if (hj->join.jointype != JOIN_INNER && hj->join.jointype != JOIN_RIGHT) + return; + if (hash == NULL || !IsA(hash, Hash)) + return; + if (probe == NULL) + return; + + /* + * Refuse a build side that depends on a parameter an enclosing nested loop + * reassigns. Such a side yields a different set of keys per outer tuple, + * and nothing here rebuilds: the rescan callbacks rescan their child and + * keep everything else, so the producer stays published and the consumer + * keeps the filter it already received. The second iteration would then be + * probed against the first iteration's keys -- a filter that is no longer a + * superset, which is the one way this feature can drop a row that joins. + * + * extParam is the whole build subtree's dependency on parameters set + * outside it (SS_finalize_plan fills it in before set_plan_references, so + * it is still here), and ctx->nestloop_params is what the nested loops we + * are nested inside actually reassign. An InitPlan's output parameter + * appears in extParam too but is evaluated once, so intersecting against + * the nestloop set rather than testing extParam for emptiness keeps those + * plans eligible. + * + * Only the build side is tested. A parameterized probe side is rescanned + * too, but the filter it probes was built from keys that did not change, so + * reusing it stays correct -- and skipping the rebuild is then a saving. + * + * The alternative is to re-exchange per iteration, which needs a generation + * in the channel key and a barrier across every segment per outer tuple. + * Not worth it for a shape this narrow. + */ + if (bms_overlap(hash->extParam, ctx->nestloop_params)) + return; + + if (!anser_hashjoin_keys(hj, &inner_attno, &outer_attno)) + return; + if (!AnserResolveKeyScan(hash, inner_attno, ctx->slice_index, + false /* for_consumer */ , + &build_parent, &build_scan, &build_attno, + &build_slice)) + return; + + /* + * Where the filter is applied. A probe side that is already a scan is the + * simple case; anything else -- a Motion over the result of the joins + * before this one -- is descended the same way the build side is, and the + * consumer lands on the base scan the probe key comes from. + * + * Filtering that scan is right for the same reason filtering the probe + * side is: the descent never crosses an outer join's nullable side (see + * AnserResolveKeyScan), so every row the probe subtree produces carries + * that relation's key column unchanged, and a row whose key the build side + * does not have cannot reach the join's output through any path. + */ + if (IsA(probe, SeqScan) && + anser_rf_scan_column_is_var(probe, outer_attno)) + { + probe_parent = (Plan *) hj; + probe_scan = probe; + probe_attno = outer_attno; + probe_slice = ctx->slice_index; /* nothing descended through */ + } + else if (!AnserResolveKeyScan(probe, outer_attno, ctx->slice_index, + true /* for_consumer */ , + &probe_parent, &probe_scan, &probe_attno, + &probe_slice)) + return; + + /* + * Nothing to gain from filtering a relation with its own keys, and the + * producer would have to finish before the consumer under it could start. + */ + if (probe_scan == build_scan) + return; + + /* + * Only filter across a Motion. Slices are divided by Motions and nothing + * else, so two scans in one slice are a co-located join, and that is a + * different proposition in both directions. + * + * It is what makes the exchange safe. An Anser exchange is a + * synchronisation point: the consumer stops until the coordinator has + * heard from every producer. Within one slice those are the same process, + * run one after another, so the wait can be on something that has not been + * reached yet -- a consumer placed under an intervening HashJoin waits for + * a producer sitting in that join's inner side, which cannot start until + * the outer side yields the tuple the consumer is holding. defer_first + * covers exactly one tuple of that. Across a Motion the two are separate + * processes and the question does not arise. + * + * And it is what makes it worth doing. Where a Motion already carries the + * rows, the barrier is paid for by work the query was doing anyway; in a + * co-located join it is new cost against a join that was already local, + * and whether it pays for itself is a measurement nobody has made. Until + * someone does, decline. + */ + if (build_slice == probe_slice) + return; + + /* + * How many producers will there be? One per process of the build scan's + * slice. Declining when we cannot tell is the safe direction: a count that + * is too low makes the coordinator complete the channel early and ship a + * filter missing keys, which drops joinable rows. + */ + n_producers = AnserSliceProducers(ctx->stmt, build_slice); + if (n_producers <= 0) + return; + /* + * Sized from the scan the producer wraps, not from the Hash: those are the + * rows whose keys actually go into the filter. They are the same thing + * when the build side is one scan under a Hash, and they are not when the + * descent came through a join -- there the Hash holds the joined result and + * the scan holds the whole key column, which is what gets inserted. + */ + if (!AnserRuntimeFilterSize(build_scan->plan_rows, &total_elems, &max_payload, + &planned_bytes)) + return; + + condition_id = ctx->next_condition_id++; + snprintf(condition_key, sizeof(condition_key), "anser_rf_%u", condition_id); + + /* + * One consumer per channel. Two consumer nodes sharing a channel would + * both be served -- the coordinator pushes to every subscriber -- but they + * would also both count toward nothing: the channel's part count comes from + * the producers, so a second consumer only multiplies deliveries. More to + * the point, the two would be indistinguishable in the debug trace and in + * any future per-channel accounting, so keep the invariant. + * + * Minting a unique condition_id per injection makes this hold by + * construction, so the check never fires. It stays as a guard against + * channel-key collisions if the key derivation ever changes (e.g. keys + * derived from the build's semantic identity, where two joins could share + * one channel). + */ + foreach(lc, ctx->consumer_keys) + { + if (strcmp((const char *) lfirst(lc), condition_key) == 0) + return; + } + + /* Producer wraps the build base scan; keyed by the mapped build attno. */ + producer = AnserBuildBloomProducerScan(build_scan, build_attno, condition_id, + condition_key, total_elems, max_payload, + planned_bytes, n_producers); + producer->scan.plan.plan_node_id = ctx->next_plan_node_id++; + anser_relink_child(build_parent, (Plan *) producer); + + /* + * Consumer wraps the probe base scan; keyed by the mapped probe attno. + * + * Whether it must let the join's first outer tuple through unfiltered + * comes down to whether ExecHashJoin prefetches one before building its + * hash table. It skips the prefetch when it null-fills the inner side -- + * HJ_FILL_INNER, which for the join types injected into means a right join + * -- and otherwise decides on a cost comparison we are not going to + * reproduce here. So: rule it out only where the join type rules it out, + * and defer everywhere else. Wrongly deferring gives up pushdown for that + * consumer; wrongly not deferring deadlocks it against its own producer. + */ + consumer = AnserBuildBloomConsumerScan(probe_scan, probe_attno, condition_id, + condition_key, total_elems, max_payload, + planned_bytes, n_producers, + hj->join.jointype != JOIN_RIGHT); + consumer->scan.plan.plan_node_id = ctx->next_plan_node_id++; + anser_relink_child(probe_parent, (Plan *) consumer); + + /* Record the channel so no later join can add a second consumer on it. */ + ctx->consumer_keys = lappend(ctx->consumer_keys, pstrdup(condition_key)); +} + +/* + * Manual in-place traversal (recurse spine + custom_plans). We do not use + * plan_tree_mutator, whose CustomScan arm does not descend into custom_plans. + */ +static void +anser_inject_walk(Plan *plan, AnserInjectCtx *ctx) +{ + int save_slice = ctx->slice_index; + + if (plan == NULL) + return; + + /* + * A Motion is the boundary between slices, and everything below one runs + * in the sending slice. cdbllize stamps that slice's index into motionID + * (cdbllize.c: "motion->motionID = sendSlice->sliceIndex") and then clears + * senderSliceInfo, so motionID is the only durable way to get here from a + * finished plan. + */ + if (IsA(plan, Motion)) + ctx->slice_index = ((Motion *) plan)->motionID; + + if (IsA(plan, HashJoin)) + anser_try_inject((HashJoin *) plan, ctx); + + anser_inject_walk(outerPlan(plan), ctx); + + /* + * A nested loop re-executes its inner side once per outer tuple, after + * assigning that tuple's values to the PARAM_EXECs in nestParams. Carry + * those paramids down the inner side so anser_try_inject can tell whether a + * build side it is looking at will be rebuilt with different values. + */ + if (IsA(plan, NestLoop) && ((NestLoop *) plan)->nestParams != NIL) + { + Bitmapset *saved = ctx->nestloop_params; + Bitmapset *inner = bms_copy(saved); + ListCell *lc; + + foreach(lc, ((NestLoop *) plan)->nestParams) + inner = bms_add_member(inner, + ((NestLoopParam *) lfirst(lc))->paramno); + + ctx->nestloop_params = inner; + anser_inject_walk(innerPlan(plan), ctx); + ctx->nestloop_params = saved; + bms_free(inner); + } + else + anser_inject_walk(innerPlan(plan), ctx); + + if (IsA(plan, CustomScan)) + { + ListCell *lc; + + foreach(lc, ((CustomScan *) plan)->custom_plans) + anser_inject_walk((Plan *) lfirst(lc), ctx); + } + + ctx->slice_index = save_slice; +} + +/* + * How many processes execute a slice -- which is how many producers will + * publish a part, and therefore how many the coordinator must wait for. + * + * This mirrors FillSliceGangInfo (execUtils.c), which has a different answer + * per gang type: numsegments * parallel_workers for a plain reader or writer, + * but one process for the coordinator's own slice, parallel_workers for a + * singleton reader, and one set per targeted segment under direct dispatch. + * parallel_workers == 0 means one. Each is handled below, because over- + * counting costs every consumer the full anser.timeout_ms: the channel never + * reaches the part count it is waiting for, so it never completes and nobody + * is ever told. Under-counting would be worse -- a filter short of one + * segment's keys rejects rows that join -- which is why none of these guesses. + * + * Both optimizers are covered, and they do differ: for the same query the + * Postgres planner may give a serial build slice (Broadcast Motion 2:8 over a + * plain Seq Scan, so 2 producers) where GPORCA gives a parallel one (Broadcast + * Motion 16:16 over a Parallel Seq Scan, so 16). Reading it from the slice + * means neither case needs special handling. + */ +int +AnserSliceProducers(PlannedStmt *stmt, int slice_index) +{ + PlanSlice *slice; + int factor; + + if (stmt == NULL || stmt->slices == NULL || + slice_index < 0 || slice_index >= stmt->numSlices) + return 0; /* unknown: caller declines to inject */ + + slice = &stmt->slices[slice_index]; + + /* The coordinator's own slice is one process and has no gang. */ + if (slice->gangType == GANGTYPE_UNALLOCATED || + slice->gangType == GANGTYPE_ENTRYDB_READER) + return 1; + + factor = slice->parallel_workers > 0 ? slice->parallel_workers : 1; + + /* + * A singleton reader runs on one segment, whatever the cluster's width: + * FillSliceGangInfo gives it planNumSegments = 1 * factor and fills its + * segment list with factor copies of one segindex. numsegments is not + * even meaningful here -- plannodes.h scopes that field to the + * PRIMARY_READER/WRITER cases. + */ + if (slice->gangType == GANGTYPE_SINGLETON_READER) + return factor; + + /* + * Direct dispatch narrows a reader gang to the segments it actually + * needs, and FillSliceGangInfo builds the segment list from contentIds -- + * factor processes for each. planNumSegments keeps saying + * numsegments * factor, so the segment list is what to count. + * + * An empty list yields zero, which the caller reads as "unknown" and + * declines on, and that is the right answer: a slice dispatched nowhere + * publishes nothing. + */ + if (slice->directDispatch.isDirectDispatch) + return list_length(slice->directDispatch.contentIds) * factor; + + if (slice->numsegments <= 0) + return 0; + + return slice->numsegments * factor; +} diff --git a/gpcontrib/anser/src/anserplanexec.c b/gpcontrib/anser/src/anserplanexec.c new file mode 100644 index 00000000000..7ac6373e434 --- /dev/null +++ b/gpcontrib/anser/src/anserplanexec.c @@ -0,0 +1,1049 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserplanexec.c + * CustomScan providers that carry the Anser runtime bloom filter through + * the executor: a pass-through "producer" above the hash build input that + * observes the build join key and publishes a bloom filter, and a + * pass-through "consumer" above the probe scan that prunes rows whose key is + * definitely absent from the received (unioned) filter. + * + * Both are thin drivers over the helper library in + * anserbloom.h, which selects transport by role + * (segment -> libpq to QD; coordinator -> direct shmem) and unions parts. The + * plan-injection pass (anserplan.c) builds the nodes and stashes the parameters + * in CustomScan.custom_private using the layout in AnserRfPrivateIndex below. + * + * IDENTIFICATION + * gpcontrib/anser/src/anserplanexec.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "access/skey.h" +#include "anser.h" +#include "anserbloom.h" +#include "anserfilter.h" +#include "anserplan.h" +#include "ansertlist.h" +#include "cdb/cdbutil.h" +#include "cdb/cdbvars.h" +#include "commands/explain.h" +#include "executor/executor.h" +#include "lib/bloomfilter.h" +#include "nodes/extensible.h" +#include "nodes/makefuncs.h" +#include "nodes/nodeFuncs.h" +#include "nodes/pg_list.h" +#include "nodes/value.h" +#include "utils/guc.h" + +/* + * Positional layout of CustomScan.custom_private shared with anserplan.c. + * Integer nodes except the last two, which are Strings. Both providers read + * the same list (each ignores fields it does not need). + */ +typedef enum AnserRfPrivateIndex +{ + ANSER_RF_PRIV_CONDITION_ID = 0, /* Integer: channel condition_id */ + ANSER_RF_PRIV_KEY_ATTNO, /* Integer: build/probe key attno */ + ANSER_RF_PRIV_TOTAL_ELEMS, /* Integer: bloom sizing (producer) */ + ANSER_RF_PRIV_MAX_PAYLOAD, /* Integer: bloom sizing (producer) */ + ANSER_RF_PRIV_PLANNED_BYTES, /* Integer: planned bitset bytes (EXPLAIN) */ + ANSER_RF_PRIV_N_PRODUCERS, /* Integer: processes publishing a part */ + ANSER_RF_PRIV_DEFER_FIRST, /* Integer: consumer must let the join's + * empty-outer prefetch through */ + ANSER_RF_PRIV_CONDITION_KEY, /* String: channel condition_key */ + ANSER_RF_PRIV__COUNT +} AnserRfPrivateIndex; + +/* Registration-timeout for the consumer's wait-for-registration phase. */ +#define ANSER_RF_REGISTRATION_TIMEOUT_MS ((long) gp_anser_timeout_ms) + +/* + * Producer scan state: feeds the build key of every child tuple into the + * filter and publishes this backend's part once the child is exhausted. + */ +typedef struct AnserBloomProduceScanState +{ + CustomScanState csstate; + AnserBloomFilterProduceState *produce; + AttrNumber key_attno; + int64 planned_bytes; + bool started; /* this process actually executed the node */ + bool my_slice; /* ... and was always going to: the node's + * slice is the one this process runs */ + bool published; +} AnserBloomProduceScanState; + +/* + * Consumer scan state: receives and unions the bloom parts on first use, + * then prunes probe rows whose key is definitely absent from the filter. + */ +typedef struct AnserBloomConsumeScanState +{ + CustomScanState csstate; + AnserBloomFilterConsumeState *consume; + bloom_filter *filter; /* NULL => fail open (pass everything) */ + AttrNumber key_attno; + int64 planned_bytes; + bool received; /* have we run the receive/union yet? */ + bool defer_first; /* this join prefetches one outer tuple before + * building its hash; see anser_consume_receive() */ + bool probed; /* has the join pulled a tuple from us before? */ + bool pushed_down; /* filter handed to the child scan as an + * SK_BLOOM_FILTER scan key (the scan filters, + * we pass through) */ +} AnserBloomConsumeScanState; + +/* Provider callbacks. */ +static Node *anser_produce_create_state(CustomScan *cscan); +static void anser_produce_begin(CustomScanState *node, EState *estate, int eflags); +static TupleTableSlot *anser_produce_exec(CustomScanState *node); +static void anser_produce_end(CustomScanState *node); +static void anser_produce_rescan(CustomScanState *node); +static void anser_produce_explain(CustomScanState *node, List *ancestors, + ExplainState *es); + +static Node *anser_consume_create_state(CustomScan *cscan); +static void anser_consume_begin(CustomScanState *node, EState *estate, int eflags); +static TupleTableSlot *anser_consume_exec(CustomScanState *node); +static void anser_consume_end(CustomScanState *node); +static void anser_consume_rescan(CustomScanState *node); +static void anser_consume_explain(CustomScanState *node, List *ancestors, + ExplainState *es); + +static const CustomScanMethods anser_produce_scan_methods = +{ + .CustomName = "Anser Bloom Producer", + .CreateCustomScanState = anser_produce_create_state, +}; + +static const CustomScanMethods anser_consume_scan_methods = +{ + .CustomName = "Anser Bloom Consumer", + .CreateCustomScanState = anser_consume_create_state, +}; + +static const CustomExecMethods anser_produce_exec_methods = +{ + .CustomName = "Anser Bloom Producer", + .BeginCustomScan = anser_produce_begin, + .ExecCustomScan = anser_produce_exec, + .EndCustomScan = anser_produce_end, + .ReScanCustomScan = anser_produce_rescan, + .ExplainCustomScan = anser_produce_explain, +}; + +static const CustomExecMethods anser_consume_exec_methods = +{ + .CustomName = "Anser Bloom Consumer", + .BeginCustomScan = anser_consume_begin, + .ExecCustomScan = anser_consume_exec, + .EndCustomScan = anser_consume_end, + .ReScanCustomScan = anser_consume_rescan, + .ExplainCustomScan = anser_consume_explain, +}; + +void +AnserRegisterRuntimeFilterMethods(void) +{ + RegisterCustomScanMethods(&anser_produce_scan_methods); + RegisterCustomScanMethods(&anser_consume_scan_methods); +} + +/* ---- node builders (called from the injection pass in anserplan.c) ---- */ + +/* + * Identity output targetlist for a pass-through CustomScan: a Var per child + * column referencing the scan tuple via INDEX_VAR, so ExecScan projects the + * child tuple through unchanged. (Post-setrefs we build this by hand rather + * than relying on set_customscan_references.) + */ +static List * +anser_rf_identity_tlist(List *child_tlist) +{ + List *tlist = NIL; + ListCell *lc; + AttrNumber attno = 0; + + foreach(lc, child_tlist) + { + TargetEntry *tle = (TargetEntry *) lfirst(lc); + Var *var; + + attno++; + var = makeVar(INDEX_VAR, attno, + exprType((Node *) tle->expr), + exprTypmod((Node *) tle->expr), + exprCollation((Node *) tle->expr), + 0); + tlist = lappend(tlist, + makeTargetEntry((Expr *) var, attno, + tle->resname ? pstrdup(tle->resname) : NULL, + tle->resjunk)); + } + + return tlist; +} + +static CustomScan * +anser_build_rf_scan(const CustomScanMethods *methods, Plan *child, + AttrNumber key_attno, uint32 condition_id, + const char *condition_key, int64 total_elems, + Size max_payload_bytes, int64 planned_bytes, + int n_producers, bool defer_first) +{ + CustomScan *cs = makeNode(CustomScan); + List *priv = NIL; + + /* custom_private, in AnserRfPrivateIndex order. */ + priv = lappend(priv, makeInteger((int) condition_id)); + priv = lappend(priv, makeInteger((int) key_attno)); + priv = lappend(priv, makeInteger((int) total_elems)); + priv = lappend(priv, makeInteger((int) max_payload_bytes)); + priv = lappend(priv, makeInteger((int) planned_bytes)); + priv = lappend(priv, makeInteger(n_producers)); + priv = lappend(priv, makeInteger(defer_first ? 1 : 0)); + priv = lappend(priv, makeString(pstrdup(condition_key))); + + cs->scan.plan.targetlist = anser_rf_identity_tlist(child->targetlist); + cs->scan.plan.qual = NIL; + cs->scan.plan.lefttree = NULL; + cs->scan.plan.righttree = NULL; + cs->scan.plan.startup_cost = child->startup_cost; + cs->scan.plan.total_cost = child->total_cost; + cs->scan.plan.plan_rows = child->plan_rows; + cs->scan.plan.plan_width = child->plan_width; + /* + * Not parallel-aware, and that is not an omission. parallel_aware only + * drives PostgreSQL's DSM callbacks, which are reached from Gather; an MPP + * parallel slice has no Gather and no ParallelContext, so the callbacks + * would never fire. Each process of the slice is a full QE with its own + * dispatch connection, so each publishes and subscribes for itself and the + * coordinator folds the lot -- which is why n_producers above has to be the + * slice's process count rather than the segment count. + */ + cs->scan.plan.parallel_aware = false; + cs->scan.plan.parallel_safe = child->parallel_safe; + cs->scan.plan.flow = (Flow *) copyObject(child->flow); + cs->scan.scanrelid = 0; + cs->flags = 0; + cs->custom_plans = list_make1(child); + cs->custom_exprs = NIL; + cs->custom_private = priv; + cs->custom_scan_tlist = AnserFilterScanTlist(child); + cs->methods = methods; + + return cs; +} + +/* + * Is this one of our own nodes? + * + * The injection pass has to recognise them because it can meet one where it + * expects a scan: two joins whose keys trace to the same base relation both + * want to filter it, and the second arrives to find the first already there. + * + * Compared by method table rather than by name, which holds in any backend and + * not just the one that built the plan: a CustomScan travels as its + * methods->CustomName, and the reader resolves that name through + * GetCustomScanMethods() back to the very table RegisterCustomScanMethods() + * stored -- ours, because _PG_init registers it in every backend. A process + * that had not registered it could not deserialize the plan at all. + */ +bool +AnserIsRuntimeFilterScan(const Plan *plan) +{ + const CustomScan *cscan; + + if (plan == NULL || !IsA(plan, CustomScan)) + return false; + + cscan = (const CustomScan *) plan; + return cscan->methods == &anser_produce_scan_methods || + cscan->methods == &anser_consume_scan_methods; +} + +CustomScan * +AnserBuildBloomProducerScan(Plan *child, AttrNumber key_attno, + uint32 condition_id, const char *condition_key, + int64 total_elems, Size max_payload_bytes, + int64 planned_bytes, int n_producers) +{ + return anser_build_rf_scan(&anser_produce_scan_methods, child, key_attno, + condition_id, condition_key, total_elems, + max_payload_bytes, planned_bytes, n_producers, + false); +} + +CustomScan * +AnserBuildBloomConsumerScan(Plan *child, AttrNumber key_attno, + uint32 condition_id, const char *condition_key, + int64 total_elems, Size max_payload_bytes, + int64 planned_bytes, int n_producers, + bool defer_first) +{ + return anser_build_rf_scan(&anser_consume_scan_methods, child, key_attno, + condition_id, condition_key, total_elems, + max_payload_bytes, planned_bytes, n_producers, + defer_first); +} + +/* ---- shared helpers ---- */ + +static void +anser_rf_build_key(CustomScan *cscan, AnserChannelKey *key) +{ + List *priv = cscan->custom_private; + int condition_id = intVal(list_nth(priv, ANSER_RF_PRIV_CONDITION_ID)); + char *condition_key = strVal(list_nth(priv, ANSER_RF_PRIV_CONDITION_KEY)); + + MemSet(key, 0, sizeof(*key)); + key->gp_session_id = gp_session_id; + key->gp_command_count = gp_command_count; + key->condition_id = (uint32) condition_id; + strlcpy(key->condition_key, condition_key, ANSER_CONDITION_KEY_SIZE); +} + +/* + * How many parts the coordinator must fold, and this backend's part index. + * + * The count comes from the plan, not from getgpsegmentCount(), because a + * parallel slice runs numsegments * parallel_workers processes and every one + * of them publishes its own part. Counting segments instead would make the + * coordinator declare the channel complete after the first nsegments parts + * and hand consumers a filter missing most of the build keys -- a bloom filter + * short of keys gives false negatives, which silently drops joinable rows. + * Only the planner knows the slice width, so it stamps it into custom_private + * (see AnserRuntimeFilterSize's caller in anserplan.c). + * + * part_index is diagnostics only: completion is by count, and the serialized + * part carries its own (0, 1). Under parallel execution the workers of one + * segment therefore share an index, which is why the trace prints the pid too. + * Because it is only diagnostic it must never be allowed to reject a real + * producer: a slice narrower than the cluster (direct dispatch to one + * segment, a singleton reader, or the entry-DB where segindex is -1) would + * otherwise put segindex >= total_parts and refuse the only producer there + * is, leaving the channel a part short and every consumer waiting out + * anser.timeout_ms. Clamp it instead. + */ +static void +anser_rf_part_info(CustomScan *cscan, uint32 *part_index, uint32 *total_parts) +{ + int n_producers = intVal(list_nth(cscan->custom_private, + ANSER_RF_PRIV_N_PRODUCERS)); + + if (Gp_role == GP_ROLE_EXECUTE) + { + *total_parts = (uint32) Max(n_producers, 1); + if (GpIdentity.segindex < 0) + *part_index = 0; /* entry-DB: single local producer */ + else + *part_index = Min((uint32) GpIdentity.segindex, + *total_parts - 1); + } + else + { + /* + * The coordinator produces locally as a single part. This is the + * whole-plan-on-the-QD case (no gang), not the parallel one. + */ + *part_index = 0; + *total_parts = 1; + } +} + +/* Move the child tuple into our scan slot (positional pass-through). */ +static TupleTableSlot * +anser_rf_child_slot(CustomScanState *node, TupleTableSlot *childslot) +{ + ExecCopySlot(node->ss.ss_ScanTupleSlot, childslot); + return node->ss.ss_ScanTupleSlot; +} + +static bool +anser_rf_recheck(CustomScanState *node, TupleTableSlot *slot) +{ + return true; +} + +/* ---- producer ---- */ + +static Node * +anser_produce_create_state(CustomScan *cscan) +{ + AnserBloomProduceScanState *st = (AnserBloomProduceScanState *) + newNode(sizeof(AnserBloomProduceScanState), T_CustomScanState); + + st->csstate.methods = &anser_produce_exec_methods; + return (Node *) st; +} + +static void +anser_produce_begin(CustomScanState *node, EState *estate, int eflags) +{ + AnserBloomProduceScanState *st = (AnserBloomProduceScanState *) node; + CustomScan *cscan = (CustomScan *) node->ss.ps.plan; + List *priv = cscan->custom_private; + Plan *child = (Plan *) linitial(cscan->custom_plans); + AnserChannelKey key; + uint32 part_index; + uint32 total_parts; + int64 total_elems = intVal(list_nth(priv, ANSER_RF_PRIV_TOTAL_ELEMS)); + Size max_payload = (Size) intVal(list_nth(priv, ANSER_RF_PRIV_MAX_PAYLOAD)); + + st->key_attno = (AttrNumber) intVal(list_nth(priv, ANSER_RF_PRIV_KEY_ATTNO)); + st->planned_bytes = intVal(list_nth(priv, ANSER_RF_PRIV_PLANNED_BYTES)); + st->started = false; + st->published = false; + + /* + * Whether this process is the one that runs the node's slice -- which is + * not the same question as whether the node ends up executing, and has to + * be asked here rather than at end time. During initialization + * currentSliceId names the slice being initialized, alien or not; once + * execution starts it always names the local one, so the distinction is + * gone by then. + */ + st->my_slice = (estate->currentSliceId == LocallyExecutingSliceIndex(estate)); + + anser_rf_build_key(cscan, &key); + anser_rf_part_info(cscan, &part_index, &total_parts); + + /* + * Build the filter only in the process that will execute this node, and + * never for EXPLAIN alone. ExecutorStart runs even then + * (EXEC_FLAG_EXPLAIN_ONLY), and on the coordinator it runs over every + * slice (eliminateAliens is false there), so without the gate a plain + * EXPLAIN allocates a full bitset per producer on the coordinator -- + * memory no work_mem/statement_mem accounting ever sees. A NULL produce + * is a first-class state downstream: the node passes tuples through, and + * publish/cancel are NULL-safe. + */ + if (st->my_slice && (eflags & EXEC_FLAG_EXPLAIN_ONLY) == 0) + st->produce = ExecInitAnserBloomFilterProduce(&key, total_elems, + max_payload, part_index, + total_parts); + else + st->produce = NULL; + ANSER_DEBUG("anser: producer init cond=%u part=%u/%u elems=" INT64_FORMAT " payload=%zu state=%s", + key.condition_id, part_index, total_parts, total_elems, + max_payload, st->produce != NULL ? "ok" : "NULL"); + + node->custom_ps = list_make1(ExecInitNode(child, estate, eflags)); +} + +/* + * Pass-through that feeds the build key of every child tuple into the filter, + * and publishes once the child is exhausted. + */ +static TupleTableSlot * +anser_produce_next(CustomScanState *node) +{ + AnserBloomProduceScanState *st = (AnserBloomProduceScanState *) node; + PlanState *child = (PlanState *) linitial(node->custom_ps); + TupleTableSlot *slot; + + /* + * Every process that deserializes the plan builds this node, but only the + * ones running its slice ever execute it; the rest must stay silent. + */ + if (!st->started) + { + st->started = true; + + /* + * No filter means the estimate that justified this node was wrong by + * enough that one cannot usefully be built. Say so now, on the first + * tuple, rather than after scanning the whole build side: the consumers + * are sitting on their timeout, and every millisecond of it is wasted. + * + * This is also the earliest point at which cancelling is safe. Doing + * it at ExecInit time would cancel channels from the coordinator, which + * builds this node for every slice but executes none of them. + */ + if (st->produce != NULL && !st->published && + !ExecAnserBloomFilterProduceHasFilter(st->produce)) + { + ANSER_DEBUG("anser: producer has no usable filter; cancelling before the scan"); + (void) ExecAnserBloomFilterProduceCancel(st->produce); + st->published = true; + } + } + + slot = ExecProcNode(child); + + if (TupIsNull(slot)) + { + if (!st->published) + { + ANSER_DEBUG("anser: producer child exhausted, publishing (state=%s)", + st->produce != NULL ? "ok" : "NULL"); + + (void) ExecAnserBloomFilterProducePublish(st->produce); + st->published = true; + } + return NULL; + } + + if (st->produce != NULL) + { + bool isnull; + Datum value = slot_getattr(slot, st->key_attno, &isnull); + + ExecAnserBloomFilterProduceAddDatum(st->produce, value, isnull); + } + + return anser_rf_child_slot(node, slot); +} + +static TupleTableSlot * +anser_produce_exec(CustomScanState *node) +{ + return ExecScan(&node->ss, + (ExecScanAccessMtd) anser_produce_next, + (ExecScanRecheckMtd) anser_rf_recheck); +} + +static void +anser_produce_end(CustomScanState *node) +{ + AnserBloomProduceScanState *st = (AnserBloomProduceScanState *) node; + + /* + * Consumers must be told, one way or another, by every process the + * channel is counting on. Two cases reach here without having published: + * + * The producer ran and was abandoned mid-scan (squelch, or an error + * upstream), so the part it promised will never come. + * + * Or it never ran at all although this process owns its slice -- which + * happens in a co-located join, where ExecHashJoin skips building the + * hash table on a segment whose outer side turned out to be empty. The + * node is initialized, never executed, and used to leave silently; the + * channel then sat one part short of complete and every consumer waited + * out the whole of anser.timeout_ms for it. + * + * What must still stay silent is a node belonging to another slice. + * Cancelling there would destroy a channel this process has no part in. + */ + if (!st->published && st->produce != NULL && (st->started || st->my_slice)) + { + (void) ExecAnserBloomFilterProduceCancel(st->produce); + st->published = true; + } + + if (st->produce != NULL) + ExecEndAnserBloomFilterProduce(st->produce); + st->produce = NULL; + if (node->custom_ps != NIL) + ExecEndNode((PlanState *) linitial(node->custom_ps)); +} + +/* + * Rescan the child and keep every piece of filter state: the producer stays + * published, the consumer keeps the filter it received, and no second exchange + * happens. + * + * That is only correct because the build side cannot produce different keys on + * a second pass, which the injection pass guarantees by refusing any join whose + * build subtree depends on a parameter an enclosing nested loop reassigns (see + * the extParam test in anser_try_inject). Given that, every iteration wants + * the same filter, so rebuilding it would be pure cost -- and a channel is + * keyed per statement, so a second exchange could not be told apart from the + * first anyway. + * + * If that gate is ever loosened, this is the code that has to change with it. + */ +static void +anser_produce_rescan(CustomScanState *node) +{ + if (node->custom_ps != NIL) + ExecReScan((PlanState *) linitial(node->custom_ps)); +} + +static void +anser_produce_explain(CustomScanState *node, List *ancestors, ExplainState *es) +{ + AnserBloomProduceScanState *st = (AnserBloomProduceScanState *) node; + char buf[64]; + + ExplainPropertyInteger("Bloom Filter Size", "bytes", st->planned_bytes, es); + + /* + * The realized filter size is planned_bytes by construction: producer and + * consumer build identical filters from the same plan parameters (see + * AnserRuntimeFilterSize in anserplan.c), so no execution stats are needed. + */ + snprintf(buf, sizeof(buf), "memory=" INT64_FORMAT "kB", + st->planned_bytes / 1024); + ExplainPropertyText("Bloom Filter Stats", buf, es); +} + +/* ---- consumer ---- */ + +static Node * +anser_consume_create_state(CustomScan *cscan) +{ + AnserBloomConsumeScanState *st = (AnserBloomConsumeScanState *) + newNode(sizeof(AnserBloomConsumeScanState), T_CustomScanState); + + st->csstate.methods = &anser_consume_exec_methods; + return (Node *) st; +} + +static void +anser_consume_begin(CustomScanState *node, EState *estate, int eflags) +{ + AnserBloomConsumeScanState *st = (AnserBloomConsumeScanState *) node; + CustomScan *cscan = (CustomScan *) node->ss.ps.plan; + List *priv = cscan->custom_private; + Plan *child = (Plan *) linitial(cscan->custom_plans); + AnserChannelKey key; + uint32 part_index; + uint32 expected_parts; + int64 total_elems = intVal(list_nth(priv, ANSER_RF_PRIV_TOTAL_ELEMS)); + Size max_payload = (Size) intVal(list_nth(priv, ANSER_RF_PRIV_MAX_PAYLOAD)); + + st->key_attno = (AttrNumber) intVal(list_nth(priv, ANSER_RF_PRIV_KEY_ATTNO)); + st->planned_bytes = intVal(list_nth(priv, ANSER_RF_PRIV_PLANNED_BYTES)); + st->filter = NULL; + st->received = false; + st->defer_first = (intVal(list_nth(priv, ANSER_RF_PRIV_DEFER_FIRST)) != 0); + st->probed = false; + st->pushed_down = false; + + anser_rf_build_key(cscan, &key); + anser_rf_part_info(cscan, &part_index, &expected_parts); + + st->consume = ExecInitAnserBloomFilterConsume(&key, total_elems, max_payload, + expected_parts); + + node->custom_ps = list_make1(ExecInitNode(child, estate, eflags)); +} + +/* + * On first call, block to receive and union the bloom parts. A NULL filter + * (channel cancelled / unreachable / feature degraded) means fail open: pass + * every row through unfiltered. + */ +static void +anser_consume_receive(AnserBloomConsumeScanState *st) +{ + if (st->received) + return; + + /* + * Not on the first tuple, when the join is one that prefetches. + * + * ExecHashJoin pulls one tuple from its outer side -- us -- before it + * builds the hash table, so that it can skip the build when the outer side + * turns out to be empty (nodeHashjoin.c, the hj_FirstOuterTupleSlot fetch + * in HJ_BUILD_HASHTABLE). Waiting there waits for the inner side the same + * join has not started; when the join is co-located -- no Motion between + * the two, producer and consumer in one slice -- that is this very + * process, and nothing but the deadline ends it. + * + * Letting that one tuple through unfiltered costs a wasted probe and can + * never cost a row: the join still applies the real condition, and passing + * rows a filter would have rejected is the safe direction. By the next + * call the hash table is built, so the producer's scan has been drained + * and its part published. + * + * It is not free, which is why it is conditional: the scan has started by + * then, and anser_consume_pushdown() has to run before that to get its key + * into the scan descriptor. So a deferred consumer keeps the filter and + * probes it itself. The planner sets the flag whenever it cannot rule the + * prefetch out, which is the conservative direction -- guessing "no + * prefetch" wrongly deadlocks, guessing "prefetch" wrongly only gives up + * pushdown. + */ + if (st->defer_first && !st->probed) + { + st->probed = true; + return; + } + + st->received = true; + + if (st->consume == NULL) + return; + + if (ExecAnserBloomFilterConsume(st->consume, ANSER_RF_REGISTRATION_TIMEOUT_MS)) + st->filter = ExecAnserBloomFilterConsumerGetFilter(st->consume); + else + st->filter = NULL; + + /* + * Diagnostic: a NULL filter means we fail open (no pruning). Log why -- + * cancelled delivery vs. nothing arriving at all -- so runtime-filter + * misbehavior is visible in the server log. The count below is what the + * payload declared the channel's producer count to be, not a tally of + * arrivals: the coordinator never delivers a channel short of its parts, + * so there is no such tally to report here. + */ + if (st->filter == NULL) + elog(LOG, + "anser bloom consumer: no filter, failing open (cancelled=%d, declared_parts=%u)", + ExecAnserBloomFilterConsumerWasCancelled(st->consume), + ExecAnserBloomFilterConsumerReceivedParts(st->consume)); +} + +/* + * Is this PlanState one of our own nodes? + * + * The exec method table is the thing to compare, because our own + * CreateCustomScanState callback assigns it (anser_consume_create_state) in + * the process that is about to run the node. It is a plain in-process pointer + * with nothing in between, so the comparison means the same thing wherever it + * runs -- which matters, because a consumer executes on the coordinator as + * well as on a segment: a slice with GANGTYPE_UNALLOCATED has no gang, and + * ExecAnserBloomFilterConsume takes the AnserDispatchLocalConsume path for it. + * + * AnserIsRuntimeFilterScan answers the same question about a Plan, and is what + * the injection pass uses. Either would work here; this walk has the PlanState + * in hand, so it asks the shorter question. + */ +static bool +anser_is_rf_scan_state(const PlanState *ps) +{ + const CustomScanState *css; + + if (ps == NULL || !IsA(ps, CustomScanState)) + return false; + + css = (const CustomScanState *) ps; + return css->methods == &anser_produce_exec_methods || + css->methods == &anser_consume_exec_methods; +} + +/* + * Can `ps` take an SK_BLOOM_FILTER scan key for the column it emits at output + * targetlist position `attno`? On true, *sk_attno_out is the attribute number + * the key must carry; on false it is InvalidAttrNumber. + * + * `attno` is a position in the node's *output* targetlist, which is how + * anser_consume_next numbers things, because the slot it probes has already + * been projected. A scan key is numbered differently: PassByBloomFilter reads + * ss_ScanTupleSlot -- the table tuple, before projection -- so the key has to + * carry the table's own attribute number. The two coincide only when the scan + * emits every column in order; for a projecting scan they do not, and + * filtering the wrong column drops rows that can join. On probe(payload, id) + * with a targetlist of just [id], `attno` is 1 while the table attno is 2: + * a key carrying 1 tests `payload` against a filter built on `id` and discards + * every row. + * + * Only a SeqScan is accepted, because nodeSeqscan.c is the only consumer of + * this scan-key shape that sees an unprojected table tuple. DynamicSeqScan + * also carries a `filters` list, but it applies the filter to its child + * SeqScan's *projected* slot and remaps varattnos per partition, so it would + * need different numbering -- and the injection pass cannot reach one anyway, + * since AnserResolveKeyScan only terminates on a plain SeqScan. + */ +bool +AnserPushdownAccepts(PlanState *ps, AttrNumber attno, AttrNumber *sk_attno_out) +{ + SeqScanState *sss; + List *tlist; + TargetEntry *tle; + Var *var; + + if (sk_attno_out != NULL) + *sk_attno_out = InvalidAttrNumber; + + if (ps == NULL || !IsA(ps, SeqScanState)) + return false; + sss = (SeqScanState *) ps; + + /* + * With filter_in_seqscan off the scan hands its keys to the table AM + * instead of evaluating them itself, and nothing guarantees the AM + * understands SK_BLOOM_FILTER. + */ + if (!sss->filter_in_seqscan) + return false; + + tlist = ps->plan->targetlist; + if (attno < 1 || attno > list_length(tlist)) + return false; + tle = (TargetEntry *) list_nth(tlist, attno - 1); + if (tle == NULL || !IsA(tle->expr, Var)) + return false; /* a computed column has no table attno */ + var = (Var *) tle->expr; + + /* + * Only a plain user column of this very relation can be pushed. A + * whole-row reference (varattno 0) or a system column (negative) is not + * something PassByBloomFilter can hash the way the producer did, and a Var + * belonging to another range table entry would send the scan looking at an + * attno that means nothing to it. + */ + if (var->varno != ((Scan *) ps->plan)->scanrelid || var->varattno < 1) + return false; + + if (sk_attno_out != NULL) + *sk_attno_out = var->varattno; + return true; +} + +/* + * Walk down from `top` following the column at output position `attno`, and + * return the deepest node that would accept a scan key for it -- or NULL if + * none would. *sk_attno_out is the attribute number for that node. + * + * Deepest, because a filter applied further down rejects the row before more + * of the plan has touched it; this is the same reason RPT+ pushes its min-max + * filters "to the deepest possible operators". The walk matters because our + * nodes stack: when two joins trace their probe key to the same relation, the + * second consumer is inserted *under* the first, so the upper one's child is a + * CustomScanState rather than the scan. Before this walk existed the upper + * consumer simply declined and probed every tuple itself. + * + * Descent follows the column's provenance, so it is a path rather than a tree + * sweep: at each step exactly one child supplies the column. It continues + * only through our own nodes, which is the complete set of things that can sit + * between a consumer and its scan -- the injection pass puts the consumer + * directly above the scan, and the only thing that later gets between them is + * another one of ours. + */ +PlanState * +AnserPushdownTarget(PlanState *top, AttrNumber attno, AttrNumber *sk_attno_out) +{ + PlanState *best = NULL; + AttrNumber best_sk = InvalidAttrNumber; + PlanState *cur = top; + AttrNumber cur_attno = attno; + + while (cur != NULL) + { + AttrNumber sk_attno; + List *tlist; + TargetEntry *tle; + Var *var; + + if (AnserPushdownAccepts(cur, cur_attno, &sk_attno)) + { + best = cur; + best_sk = sk_attno; + } + + if (!anser_is_rf_scan_state(cur)) + break; + + /* Follow the column into our child, remapping the position. */ + tlist = cur->plan->targetlist; + if (cur_attno < 1 || cur_attno > list_length(tlist)) + break; + tle = (TargetEntry *) list_nth(tlist, cur_attno - 1); + if (tle == NULL || !IsA(tle->expr, Var)) + break; + var = (Var *) tle->expr; + if (var->varno != INDEX_VAR || var->varattno < 1) + break; /* not the identity tlist we build */ + + cur_attno = var->varattno; + cur = (PlanState *) linitial(((CustomScanState *) cur)->custom_ps); + } + + if (sk_attno_out != NULL) + *sk_attno_out = best_sk; + return best; +} + +/* + * Hand the received filter to the deepest scan that will take it, as an + * SK_BLOOM_FILTER scan key -- the exact structure PassByBloomFilter consumes + * (nodeSeqscan.c), so the scan does the pruning itself and a table AM that + * supports SCAN_SUPPORT_RUNTIME_FILTER may additionally use the key for + * block-level pruning. Runs before the child's first ExecProcNode, so the key + * is in node->filters when the scan descriptor is created. The filter stays + * owned by us; anser_consume_end ends the child before freeing it. + * + * Declining (leaving pushed_down false) costs nothing but the pushdown: the + * caller then probes the filter itself in anser_consume_next. + */ +static void +anser_consume_pushdown(CustomScanState *node) +{ + AnserBloomConsumeScanState *st = (AnserBloomConsumeScanState *) node; + PlanState *child = (PlanState *) linitial(node->custom_ps); + PlanState *target; + AttrNumber sk_attno; + ScanKey sk; + MemoryContext oldcxt; + + if (!gp_enable_runtime_filter_pushdown) + return; + + target = AnserPushdownTarget(child, st->key_attno, &sk_attno); + if (target == NULL) + return; + + /* AnserPushdownAccepts admits nothing else, and the cast below needs it. */ + Assert(IsA(target, SeqScanState)); + + oldcxt = MemoryContextSwitchTo(node->ss.ps.state->es_query_cxt); + sk = (ScanKey) palloc0(sizeof(ScanKeyData)); + sk->sk_attno = sk_attno; + sk->sk_flags = SK_BLOOM_FILTER; + sk->sk_argument = PointerGetDatum(st->filter); + ((SeqScanState *) target)->filters = + lappend(((SeqScanState *) target)->filters, sk); + MemoryContextSwitchTo(oldcxt); + + st->pushed_down = true; +} + +static TupleTableSlot * +anser_consume_next(CustomScanState *node) +{ + AnserBloomConsumeScanState *st = (AnserBloomConsumeScanState *) node; + PlanState *child = (PlanState *) linitial(node->custom_ps); + + anser_consume_receive(st); + + /* + * When the pushdown machinery is enabled, hand the filter to the scan and + * pass tuples through untouched; otherwise probe the filter here. + */ + if (st->filter != NULL && !st->pushed_down) + anser_consume_pushdown(node); + + for (;;) + { + TupleTableSlot *slot = ExecProcNode(child); + bool isnull; + Datum value; + + if (TupIsNull(slot)) + return NULL; + + /* + * Fail open (no usable filter) or pushed down (the scan filters via + * PassByBloomFilter): pass everything. + */ + if (st->filter == NULL || st->pushed_down) + return anser_rf_child_slot(node, slot); + + /* + * key_attno is an output-targetlist position, which is how this slot + * is numbered: it has already been projected. The pushdown path sees + * the table tuple instead and needs the table attno -- see + * anser_consume_pushdown. + */ + value = slot_getattr(slot, st->key_attno, &isnull); + + /* NULLs never match an equijoin key; let the join handle them. */ + if (!isnull) + { + /* + * Count lookups/prunes via instrumentation (nfiltered1/nfiltered2), + * not local state: these fields ride the CdbExplain_StatInst wire + * format back to the QD, so EXPLAIN ANALYZE shows them for + * segment-executed nodes too. + */ + InstrCountFiltered1(node, 1); + if (bloom_lacks_element(st->filter, (unsigned char *) &value, + sizeof(Datum))) + { + /* Definitely absent from the build side -> prune. */ + InstrCountFiltered2(node, 1); + continue; + } + } + + return anser_rf_child_slot(node, slot); + } +} + +static TupleTableSlot * +anser_consume_exec(CustomScanState *node) +{ + return ExecScan(&node->ss, + (ExecScanAccessMtd) anser_consume_next, + (ExecScanRecheckMtd) anser_rf_recheck); +} + +static void +anser_consume_end(CustomScanState *node) +{ + AnserBloomConsumeScanState *st = (AnserBloomConsumeScanState *) node; + + /* + * End the child first: when the filter was pushed down, the child's scan + * key references it, so the filter must outlive the child node. + */ + if (node->custom_ps != NIL) + ExecEndNode((PlanState *) linitial(node->custom_ps)); + if (st->consume != NULL) + ExecEndAnserBloomFilterConsume(st->consume); + st->consume = NULL; + st->filter = NULL; +} + +/* Keeps the received filter; see anser_produce_rescan for why that is sound. */ +static void +anser_consume_rescan(CustomScanState *node) +{ + if (node->custom_ps != NIL) + ExecReScan((PlanState *) linitial(node->custom_ps)); +} + +static void +anser_consume_explain(CustomScanState *node, List *ancestors, ExplainState *es) +{ + AnserBloomConsumeScanState *st = (AnserBloomConsumeScanState *) node; + + ExplainPropertyInteger("Bloom Filter Size", "bytes", st->planned_bytes, es); + + if (es->analyze && node->ss.ps.instrument != NULL) + { + char buf[128]; + double nloops = node->ss.ps.instrument->nloops; + double nfiltered = node->ss.ps.instrument->nfiltered2; + double nchecked = node->ss.ps.instrument->nfiltered1; + + /* + * checked/rejected come from nfiltered1/nfiltered2, which cdbexplain + * transports from the segments and deposits into this node's + * instrument. As with all per-node EXPLAIN ANALYZE stats in MPP, + * these are the winning segment's values (max ntuples/nloops), not a + * cluster-wide sum. memory is planned_bytes: the unioned filter's + * size is identical on every segment by construction (AnserRuntimeFilterSize). + * When the filter was pushed into the child scan, pruning happens + * there instead (its "Rows Removed by Pushdown Runtime Filter" line), + * so our counters stay zero. + */ + if (st->pushed_down) + snprintf(buf, sizeof(buf), + "memory=" INT64_FORMAT "kB (pushed down to Seq Scan)", + st->planned_bytes / 1024); + else + snprintf(buf, sizeof(buf), + "memory=" INT64_FORMAT "kB checked=%.0f rejected=%.0f", + st->planned_bytes / 1024, nchecked, nfiltered); + ExplainPropertyText("Bloom Filter Stats", buf, es); + + if (!st->pushed_down && nloops > 0) + ExplainPropertyFloat("Rows Removed by Bloom Filter", NULL, + nfiltered / nloops, 0, es); + } +} diff --git a/gpcontrib/anser/src/ansersideband.c b/gpcontrib/anser/src/ansersideband.c new file mode 100644 index 00000000000..24161df52da --- /dev/null +++ b/gpcontrib/anser/src/ansersideband.c @@ -0,0 +1,995 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * ansersideband.c + * Segment side of the dispatch-connection transport. + * + * A producer sends its part as a NOTIFY and moves on; a consumer subscribes, + * then blocks on its own dispatch socket until the coordinator pushes the + * merged filter back. Both directions reuse the connection the dispatcher + * already owns, so there is no second connection to open and nothing to + * authenticate. + * + * Reading the frontend socket in the middle of executing a query is the + * pattern cdb_sequence_nextval_qe() established (commands/sequence.c); the + * loop below mirrors its use of pq_startmsgread/pq_getbyte_if_available, but + * sleeps on the socket instead of spinning. + * + * IDENTIFICATION + * gpcontrib/anser/src/ansersideband.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "anser.h" +#include "ansersideband.h" +#include "cdb/cdbvars.h" +#include "commands/async.h" +#include "common/base64.h" +#include "libpq/libpq-be.h" +#include "libpq/libpq.h" +#include "libpq/pqformat.h" +#include "miscadmin.h" +#include "nodes/pg_list.h" +#include "port/pg_bswap.h" +#include "port/pg_crc32c.h" +#include "portability/instr_time.h" +#include "storage/latch.h" +#include "tcop/dest.h" +#include "tcop/tcopprot.h" +#include "utils/faultinjector.h" +#include "utils/memutils.h" +#include "utils/wait_event.h" + +/* How long to sleep between wakeups while waiting for a delivery. */ +#define ANSER_SIDEBAND_POLL_MS 100L + +/* + * Delivery state: one live subscription, one assembly slot. + * + * With pull-based chunking there is at most one live subscription per + * process: AnserSidebandConsumeWait() is called from one place + * (anserbloomconsume.c), its loop never runs the executor, so waits cannot + * nest, and every exit path clears the flag. And nothing arrives without a + * subscription: the coordinator sends a chunk only after a SUBSCRIBE or a + * NEXT. So the push model's inbox -- several parked deliveries, one per + * channel -- collapses to a single slot, and the cases "several entries" and + * "an entry for someone else" are impossible by construction rather than by + * argument. + * + * The full-key match is still what rejects a late chunk from an earlier + * command: AnserWaitKey carries this command's session and command ids, so a + * chunk from an older one never enters the slot. + */ +typedef struct AnserInboxEntry +{ + AnserChannelKey key; /* for the trace lines; always AnserWaitKey */ + char payload_type; + char *payload; /* the assembly buffer; NULL when cancelled */ + Size total_len; /* declared full size; 0 when cancelled */ + Size received; /* bytes assembled so far */ + instr_time last_chunk_at; /* when the latest chunk was accepted */ + bool complete; /* received == total_len, or cancelled */ + bool cancelled; +} AnserInboxEntry; + +static bool AnserWaiting = false; /* a subscription is live... */ +static AnserChannelKey AnserWaitKey; /* ...for this key */ +static AnserInboxEntry *AnserSlot = NULL; /* its assembly, or NULL */ + +static bool anser_sideband_send(const char *payload); +static bool anser_inbox_take(const AnserChannelKey *key, char payload_type, + void **payload, Size *payload_len, bool *cancelled); +static bool anser_sideband_read_one(long timeout_ms); +static bool anser_key_equal(const AnserChannelKey *a, const AnserChannelKey *b); +static void anser_slot_free(void); + +static bool +anser_key_equal(const AnserChannelKey *a, const AnserChannelKey *b) +{ + return a->condition_id == b->condition_id && + a->gp_session_id == b->gp_session_id && + a->gp_command_count == b->gp_command_count && + strncmp(a->condition_key, b->condition_key, + ANSER_CONDITION_KEY_SIZE) == 0; +} + +/* Free the assembly slot, whatever state the delivery is in. */ +static void +anser_slot_free(void) +{ + if (AnserSlot != NULL) + { + ANSER_DEBUG("anser: seg%d dropping delivery cond=%u pushed for ssid=%d ccnt=%d (%zu of %zu bytes, complete=%d)", + GpIdentity.segindex, AnserSlot->key.condition_id, + AnserSlot->key.gp_session_id, AnserSlot->key.gp_command_count, + AnserSlot->received, AnserSlot->total_len, + AnserSlot->complete ? 1 : 0); + if (AnserSlot->payload != NULL) + pfree(AnserSlot->payload); + pfree(AnserSlot); + AnserSlot = NULL; + } +} + +/* + * Publish one part. Fire-and-forget: the coordinator does not acknowledge, + * because nothing on this side needs to wait for it. + */ +bool +AnserSidebandPublish(const AnserChannelKey *channel_key, char payload_type, + uint32 part_index, uint32 total_parts, + const void *payload, Size payload_len, bool cancelled) +{ + char *msg; + int flags = cancelled ? ANSER_WIRE_F_CANCELLED : 0; + bool ok; + + if (channel_key == NULL) + return false; + + if (!cancelled && payload_len > (Size) gp_anser_max_info_size) + { + /* Too large to ship; tell the coordinator so consumers stop waiting. */ + flags = ANSER_WIRE_F_CANCELLED; + payload = NULL; + payload_len = 0; + } + + msg = AnserWireFormat(channel_key, ANSER_WIRE_KIND_PART, payload_type, + part_index, total_parts, flags, + (flags & ANSER_WIRE_F_CANCELLED) ? NULL : payload, + (flags & ANSER_WIRE_F_CANCELLED) ? 0 : payload_len); + ok = anser_sideband_send(msg); + ANSER_DEBUG("anser: seg%d published cond=%u type=%c part=%u/%u bytes=%zu cancelled=%d sent=%d", + GpIdentity.segindex, channel_key->condition_id, payload_type, + part_index, total_parts, payload_len, + (flags & ANSER_WIRE_F_CANCELLED) ? 1 : 0, ok ? 1 : 0); + pfree(msg); + + return ok; +} + +/* + * Withdraw a subscription, in full: tell the coordinator, forget our own + * record of it, and drop the delivery slot whatever state it is in -- a + * completed but unclaimed payload is just as dead once its consumer walks + * away. Called on every path out of AnserSidebandConsumeWait that leaves a + * subscription behind (deadline expiry, query finishing early). + * + * With chunked delivery this closes the problem rather than narrowing a + * window: the coordinator stops at the chunk boundary, at most one whole + * chunk can still be in flight, and read_one drops it because the slot has no + * live subscription -- or, if we have already left the wait, the command loop + * skips the message whole (postgres.c, GP_SIDEBAND_MESSAGE). A send failure + * is only logged: the coordinator then keeps a stalled delivery until the + * channel is reset, which costs it nothing further. + */ +static void +anser_sideband_withdraw(const AnserChannelKey *channel_key) +{ + char *msg = AnserWireFormat(channel_key, ANSER_WIRE_KIND_UNSUBSCRIBE, + ANSER_PAYLOAD_NONE, 0, 0, 0, NULL, 0); + + if (!anser_sideband_send(msg)) + ANSER_DEBUG("anser: seg%d could not unsubscribe from cond=%u", + GpIdentity.segindex, channel_key->condition_id); + pfree(msg); + + AnserWaiting = false; + anser_slot_free(); +} + +/* + * Subscribe, then wait for the merged payload. + * + * Returns true with *payload set, or false for "run unfiltered" -- including + * on timeout. The deadline exists because a producer that gets squelched + * never publishes anything: ExecSquelchNode does not call CustomScan + * callbacks, it only marks the node (execAmi.c), so without a deadline this + * wait could outlive the reason for it. + */ +bool +AnserSidebandConsumeWait(const AnserChannelKey *channel_key, char payload_type, + void **payload, Size *payload_len, + bool *cancelled, long timeout_ms) +{ + char *msg; + instr_time subscribe_start; + instr_time now; + + if (payload != NULL) + *payload = NULL; + if (payload_len != NULL) + *payload_len = 0; + if (cancelled != NULL) + *cancelled = false; + + if (channel_key == NULL || MyProcPort == NULL || + MyProcPort->sock == PGINVALID_SOCKET) + { + /* + * No way to reach the coordinator from here, so this consumer will run + * unfiltered without ever having asked for anything. Which of the + * three reasons it was matters: a process with no Port is one the + * sideband was never designed for, while a bad socket on a process + * that has one is a different problem entirely. + */ + ANSER_DEBUG("anser: seg%d cannot consume cond=%u: key=%s port=%s sock=%d role=%d writer=%d dest=%d", + GpIdentity.segindex, + channel_key != NULL ? channel_key->condition_id : 0, + channel_key != NULL ? "ok" : "NULL", + MyProcPort != NULL ? "ok" : "NULL", + MyProcPort != NULL ? (int) MyProcPort->sock : -1, + (int) Gp_role, Gp_is_writer ? 1 : 0, + (int) whereToSendOutput); + return false; + } + + /* + * With no deadline to wait out there is nothing to subscribe for: we would + * give up on the next line anyway, and the only lasting effect would be a + * stalled delivery on the coordinator. So do not ask in the first place. + */ + if (timeout_ms == 0) + { + ANSER_DEBUG("anser: seg%d not subscribing to cond=%u: no deadline to wait out", + GpIdentity.segindex, channel_key->condition_id); + return false; + } + + /* + * One subscription at a time, by construction -- see the slot's comment. + * A slot left over from an error exit (PG error unwinding passes none of + * the exits below) is always stale: its command count is not ours. + */ + Assert(!AnserWaiting); + if (AnserSlot != NULL) + anser_slot_free(); + + /* + * A subscription carries nothing, so its payload type is NONE -- what this + * consumer expects to receive is checked on delivery, not announced here. + * The key is marked live before anything can arrive: read_one drops chunks + * unless the subscription is, which is how a delivery whose consumer has + * given up ends at its current chunk. + */ + msg = AnserWireFormat(channel_key, ANSER_WIRE_KIND_SUBSCRIBE, + ANSER_PAYLOAD_NONE, 0, 0, 0, NULL, 0); + if (!anser_sideband_send(msg)) + { + ANSER_DEBUG("anser: seg%d could not subscribe to cond=%u (role=%d writer=%d dest=%d)", + GpIdentity.segindex, channel_key->condition_id, + (int) Gp_role, Gp_is_writer ? 1 : 0, + (int) whereToSendOutput); + pfree(msg); + return false; + } + pfree(msg); + AnserWaitKey = *channel_key; + AnserWaiting = true; + ANSER_DEBUG("anser: seg%d subscribed cond=%u, waiting up to %ld ms", + GpIdentity.segindex, channel_key->condition_id, timeout_ms); + + INSTR_TIME_SET_CURRENT(subscribe_start); + for (;;) + { + CHECK_FOR_INTERRUPTS(); + + /* + * The coordinator has what it needs and told us to wrap up -- a LIMIT + * that filled, typically. CHECK_FOR_INTERRUPTS does not cover this: + * it is a flag the executor is expected to notice, which is why + * nodeHashjoin.c tests it beside the same call. Without that, a + * finished query is held open by a consumer still waiting on a filter + * nobody will use, for up to anser.timeout_ms. + */ + if (QueryFinishPending) + { + ANSER_DEBUG("anser: seg%d abandoning cond=%u: the query is finishing", + GpIdentity.segindex, channel_key->condition_id); + + /* + * Same reason the deadline path withdraws: the query is wrapping + * up, so nobody here will read the filter the coordinator is + * about to push. + */ + anser_sideband_withdraw(channel_key); + return false; + } + + if (anser_inbox_take(channel_key, payload_type, payload, payload_len, + cancelled)) + { + AnserWaiting = false; + return payload != NULL && *payload != NULL; + } + + /* + * The deadline guards against a delivery that stalls: it counts from + * the last accepted chunk (or from the subscribe while none has + * arrived), not from the subscribe alone -- aborting a healthy + * delivery mid-flight for being large would only waste the work + * already done. A delivery that trickles just under the deadline + * forever must still end, so a hard cap a few times the deadline + * counts from the subscribe. + */ + if (timeout_ms >= 0) + { + double since_progress; + double since_subscribe; + instr_time *last = (AnserSlot != NULL) ? + &AnserSlot->last_chunk_at : &subscribe_start; + + INSTR_TIME_SET_CURRENT(now); + INSTR_TIME_SUBTRACT(now, *last); + since_progress = INSTR_TIME_GET_MILLISEC(now); + + INSTR_TIME_SET_CURRENT(now); + INSTR_TIME_SUBTRACT(now, subscribe_start); + since_subscribe = INSTR_TIME_GET_MILLISEC(now); + + if (since_progress >= timeout_ms || + since_subscribe >= (double) timeout_ms * 4) + { + /* + * Withdraw the subscription on the way out. From here on + * this process is somewhere else in the plan, and the + * chunked delivery stops at its current chunk once the + * withdrawal lands. + * + * Withdraw frees the slot, so read the totals first: "gave up + * having received 0 of 0" and "never started" should not look + * alike in the log. + */ + Size got = AnserSlot != NULL ? AnserSlot->received : 0; + Size total = AnserSlot != NULL ? AnserSlot->total_len : 0; + + anser_sideband_withdraw(channel_key); + + ANSER_DEBUG("anser: seg%d gave up on cond=%u after %ld ms (received %zu of %zu bytes)", + GpIdentity.segindex, channel_key->condition_id, + timeout_ms, got, total); + return false; + } + } + + /* + * The chunk we read lands in the slot; the loop then rechecks whether + * the delivery is complete. A read failure means the connection is + * gone, which the interconnect will report far more usefully than we + * can -- stop waiting and let the query run unfiltered. + */ + if (!anser_sideband_read_one(ANSER_SIDEBAND_POLL_MS)) + { + anser_sideband_withdraw(channel_key); + return false; + } + } +} + +/* + * Wait briefly for one sideband message and stash it in the inbox. + * + * Returns false only when the connection is unusable; a timeout with nothing + * to read is a normal true. + */ +static bool +anser_sideband_read_one(long timeout_ms) +{ + unsigned char qtype; + int retval; + int64 maxlen; + StringInfoData buf; + AnserChannelKey key; + MemoryContext oldcxt; + char paytype; + uint32 crc; + int condid; + uint32 session_id; + uint32 command_count; + int flags; + uint32 total_len; + uint32 offset; + int keylen; + int chunklen; + const char *keyptr; + const char *payptr; + + pq_startmsgread(); + retval = pq_getbyte_if_available(&qtype); + if (retval == 0) + { + /* Nothing buffered: sleep on the socket rather than spinning. */ + pq_endmsgread(); + + ResetLatch(MyLatch); + (void) WaitLatchOrSocket(MyLatch, + WL_LATCH_SET | WL_SOCKET_READABLE | + WL_TIMEOUT | WL_EXIT_ON_PM_DEATH, + MyProcPort->sock, + timeout_ms, + PG_WAIT_EXTENSION); + return true; + } + + if (retval == EOF) + { + pq_endmsgread(); + elog(LOG, "anser: dispatch connection closed while awaiting a filter"); + return false; + } + + if (qtype != GP_SIDEBAND_MESSAGE) + { + /* + * The one type that can legitimately turn up is '?', the nextval + * response: the QD sends it and cancels the QEs when it fails to get a + * value, so it can arrive with nobody waiting for it. We do not skip + * it, because that would consume a reply a later nextval might be + * about to wait for, and we are not the owner of that exchange. Other + * types should not reach us at all. + * + * Either way there is no way back by the time we are here: + * pq_getbyte_if_available() has already taken the byte, and the + * backend cannot push one in front of the stream, so the message can + * neither be handed to the command loop nor left for its owner. The + * connection is one byte short from here on. + * + * Terminating is therefore the only honest outcome. Returning and + * running unfiltered -- the instinct everywhere else in this code -- + * would be much worse than losing the filter: every later read lands + * at the wrong offset, and the failure surfaces somewhere else + * entirely as an absurd allocation request or "invalid frontend + * message type". It has to be FATAL rather than ERROR because the + * connection cannot be reused: an ERROR would return this QE to the + * gang pool with a desynchronised stream, poisoning a later query. + * + * The mirror case -- a sideband message reaching the nextval reader -- + * is the common one, and that reader skips it rather than dying: it + * knows our message is unowned by then, which we cannot say of its. + */ + pq_endmsgread(); + ereport(FATAL, + (errcode(ERRCODE_PROTOCOL_VIOLATION), + errmsg("anser: unexpected message type '%c' while awaiting a filter", + (char) qtype), + errdetail("The message boundary was lost; this connection cannot be used further."))); + } + + /* + * In int64 and clamped, though anser.max_info_size is bounded so that it + * cannot overflow this on its own. The bound lives in another file; this + * costs nothing and does not depend on the two staying in step. + */ + maxlen = (int64) gp_anser_max_info_size + ANSER_SIDEBAND_FRAME_ROOM; + if (maxlen > (int64) PQ_LARGE_MESSAGE_LIMIT) + maxlen = (int64) PQ_LARGE_MESSAGE_LIMIT; + + initStringInfo(&buf); + if (pq_getmessage(&buf, (int) maxlen) != 0) + { + /* + * Same reasoning as the message type above, and the same outcome. + * Every path on which pq_getmessage returns EOF leaves us unable to go + * on: either the peer is gone, or -- when the length word exceeds the + * cap -- it has taken those four bytes and left the body in the stream + * (pqcomm.c discards the body only on the paths where it already read + * it). Running unfiltered from here would hand the gang pool a + * connection whose next read lands mid-message. + * + * The cap can be exceeded for real: gp_anser_max_info_size is USERSET, + * and a gang busy with a named portal does not receive a dispatched + * SET -- see cdbdisp_markNamedPortalGangsDestroyed(). So this is a + * reachable disagreement, not a corrupt-stream-only case. + * + * pq_getmessage clears the reading-message flag when it succeeds but + * not on its EOF paths; clear it by hand so the error path does not + * also trip the assertion in a later pq_startmsgread. + */ + pq_endmsgread(); + pfree(buf.data); + ereport(FATAL, + (errcode(ERRCODE_PROTOCOL_VIOLATION), + errmsg("anser: could not read the filter message"), + errdetail("The message boundary was lost; this connection cannot be used further."))); + } + + paytype = (char) pq_getmsgint(&buf, 4); + crc = (uint32) pq_getmsgint(&buf, 4); + condid = pq_getmsgint(&buf, 4); + session_id = (uint32) pq_getmsgint(&buf, 4); + command_count = (uint32) pq_getmsgint(&buf, 4); + flags = pq_getmsgint(&buf, 4); + total_len = (uint32) pq_getmsgint(&buf, 4); + offset = (uint32) pq_getmsgint(&buf, 4); + keylen = pq_getmsgint(&buf, 4); + if (keylen < 0 || keylen >= ANSER_CONDITION_KEY_SIZE) + { + pfree(buf.data); + elog(LOG, "anser: filter message has a bad condition key"); + return false; + } + keyptr = pq_getmsgbytes(&buf, keylen); + chunklen = pq_getmsgint(&buf, 4); + if (chunklen < 0 || (uint32) chunklen > total_len || + total_len > (uint32) gp_anser_max_info_size || + offset > total_len || (uint32) chunklen > total_len - offset) + { + pfree(buf.data); + elog(LOG, "anser: filter message has a bad chunk frame"); + return false; + } + payptr = chunklen > 0 ? pq_getmsgbytes(&buf, chunklen) : NULL; + + if (crc != (uint32) AnserWirePushCrc(paytype, (uint32) condid, + session_id, command_count, + (uint32) flags, total_len, offset, + keyptr, keylen, + payptr, chunklen)) + { + /* + * Drop it and keep waiting: the deadline will expire and this consumer + * will run unfiltered. Using the filter anyway is the one thing we must + * not do -- a bit corrupted 1 -> 0 silently drops joinable rows. + */ + pfree(buf.data); + elog(LOG, "anser: checksum mismatch on filter for condition %u; discarding", + (uint32) condid); + return true; + } + + /* + * The identity comes from the wire, never from our own globals: a message + * parked from an earlier command keeps that command's ids, so a later + * statement can never claim it by matching its own. + */ + key.gp_session_id = (int) session_id; + key.gp_command_count = (int) command_count; + key.condition_id = (uint32) condid; + memcpy(key.condition_key, keyptr, keylen); + key.condition_key[keylen] = '\0'; + + /* + * A chunk for anything but the live subscription belongs to a delivery + * whose consumer has already given up. Drop it without answering: with no + * NEXT the delivery ends at its current chunk, and nothing is left in + * flight to break the stream. + */ + if (!AnserWaiting || !anser_key_equal(&AnserWaitKey, &key)) + { + ANSER_DEBUG("anser: seg%d dropping chunk cond=%u off=%u len=%d: not subscribed", + GpIdentity.segindex, (uint32) condid, offset, chunklen); + pfree(buf.data); + return true; + } + + if (flags & ANSER_WIRE_F_CANCELLED) + { + /* + * A cancelled delivery is a single chunk with total_len = 0. It + * completes the slot at once; any partial assembly is replaced. + */ + if (AnserSlot == NULL) + { + oldcxt = MemoryContextSwitchTo(TopMemoryContext); + AnserSlot = palloc0(sizeof(AnserInboxEntry)); + MemoryContextSwitchTo(oldcxt); + } + else if (AnserSlot->payload != NULL) + { + pfree(AnserSlot->payload); + } + AnserSlot->key = key; + AnserSlot->payload_type = paytype; + AnserSlot->payload = NULL; + AnserSlot->cancelled = true; + AnserSlot->complete = true; + AnserSlot->total_len = 0; + AnserSlot->received = 0; + INSTR_TIME_SET_CURRENT(AnserSlot->last_chunk_at); + pfree(buf.data); + return true; + } + + if (AnserSlot == NULL) + { + if (offset != 0) + { + /* The window never reopens: out of order means desync. */ + elog(LOG, "anser: chunk for condition %u starts at %u, expected 0; discarding", + (uint32) condid, offset); + pfree(buf.data); + return true; + } + + /* + * The slot outlives this call and the memory context it was reached + * in, so anchor it somewhere stable; AnserSidebandResetInbox drops it. + * The assembly buffer is sized by the declared total_len, already + * checked against anser.max_info_size above. + */ + oldcxt = MemoryContextSwitchTo(TopMemoryContext); + AnserSlot = palloc0(sizeof(AnserInboxEntry)); + AnserSlot->key = key; + AnserSlot->payload_type = paytype; + AnserSlot->total_len = total_len; + AnserSlot->payload = total_len > 0 ? palloc(total_len) : NULL; + MemoryContextSwitchTo(oldcxt); + } + else if (AnserSlot->complete || AnserSlot->received != offset || + AnserSlot->total_len != total_len || + AnserSlot->payload_type != paytype) + { + /* + * A duplicate, a gap, or a frame that disagrees with the first chunk: + * the delivery is desynced, the stream is not. The total_len and + * payload_type checks are what keep the memcpy below inside the + * buffer the first chunk sized. + */ + elog(LOG, "anser: chunk for condition %u at %u (total %u), have %zu of %zu; discarding", + (uint32) condid, offset, total_len, + AnserSlot->received, AnserSlot->total_len); + pfree(buf.data); + return true; + } + + /* + * Ask for the following chunk before copying this one, so the coordinator + * works while we copy. A NEXT names the offset we will hold after it. + */ + if (AnserSlot->received + (uint32) chunklen < total_len) + { + char *next = AnserWireFormat(&key, ANSER_WIRE_KIND_NEXT, + ANSER_PAYLOAD_NONE, + AnserSlot->received + (uint32) chunklen, + 0, 0, NULL, 0); + + if (!anser_sideband_send(next)) + ANSER_DEBUG("anser: seg%d could not ask for the next chunk of cond=%u", + GpIdentity.segindex, (uint32) condid); + pfree(next); + } + + if (chunklen > 0) + memcpy(AnserSlot->payload + offset, payptr, chunklen); + AnserSlot->received += (uint32) chunklen; + if (AnserSlot->received == total_len) + AnserSlot->complete = true; + INSTR_TIME_SET_CURRENT(AnserSlot->last_chunk_at); + + /* Log only the first and last chunk; a delivery is hundreds of them. */ + if (offset == 0 || AnserSlot->complete) + ANSER_DEBUG("anser: seg%d received cond=%u type=%c chunk=[%u,%zu) of %u cancelled=0", + GpIdentity.segindex, (uint32) condid, paytype, + offset, AnserSlot->received, total_len); + +#ifdef FAULT_INJECTOR + /* + * Let a test pause a consumer mid-delivery, past its deadline: the next + * loop iteration finds it expired and withdraws -- the walk-away path. + * + * Not a 'sleep' fault: that is a bare nanosleep() (pgsleep.c), which any + * signal cuts short, and a QE does get signals mid-query, so the pause + * ends well inside the deadline and the whole delivery completes. So the + * fault only says "pause", and this waits out the pause on the monotonic + * clock, re-arming after every early wakeup. It stays cancellable, and + * it does not touch the socket: the chunk it fires for stays queued, to + * be dropped as unsubscribed after the withdrawal. + */ + if (SIMPLE_FAULT_INJECTOR("anser_after_chunk") == FaultInjectorTypeSkip) + { + double pause_ms = (double) gp_anser_timeout_ms + 100; + instr_time pause_start; + instr_time now; + + INSTR_TIME_SET_CURRENT(pause_start); + for (;;) + { + double left; + + CHECK_FOR_INTERRUPTS(); + + INSTR_TIME_SET_CURRENT(now); + INSTR_TIME_SUBTRACT(now, pause_start); + left = pause_ms - INSTR_TIME_GET_MILLISEC(now); + if (left <= 0) + break; + + (void) WaitLatch(MyLatch, + WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH, + (long) left + 1, + PG_WAIT_EXTENSION); + ResetLatch(MyLatch); + } + } +#endif + + pfree(buf.data); + return true; +} + +/* Claim the delivery if it is complete. */ +static bool +anser_inbox_take(const AnserChannelKey *key, char payload_type, void **payload, + Size *payload_len, bool *cancelled) +{ + AnserInboxEntry *entry = AnserSlot; + + if (entry == NULL || !entry->complete) + return false; + + /* + * The slot can only hold the live subscription's delivery, so a mismatch + * here is a bug, not a late message: assert in assert builds, and end the + * wait without the filter rather than log it every 100 ms until the + * deadline. + */ + if (!anser_key_equal(&entry->key, key)) + { + Assert(false); + elog(LOG, "anser: inbox slot for condition %u does not match the waiter %u", + entry->key.condition_id, key->condition_id); + if (cancelled != NULL) + *cancelled = true; + AnserSlot = NULL; + if (entry->payload != NULL) + pfree(entry->payload); + pfree(entry); + return true; + } + + /* + * Right channel, wrong kind of information. The coordinator stamps + * the type from the parts it folded, so this means a producer and a + * consumer disagree about what the channel carries -- claim the entry + * to stop waiting on it, and treat it as a cancellation so this + * consumer runs unfiltered. An empty delivery carries no type to + * check. + */ + if (!entry->cancelled && entry->payload != NULL && + entry->payload_type != payload_type) + { + elog(WARNING, "anser: condition %u delivered payload type '%c', expected '%c'", + key->condition_id, entry->payload_type, payload_type); + entry->cancelled = true; + } + + if (cancelled != NULL) + *cancelled = entry->cancelled; + if (!entry->cancelled && entry->payload != NULL) + { + if (payload != NULL) + { + *payload = palloc(entry->total_len); + memcpy(*payload, entry->payload, entry->total_len); + } + if (payload_len != NULL) + *payload_len = entry->total_len; + } + + AnserSlot = NULL; + ANSER_DEBUG("anser: seg%d claimed cond=%u (pushed for ssid=%d ccnt=%d, cancelled=%d)", + GpIdentity.segindex, key->condition_id, + entry->key.gp_session_id, entry->key.gp_command_count, + entry->cancelled ? 1 : 0); + if (entry->payload != NULL) + pfree(entry->payload); + pfree(entry); + return true; +} + +/* Drop a delivery nobody claimed, whatever state it is in. */ +void +AnserSidebandResetInbox(void) +{ + AnserWaiting = false; + anser_slot_free(); +} + +void +AnserSidebandResetAll(void) +{ + AnserSidebandResetInbox(); + AnserDispatchReset(); +} + +/* + * Build a QE -> QD payload: fixed-width header, then the key, then the base64 + * body. See ansersideband.h for the layout and AnserWireParse() for the + * reader. + */ +char * +AnserWireFormat(const AnserChannelKey *channel_key, char kind, + char payload_type, uint32 part_index, uint32 total_parts, + int flags, const void *payload, Size payload_len) +{ + StringInfoData buf; + char hdr[ANSER_WIRE_HDR_LEN + 1]; + int hdrlen; + int keylen = (int) strlen(channel_key->condition_key); + int bodylen = 0; + char *body = NULL; + pg_crc32c crc; + + /* + * What the checksum covers has to be exactly what goes on the wire, so the + * bytes it will cover are recorded here, as the body is encoded, rather + * than re-derived from the arguments afterwards. Deriving them again would + * mean covering a payload that was suppressed -- a cancelled message + * carries no body however it was called -- and the reader, seeing no body, + * would compute a different checksum and discard a perfectly good message. + */ + const void *crc_body = NULL; + Size crc_body_len = 0; + + if (!(flags & ANSER_WIRE_F_CANCELLED) && payload != NULL && payload_len > 0) + { + int maxlen = pg_b64_enc_len((int) payload_len); + + if (maxlen > ANSER_WIRE_MAX_BODYLEN) + { + /* + * No header can describe a body this large. gp_anser_max_info_size + * keeps us far away from this, so it is a belt-and-braces check on + * the field width rather than a reachable path. + */ + bodylen = 0; + flags |= ANSER_WIRE_F_CANCELLED; + } + else + { + body = palloc(maxlen + 1); + bodylen = pg_b64_encode((const char *) payload, (int) payload_len, + body, maxlen); + if (bodylen < 0) + { + pfree(body); + body = NULL; + bodylen = 0; + flags |= ANSER_WIRE_F_CANCELLED; + } + else + { + crc_body = payload; + crc_body_len = payload_len; + } + } + } + + Assert(keylen <= ANSER_WIRE_MAX_KEYLEN); + + /* + * Format the header with a zero CRC, checksum it together with the key and + * the raw body, then overwrite the CRC field in place -- it sits at a fixed + * offset, so one pass suffices and the checksum still covers every header + * field. Checksumming the body before base64 rather than after means a + * mangled encoding is caught too, and whether the body is covered at all is + * the payload type's call (anserpayload.h). + */ + hdrlen = snprintf(hdr, sizeof(hdr), ANSER_WIRE_HDR_FORMAT, + kind, payload_type, + (uint32) channel_key->gp_session_id, + (uint32) channel_key->gp_command_count, + channel_key->condition_id, + part_index, total_parts, + (uint32) flags, (uint32) keylen, (uint32) bodylen, 0U); + if (hdrlen != ANSER_WIRE_HDR_LEN) + { + /* + * Cannot happen -- every field is width-limited -- but complain rather + * than throw: this is reached from producer teardown, and the + * coordinator's length cross-check will reject the message anyway, + * which costs the filter and not the query. + */ + elog(WARNING, "anser: formatted a %d byte header, expected %d", + hdrlen, ANSER_WIRE_HDR_LEN); + } + + INIT_CRC32C(crc); + COMP_CRC32C(crc, hdr, ANSER_WIRE_CRC_OFFSET); + COMP_CRC32C(crc, channel_key->condition_key, keylen); + if (crc_body != NULL && AnserPayloadChecksumsBody(payload_type)) + COMP_CRC32C(crc, crc_body, crc_body_len); + FIN_CRC32C(crc); + snprintf(hdr + ANSER_WIRE_CRC_OFFSET, 9, "%08x", (uint32) crc); + + initStringInfo(&buf); + appendBinaryStringInfo(&buf, hdr, ANSER_WIRE_HDR_LEN); + appendBinaryStringInfo(&buf, channel_key->condition_key, keylen); + if (bodylen > 0) + appendBinaryStringInfo(&buf, body, bodylen); + + if (body != NULL) + pfree(body); + + return buf.data; +} + +/* + * Checksum of a QD -> QE push. Lives here, on the reading side, but is called + * from anserdispatch.c too: one definition means the writer and the reader + * cannot disagree about what is covered -- including whether the body is, which + * follows from the payload type. Integers go in network byte order so the + * value does not depend on the host. + */ +pg_crc32c +AnserWirePushCrc(char payload_type, uint32 condition_id, uint32 session_id, + uint32 command_count, uint32 flags, uint32 total_len, + uint32 offset, const char *key, int keylen, + const void *body, int bodylen) +{ + pg_crc32c crc; + uint32 fields[8]; + uint32 belen; + + fields[0] = pg_hton32((uint32) (unsigned char) payload_type); + fields[1] = pg_hton32(condition_id); + fields[2] = pg_hton32(session_id); + fields[3] = pg_hton32(command_count); + fields[4] = pg_hton32(flags); + fields[5] = pg_hton32(total_len); + fields[6] = pg_hton32(offset); + fields[7] = pg_hton32((uint32) keylen); + + INIT_CRC32C(crc); + COMP_CRC32C(crc, fields, sizeof(fields)); + if (keylen > 0) + COMP_CRC32C(crc, key, keylen); + belen = pg_hton32((uint32) bodylen); + COMP_CRC32C(crc, &belen, sizeof(belen)); + if (bodylen > 0 && AnserPayloadChecksumsBody(payload_type)) + COMP_CRC32C(crc, body, bodylen); + FIN_CRC32C(crc); + + return crc; +} + +/* + * Hand a payload to the coordinator. + * + * NotifyMyFrontEnd enforces no length limit of its own -- the ~8 KB cap + * applies to the SQL-level NOTIFY, which has to fit its queue page -- so a + * multi-megabyte part is fine here. The payload is base64, hence free of the + * NUL that would truncate it in pq_sendstring(). + */ +static bool +anser_sideband_send(const char *payload) +{ + if (whereToSendOutput != DestRemote) + { + /* + * Reached by any process that runs the plan without a frontend to + * answer to. Both directions of the protocol come through here, so + * this is the single place that says "this process is not on the + * sideband at all" -- for a publisher as much as for a subscriber. + */ + ANSER_DEBUG("anser: seg%d has no frontend to send on (role=%d writer=%d dest=%d port=%s sock=%d)", + GpIdentity.segindex, (int) Gp_role, Gp_is_writer ? 1 : 0, + (int) whereToSendOutput, + MyProcPort != NULL ? "ok" : "NULL", + MyProcPort != NULL ? (int) MyProcPort->sock : -1); + return false; + } + + NotifyMyFrontEnd(ANSER_NOTIFY_CHANNEL, payload, gp_session_id); + pq_flush(); + return true; +} diff --git a/gpcontrib/anser/test/.gitignore b/gpcontrib/anser/test/.gitignore new file mode 100644 index 00000000000..d65b9b5dd8d --- /dev/null +++ b/gpcontrib/anser/test/.gitignore @@ -0,0 +1,2 @@ +/anserfold_test +/anserdetach_test diff --git a/gpcontrib/anser/test/Makefile b/gpcontrib/anser/test/Makefile new file mode 100644 index 00000000000..15bc250fa60 --- /dev/null +++ b/gpcontrib/anser/test/Makefile @@ -0,0 +1,83 @@ +#------------------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +# Unit tests for anser: plain programs, no cluster. +# +# The backend's own unit tests live in src/backend/*/test and go through +# mock.mk, which links a mock of every backend file; an extension is not +# backend code and does not fit that. What is tested here does not need it +# either -- it works on caller-supplied memory -- so the few backend symbols +# anserfilter.c refers to from its other functions are stubbed in the test +# itself, where a wrong signature is a compile error. +# +# Built and run by the parent's installcheck; never installed. +# +# IDENTIFICATION +# gpcontrib/anser/test/Makefile +# +#------------------------------------------------------------------------- + +PROGRAM = anserfold_test +OBJS = anserfold_test.o anserfilter.o + +PGFILEDESC = "anserfold_test - unit test for the Anser bloom part fold" +NO_INSTALL = 1 + +PG_CPPFLAGS = -I$(srcdir)/../include +PG_LIBS_INTERNAL += $(libpgport) + +ifdef USE_PGXS +PG_CONFIG ?= pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) +else +subdir = gpcontrib/anser/test +top_builddir = ../../.. +include $(top_builddir)/src/Makefile.global +include $(top_srcdir)/contrib/contrib-global.mk +endif + +# Its own copy rather than the one the extension builds: that one is built for +# a shared library, and depending on it would tie this to the parent's build +# order for no gain. +anserfilter.o: $(srcdir)/../src/anserfilter.c + $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $< + +# Not called "check": pgxs.mk already defines that target, and overriding it +# here would only earn a warning from make. +.PHONY: unitcheck +unitcheck: $(PROGRAM) anserdetach_test + ./$(PROGRAM) + ./anserdetach_test + +# Only teardown code from the included transport source is linked. Any +# allocation, wait or error-reporting dependency in that path is unresolved, +# so the test also checks the abort-cleanup contract at link time. +ifeq ($(PORTNAME),darwin) +DETACH_TEST_LDFLAGS = -Wl,-dead_strip +else +DETACH_TEST_LDFLAGS = -Wl,--gc-sections +endif + +anserdetach_test: $(srcdir)/anserdetach_test.c $(srcdir)/../src/anserdispatch.c $(top_srcdir)/src/interfaces/libpq/libpq-events.c + $(CC) $(CFLAGS) $(CPPFLAGS) $(PG_CPPFLAGS) -ffunction-sections -fdata-sections \ + -I$(top_srcdir)/src/interfaces/libpq -I$(top_srcdir)/src/backend \ + $(DETACH_TEST_LDFLAGS) $< $(top_srcdir)/src/interfaces/libpq/libpq-events.c -o $@ + +EXTRA_CLEAN += anserdetach_test diff --git a/gpcontrib/anser/test/anserdetach_test.c b/gpcontrib/anser/test/anserdetach_test.c new file mode 100644 index 00000000000..03c2bd66a5b --- /dev/null +++ b/gpcontrib/anser/test/anserdetach_test.c @@ -0,0 +1,175 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserdetach_test.c + * Connection-event cleanup of Anser deliveries and queued chunks. + * + * Runs the production callback without a cluster. PostgreSQL allocation, + * wait and error-reporting functions are deliberately unavailable: adding a + * dependency on them to cleanup makes the test fail to link. + * + * IDENTIFICATION + * gpcontrib/anser/test/anserdetach_test.c + * + *------------------------------------------------------------------------- + */ +/* Test the production teardown with allocation, waiting and error-reporting + * entry points deliberately unavailable. Unused transport code is discarded + * by the linker; any such call reachable from teardown breaks this test. */ +#include "postgres.h" + +static bool fail_name; +static bool fail_events; +static int allocation_attempts; +static char *fixture_strdup(const char *text); +static void *fixture_realloc(void *memory, size_t bytes); +#define strdup fixture_strdup +#define realloc fixture_realloc +#include "../src/anserdispatch.c" +#undef strdup +#undef realloc + +static char *fixture_strdup(const char *text) +{ + ++allocation_attempts; + return fail_name ? NULL : strdup(text); +} +static void *fixture_realloc(void *memory, size_t bytes) +{ + ++allocation_attempts; + return fail_events ? NULL : realloc(memory, bytes); +} +#include + +#define CHECK(c) do { if (!(c)) abort(); } while (0) + +void pfree(void *p) { free(p); } +void list_free(List *list) +{ + if (list == NIL) return; + free(list->elements); + free(list); +} +void ExceptionalCondition(const char *a, const char *b, int c) { abort(); } + +static List * +fixture_list(void *a, void *b) +{ + List *list = calloc(1, sizeof(List)); + CHECK(list); + list->type = T_List; + list->length = list->max_length = b == NULL ? 1 : 2; + list->elements = calloc(list->length, sizeof(ListCell)); + CHECK(list->elements); + list->elements[0].ptr_value = a; + if (b) list->elements[1].ptr_value = b; + return list; +} + +/* Mirror libpq's documented callback-before-free ordering. */ +static void +finish_fixture_conn(PGconn *conn) +{ + int i; + + for (i = 0; i < conn->nEvents; ++i) + { + PGEventConnDestroy event = {conn}; + + CHECK(conn->events[i].proc(PGEVT_CONNDESTROY, &event, + conn->events[i].passThrough)); + free(conn->events[i].name); + } + free(conn->events); + free(conn); +} + +static int other_event(PGEventId event, void *info, void *pass_through) +{ + return 1; +} + +int main(void) +{ + PGconn *own = calloc(1, sizeof(*own)); + PGconn *other = calloc(1, sizeof(*other)); + AnserDispChannel channel = {0}; + char payload[] = "completed payload"; + AnserDispDelivery *a = calloc(1, sizeof(*a)); + AnserDispDelivery *b = calloc(1, sizeof(*b)); + CHECK(own && other && a && b); + CHECK(!anser_disp_track_conn(NULL)); + fail_name = true; + CHECK(!anser_disp_track_conn(own) && own->nEvents == 0); + fail_name = false; + fail_events = true; + CHECK(!anser_disp_track_conn(own) && own->nEvents == 0); + fail_events = false; + /* Existing libpq users must keep their event entries and instance data. */ + CHECK(PQregisterEventProc(own, other_event, "other user", NULL)); + CHECK(PQsetInstanceData(own, other_event, &channel)); + CHECK(anser_disp_track_conn(own) && anser_disp_track_conn(other)); + CHECK(anser_disp_track_conn(own) && own->nEvents == 2); + CHECK(PQinstanceData(own, other_event) == &channel); + CHECK(!PQregisterEventProc(own, anser_disp_conn_event, "duplicate", NULL)); + own->outCount = other->outCount = 12; + own->status = other->status = CONNECTION_OK; + AnserDispPendingPush = fixture_list(own, other); + a->channel = b->channel = &channel; + a->conn = own; b->conn = other; + channel.deliveries = fixture_list(a, b); + channel.complete = true; + channel.payload = payload; + dlist_push_head(&AnserDispDeliveries, &a->active_node); + dlist_push_head(&AnserDispDeliveries, &b->active_node); + + /* Destroy one connection: its pointers disappear before the memory does. + * Other dispatches and the merged payload remain available. */ + { + int attempts = allocation_attempts; + finish_fixture_conn(own); + CHECK(allocation_attempts == attempts); + } + CHECK(list_length(AnserDispPendingPush) == 1); + CHECK(linitial(AnserDispPendingPush) == other); + CHECK(list_length(channel.deliveries) == 1 && linitial(channel.deliveries) == b); + CHECK(dlist_head_element(AnserDispDelivery, active_node, &AnserDispDeliveries) == b); + CHECK(other->status == CONNECTION_OK); + + /* A reset starts another stream on the same PGconn. Forget old state, + * but keep registration so later subscriptions remain protected. */ + { + PGEventConnReset reset = {other}; + CHECK(anser_disp_conn_event(PGEVT_CONNRESET, &reset, NULL)); + CHECK(anser_disp_conn_event(PGEVT_CONNRESET, &reset, NULL)); + } + CHECK(AnserDispPendingPush == NIL && channel.deliveries == NIL); + CHECK(dlist_is_empty(&AnserDispDeliveries)); + CHECK(channel.complete && channel.payload == payload); + CHECK(anser_disp_track_conn(other) && other->nEvents == 1); + + /* Query-end cleanup must not dereference the connection already freed. */ + AnserDispatchFlushPending(10000); + AnserDispPendingPush = fixture_list(other, NULL); + AnserDispatchFlushPending(10000); + CHECK(other->status == CONNECTION_BAD && AnserDispPendingPush == NIL); + finish_fixture_conn(other); + CHECK(dlist_is_empty(&AnserDispDeliveries)); + return 0; +} diff --git a/gpcontrib/anser/test/anserfold_test.c b/gpcontrib/anser/test/anserfold_test.c new file mode 100644 index 00000000000..bf753105072 --- /dev/null +++ b/gpcontrib/anser/test/anserfold_test.c @@ -0,0 +1,258 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserfold_test.c + * What AnserBloomFoldPartInPlace() does to a part header. + * + * The coordinator folds every later part of a channel into the first one, and + * total_parts in that header is how many producers the channel is waiting for: + * every part carries the same number, and folding must not change it. It used + * to be incremented per fold, which left the merged header holding + * n_producers + (folds) -- 2N-1 for a complete channel, and N only when N is 1. + * That number is what a consumer reports and what the log prints when a filter + * goes missing, so it was wrong exactly where someone would be reading it. + * + * A regression test rather than a demonstration: the SQL-level fold test could + * not see this, because it folds one part into one and 2N-1 is N there. This + * one folds three, where the two differ. + * + * Runs without a cluster -- the fold is a bitwise OR over caller-supplied + * memory, so the parts here are built by hand rather than by bloom_create(). + * The backend symbols anserfilter.c refers to from its other functions are + * stubbed below; none of them is on this path, and each aborts if that ever + * stops being true. + * + * IDENTIFICATION + * gpcontrib/anser/test/anserfold_test.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include +#include + +#include "anserfilter.h" +#include "common/hashfn.h" +#include "lib/bloomfilter.h" +#include "port/pg_bitutils.h" + +#define BITSET_BYTES 64 +#define PART_BYTES (sizeof(AnserBloomPartHeader) + BITSET_BYTES) + +#define PRODUCERS 3 /* so that N and 2N-1 differ */ + +static int failures = 0; + +static void +check(bool ok, const char *what) +{ + if (!ok) + { + fprintf(stderr, "FAIL: %s\n", what); + failures++; + } +} + +/* + * One part as it travels: the header every producer writes, then a bitset with + * a single byte set, so a union of several is easy to recognise. + */ +static void +make_part(unsigned char *buf, uint32 part_index, uint32 total_parts, + int set_byte) +{ + AnserBloomPartHeader *h = (AnserBloomPartHeader *) buf; + + memset(buf, 0, PART_BYTES); + h->magic = ANSER_BLOOM_PART_MAGIC; + h->version = ANSER_BLOOM_PART_VERSION; + h->part_index = part_index; + h->total_parts = total_parts; + buf[sizeof(AnserBloomPartHeader) + set_byte] = 0x01; +} + +static bool +header_is(const unsigned char *buf, uint32 part_index, uint32 total_parts) +{ + const AnserBloomPartHeader *h = (const AnserBloomPartHeader *) buf; + + return h->magic == ANSER_BLOOM_PART_MAGIC && + h->version == ANSER_BLOOM_PART_VERSION && + h->part_index == part_index && + h->total_parts == total_parts; +} + +static bool +bit_set(const unsigned char *buf, int byte) +{ + return buf[sizeof(AnserBloomPartHeader) + byte] == 0x01; +} + +int +main(void) +{ + unsigned char acc[PART_BYTES]; + unsigned char second[PART_BYTES]; + unsigned char third[PART_BYTES]; + unsigned char before[PART_BYTES]; + unsigned char bigger[PART_BYTES * 2]; + unsigned char bad[PART_BYTES]; + + /* + * A three-producer channel: the first part is kept verbatim, the other two + * fold into it. This is the whole point of the file -- with one fold the + * old behaviour was indistinguishable from the right one. + */ + make_part(acc, 0, PRODUCERS, 0); + make_part(second, 1, PRODUCERS, 1); + make_part(third, 2, PRODUCERS, 2); + + check(AnserBloomFoldPartInPlace(acc, PART_BYTES, second, PART_BYTES), + "folding a same-sized part is accepted"); + check(header_is(acc, 0, PRODUCERS), + "one fold leaves the header alone"); + + check(AnserBloomFoldPartInPlace(acc, PART_BYTES, third, PART_BYTES), + "folding a third part is accepted"); + check(header_is(acc, 0, PRODUCERS), + "two folds leave the header alone (was 2N-1)"); + + check(bit_set(acc, 0) && bit_set(acc, 1) && bit_set(acc, 2), + "the union carries every part's bits"); + + /* The parts folded in are read-only. */ + check(header_is(second, 1, PRODUCERS) && header_is(third, 2, PRODUCERS), + "the folded-in parts are untouched"); + + /* + * A part of another size cannot be OR'd, and the refusal has to leave the + * accumulator exactly as it was -- the caller cancels the channel on it. + */ + memcpy(before, acc, PART_BYTES); + make_part(bigger, 0, PRODUCERS, 0); + check(!AnserBloomFoldPartInPlace(acc, PART_BYTES, bigger, PART_BYTES * 2), + "a differently-sized part is refused"); + check(memcmp(acc, before, PART_BYTES) == 0, + "a refused size leaves the accumulator byte-identical"); + + /* The same for a part whose header does not pass validation. */ + make_part(bad, 0, PRODUCERS, 0); + ((AnserBloomPartHeader *) bad)->magic = 0xDEADBEEFU; + check(!AnserBloomFoldPartInPlace(acc, PART_BYTES, bad, PART_BYTES), + "a part with a bad magic is refused"); + check(memcmp(acc, before, PART_BYTES) == 0, + "a refused part leaves the accumulator byte-identical"); + + if (failures > 0) + { + fprintf(stderr, "%d check(s) failed\n", failures); + return 1; + } + printf("all checks passed\n"); + return 0; +} + +/* + * Stubs for what anserfilter.c's other functions refer to. The fold path + * touches none of them; reaching one means this test is no longer testing what + * it says it is, so they abort rather than return something plausible. + * + * Signatures are copied from the declarations the compiler has already seen, + * so a mismatch is a build error here rather than a surprise at link time. + */ +#define NOT_ON_THIS_PATH(fn) \ + do { \ + fprintf(stderr, "%s called: the fold path is not supposed to\n", fn); \ + abort(); \ + } while (0) + +bool gp_anser_debug = false; + +bloom_filter * +bloom_create(int64 total_elems, int bloom_work_mem, uint64 seed) +{ + NOT_ON_THIS_PATH("bloom_create"); +} + +bloom_filter * +bloom_create_from_bitset(int64 total_elems, int bloom_work_mem, uint64 seed, + const unsigned char *bitset, Size bitset_bytes) +{ + NOT_ON_THIS_PATH("bloom_create_from_bitset"); +} + +void +bloom_free(bloom_filter *filter) +{ + NOT_ON_THIS_PATH("bloom_free"); +} + +Size +bloom_bitset_bytes(const bloom_filter *filter) +{ + NOT_ON_THIS_PATH("bloom_bitset_bytes"); +} + +const unsigned char * +bloom_bitset_data(const bloom_filter *filter) +{ + NOT_ON_THIS_PATH("bloom_bitset_data"); +} + +uint64 +bloom_total_bits(bloom_filter *filter) +{ + NOT_ON_THIS_PATH("bloom_total_bits"); +} + +uint64 +hash_bytes_extended(const unsigned char *k, int keylen, uint64 seed) +{ + NOT_ON_THIS_PATH("hash_bytes_extended"); +} + +uint64 +pg_popcount(const char *buf, int bytes) +{ + NOT_ON_THIS_PATH("pg_popcount"); +} + +/* ANSER_DEBUG expands to elog(); gp_anser_debug is off, so it never fires. */ +bool +errstart(int elevel, const char *domain) +{ + return false; +} + +bool +errstart_cold(int elevel, const char *domain) +{ + return false; +} + +void +errfinish(const char *filename, int lineno, const char *funcname) +{ +} + +void +errmsg_internal(const char *fmt,...) +{ +} diff --git a/gpcontrib/robust_predicate_transfer/.gitignore b/gpcontrib/robust_predicate_transfer/.gitignore new file mode 100644 index 00000000000..9545f33b39b --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/.gitignore @@ -0,0 +1,7 @@ +/test/graph_test +/test/inject_test +/test/forward_walk_test +/test/*.dSYM + +# Local design notes and implementation plans. +/docs/ diff --git a/gpcontrib/robust_predicate_transfer/Makefile b/gpcontrib/robust_predicate_transfer/Makefile new file mode 100644 index 00000000000..c059b90b47f --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/Makefile @@ -0,0 +1,157 @@ +#------------------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +# Makefile for the robust_predicate_transfer extension. +# +# IDENTIFICATION +# gpcontrib/robust_predicate_transfer/Makefile +# +#------------------------------------------------------------------------- + +MODULE_big = robust_predicate_transfer +OBJS = \ + $(WIN32RES) \ + src/rptbackward.o \ + src/rptbackwardplan.o \ + src/rptgraph.o \ + src/rpteligible.o \ + src/rptforward.o \ + src/rptforwardplan.o \ + src/rptinit.o \ + src/rptinject.o \ + src/rptlocal.o \ + src/rptplan.o \ + src/rptregion.o + +PGFILEDESC = "robust_predicate_transfer - RPT+ transfer plans over Anser" + +EXTENSION = robust_predicate_transfer +DATA = robust_predicate_transfer--1.0.sql + +REGRESS = rpt_regions rpt_graph rpt_backward_eligibility rpt_key_types rpt_forward rpt_forward_faults rpt_backward rpt_backward_faults + +# Anser is a sibling module, not a linked library: it is preloaded and dfmgr.c +# loads modules RTLD_GLOBAL, so the AnserXxx entry points resolve at load time. +# That is what the PGDLLEXPORT markers on Anser's public headers are for. +PG_CPPFLAGS = -I$(srcdir)/include -I$(srcdir)/../anser/include + +# C++ module; LLVM bitcode is not generated for it (as gpcloud). +with_llvm = no + +ifdef USE_PGXS +PG_CONFIG ?= pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) +else +subdir = gpcontrib/robust_predicate_transfer +top_builddir = ../.. +include $(top_builddir)/src/Makefile.global +include $(top_srcdir)/contrib/contrib-global.mk +endif + +# Link with the C++ driver so the module owns its STL/exception runtime +# dependency even when the server was built without ORCA. +COMPILER = $(CXX) $(CXXFLAGS) + +# The analysis needs anser's library installed and preloaded: our module +# resolves AnserIsRuntimeFilterScan from it at load time (RTLD_NOW), and +# load order must be anser first. Mirrors gpcontrib/anser's preload +# machinery. +# Backward fault tests use anser_test, which Anser's normal install omits. +installcheck: install-anser-test preload-rpt + +.PHONY: install-anser-test +install-anser-test: + $(MAKE) -C ../anser install-anser-test + +.PHONY: preload-rpt +preload-rpt: + @if [ -z "$$COORDINATOR_DATA_DIRECTORY$$MASTER_DATA_DIRECTORY" ]; then \ + echo "ERROR: COORDINATOR_DATA_DIRECTORY (or MASTER_DATA_DIRECTORY) is not set;" >&2; \ + echo " source cloudberry-env.sh and gpdemo-env.sh before running installcheck." >&2; \ + exit 1; \ + fi + @current=`psql -d postgres -At -c "SHOW shared_preload_libraries;"` || { \ + echo "ERROR: cannot read shared_preload_libraries; is the cluster up?" >&2; \ + exit 1; \ + }; \ + current=`echo "$$current" | tr -d ' '`; \ + case ",$$current," in \ + *,anser,*robust_predicate_transfer,*) \ + echo "==> shared_preload_libraries already carries anser + robust_predicate_transfer" ;; \ + *) \ + without=`echo "$$current" | sed -e 's/anser//g' -e 's/robust_predicate_transfer//g' -e 's/,,*/,/g' -e 's/^,//' -e 's/,$$//'`; \ + if [ -z "$$without" ]; then new="anser,robust_predicate_transfer"; else new="$$without,anser,robust_predicate_transfer"; fi; \ + echo "==> Setting shared_preload_libraries to $$new"; \ + gpconfig -c shared_preload_libraries -v "'$$new'" --skipvalidation || exit 1; \ + gpstop -ra || exit 1 ;; \ + esac + +# CI runs a single make target per test config, so offer one target that +# installs the extension (and Anser, which we load-resolve against), arms the +# cluster, and runs the tests. +.PHONY: installcheck-with-install +installcheck-with-install: + $(MAKE) unitcheck surveycheck diagnosticcheck + $(MAKE) -C ../anser install + $(MAKE) install + $(MAKE) installcheck + +# Uses PostgreSQL headers but no server: synthetic plans exercise the exact +# production graph code and deterministic failure paths. +.PHONY: unitcheck +unitcheck: test/graph_test test/inject_test test/forward_walk_test + ./test/graph_test + ./test/inject_test + ./test/forward_walk_test + +test/graph_test: $(srcdir)/test/graph_test.cpp $(wildcard $(srcdir)/src/*.cpp) $(srcdir)/include/rpt.h $(srcdir)/include/rptqual.h + $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(PG_CPPFLAGS) $< -o $@ + +test/inject_test: $(srcdir)/test/inject_test.cpp $(wildcard $(srcdir)/src/*.cpp) $(srcdir)/include/rpt.h $(srcdir)/include/rptinject.h $(srcdir)/../anser/include/ansertlist.h + $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(PG_CPPFLAGS) -ffunction-sections -fdata-sections \ + $(WALK_TEST_LDFLAGS) $< -o $@ + +EXTRA_CLEAN += test/graph_test test/inject_test + +SURVEY_PYTHON ?= python3 +.PHONY: surveycheck +surveycheck: + PYTHONDONTWRITEBYTECODE=1 $(SURVEY_PYTHON) $(srcdir)/test/survey_test.py + +# Link the production backend walker; unused planner code is discarded so the +# fixture needs no server. Expression-list traversal is stubbed in the test. +ifeq ($(PORTNAME),darwin) +WALK_TEST_LDFLAGS = -Wl,-dead_strip +else +WALK_TEST_LDFLAGS = -Wl,--gc-sections +endif + +test/forward_walk_backend.o: $(top_srcdir)/src/backend/optimizer/util/walkers.c + $(CC) $(CFLAGS) $(CPPFLAGS) $(PG_CPPFLAGS) -ffunction-sections -fdata-sections -c $< -o $@ + +test/forward_walk_test: $(srcdir)/test/forward_walk_test.cpp $(srcdir)/src/rptforward.cpp $(srcdir)/include/rpt.h $(srcdir)/include/rptinject.h test/forward_walk_backend.o + $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(PG_CPPFLAGS) -ffunction-sections -fdata-sections \ + $(WALK_TEST_LDFLAGS) $< test/forward_walk_backend.o -o $@ + +EXTRA_CLEAN += test/forward_walk_test test/forward_walk_backend.o + +.PHONY: diagnosticcheck +diagnosticcheck: + PYTHONDONTWRITEBYTECODE=1 $(SURVEY_PYTHON) $(srcdir)/test/diagnose_rpt_test.py diff --git a/gpcontrib/robust_predicate_transfer/README.md b/gpcontrib/robust_predicate_transfer/README.md new file mode 100644 index 00000000000..87ef91371b3 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/README.md @@ -0,0 +1,669 @@ +# RPT+ for Apache Cloudberry + +RPT+ (Robust Predicate Transfer with Dynamic Execution) reduces the work of +multi-table joins by transferring runtime filters between related tables. +A selective predicate on one table can reduce another table before their +join executes, and that reduction can propagate through the join graph. +The goal is to make query performance less sensitive to join order and +inaccurate cardinality estimates. + +A Bloom filter summarizes the join keys that survive filtering. A receiving +scan can discard keys that are definitely absent, while the join still +checks all remaining rows. Bloom-filter false positives can add work but do +not remove matching rows. RPT+ chooses transfer schedules and controls their +overhead; building and exchanging filters is useful only when the saved +query work exceeds that cost. + +## Why it uses Anser + +Cloudberry executes queries across multiple segments. For a global filter, +keys from every producer must be combined before a receiving scan can safely +use it. The [Anser extension](../anser/README.md) supplies this runtime +information-sharing mechanism: producers publish filter parts, the +coordinator merges them, and consumers receive the completed filter over +existing dispatch connections. Its channel identity, delivery protocol and +timeout/cancellation handling let RPT+ reuse the transport rather than build +another distributed filter service. + +RPT+ decides **which tables exchange filters and in what order**; Anser +provides **global filter exchange and scan consumers**. RPT+ uses Anser's +transport independently of Anser's per-join injection policy. Enable RPT+ +with `anser.runtime_filter=off` so only one policy injects filters. + +The forward reducer runs as an InitPlan of the same statement, under its +snapshot, before the main query starts. Intermediate co-distributed transfers +stay inside the reducer process. Filters used by the main query cross the +dispatch boundary through Anser's statement-scoped channels. + +The backward pass builds one filter per equivalence class on the largest +root's main-plan scan, above its forward consumers. Eligible class members +consume that filter in their main-plan scans. No extra root scan is added. +A consumer is injected only when the existing hash build/probe and slice +ordering proves it can wait safely. Local edges rely on core runtime-filter +pushdown; bridge-led backward classes and dynamic abandonment are deferred. + +## References and attribution + +- **RPT+ paper:** Yiming Qiao, Peter Boncz and Huanchen Zhang, + [*Robust Predicate Transfer with Dynamic Execution*](https://doi.org/10.14778/3797919.3797934), + PVLDB 19(6):1278–1290, 2026. +- **RPT+ artifact:** + [embryo-labs/dynamic-predicate-transfer](https://github.com/embryo-labs/dynamic-predicate-transfer). + The transfer-graph code in this extension is adapted from this artifact. +- **Earlier RPT implementation:** + [robust-sql/robust](https://github.com/robust-sql/robust), the baseline + implementation extended by the RPT+ work. +- **Anser paper:** Liang Lin et al., + [*Anser: Adaptive Information Sharing Framework of AnalyticDB*](https://doi.org/10.14778/3611540.3611553), + PVLDB 16(12):3636–3648, 2023. + +The adapted artifact code is MIT-licensed, Copyright 2018–2025 Stichting +DuckDB Foundation. See [NOTICE](../../NOTICE), [LICENSE](../../LICENSE) and +[the artifact license](../../licenses/LICENSE-mit-duckdb-rpt.txt). + +## Building and enabling + +Configure Cloudberry with both extensions: + +```sh +./configure --with-anser --with-rpt +``` + +Install the extensions on every host and preload +`anser,robust_predicate_transfer` in that order. Set `anser.enable=on` in the +server configuration and restart the cluster. In a database, create the +`robust_predicate_transfer` extension and set `rpt.enable=on` and +`anser.runtime_filter=off` for queries using RPT+. + +`rpt_transfer_plan(text)` reports the transfer analysis without injecting +filters into the query it describes. `rpt.debug=on` logs planning decisions. + +## Planning workflow and call stack + +RPT transforms a finished distributed `PlannedStmt` on the coordinator. +PostgreSQL or ORCA chooses the joins, scans and Motions first. RPT then +adds filter nodes and, when forward transfer is selected, a reducer InitPlan. +It does not replace the optimizer or execute queries during planning. + +With `anser,robust_predicate_transfer` preloaded in that order, the planning +call sequence is: + +```text +pg_plan_query() + planner() + rpt_planner() [RPT planner hook] + anser_planner() [previous hook] + standard_planner() [PostgreSQL planner or ORCA] + AnserApplyRuntimeFilters() [inactive with runtime_filter=off] + rpt_log_analysis() [only with rpt.enable + rpt.debug] + rpt_try_injection() [eligible coordinator SELECT] + rpt_build_injection() [C++ exception boundary] + RptInject(stmt, materialize=true) + return final PlannedStmt +``` + +The hook injects only when `rpt.enable` is on and at least one of +`rpt.forward` or `rpt.backward` is on. It excludes SELECT INTO, COPY INTO and +refresh plans. `RptInject()` also requires `anser.enable=on`, +`anser.runtime_filter=off`, and a plan without existing Anser or RPT filter +nodes. See [rptinit.cpp](src/rptinit.cpp) and +[Anser's hooks](../anser/src/anserinit.c). + +`rpt_try_injection()` creates a memory context for construction. +`RptInject()` copies the statement and edits that copy. If construction +fails, the hook discards the context, reports a warning and returns the +original plan. Query cancellation propagates. `RptPgCall()` translates +PostgreSQL errors into C++ exceptions while STL owners unwind; the entry +boundary then returns the error to PostgreSQL's handler. + +### Inside the shared injector + +[`RptInject()`](src/rptforward.cpp) runs the following sequence once for +runtime injection. Forward and backward use the same largest-root forest +and the same scan pointers into the copied statement. + +```text +RptInject() + check Anser settings and existing filter nodes + copy PlannedStmt + RptCollectGraph() → analysis: RptTransferMgr + RptBuildClasses(analysis) + RptBuildForwardPlan(analysis) → runtime schedule: RptTransferMgr + RptCheckBackwardConsumers(stmt, schedule) → backward_consumers proofs + rpt_find_parents() → original parents and scan slices + initialize RptInjectionState → IDs and current scan-stack tops + collect process counts and row/selectivity estimates + + RptInjectForward() [when rpt.forward=on] + RptSelectForward() → RptForwardComponent[] + preflight scans and key output positions + rpt_assign_forward_channels() → IDs from shared state + build reducer scans and producer/consumer wrappers + rpt_apply_in_main() → forward consumers in main plan + return reducer children and selected components + + RptInjectBackward() [when rpt.backward=on] + AnserSliceProducers() → actual slice process counts + RptSelectBackward() → RptBackwardClass[] + preflight each accepted class against original parents/current stack tops + assign channel ID from shared state + AnserBuildBloomProducerScan() → wrap root's current top + AnserBuildBloomConsumerScan() → wrap each accepted member's top + RptRelinkStack() → update original parent and top map + + RptAttachReducerInitPlan() [only if reducer children exist] + return copied statement if anything was injected, otherwise original +``` + +`RptInjectForward()` is an internal function in +[rptforward.cpp](src/rptforward.cpp). Backward placement lives in +[rptbackward.cpp](src/rptbackward.cpp); its selector is in +[rptbackwardplan.cpp](src/rptbackwardplan.cpp). A dry run follows selection +and preflight without constructing executable nodes. + +### Graphs, schedules and placement state + +The analysis structures are declared in [rpt.h](include/rpt.h): + +| Structure | Produced by | Used for | +| --- | --- | --- | +| `RptVertexInfo` | `RptCollectGraph()` | One base scan, relation OID, row estimate, restriction/distribution properties and join keys; `scan` points into the working statement | +| `RptKey` | Graph collection | Physical column identity `{rtindex, attno}` | +| `RptEdgeInfo` | Graph collection and class building | Equality clauses between two vertices and transfer directions prohibited by join semantics | +| `RptKeyGroup` | `RptBuildClasses()` | An equivalence class, its members and selected leader/key | +| `RptGraphNode` / `RptGraphEdge` | `RptBuildForwardPlan()` | Per-vertex incoming/outgoing transfers for both passes and their local/global classification | +| `RptBackwardConsumer` | `RptCheckBackwardConsumers()` | Per-edge proof or refusal, original parents, output key positions, slice indexes, `defer_first` and estimated benefit | +| `RptForwardComponent` / `RptForwardChannel` | `RptSelectForward()` | One root's ordered reducer chain, channels, sizing, estimates and acceptance decision | +| `RptBackwardClass` / `RptBackwardTarget` | `RptSelectBackward()` | One root producer and its selected/refused members, sizing and exchange-cost decision | +| `RptTransferMgr` | Collection, class building and scheduling | Owns the temporary graph, classes, forest and eligibility decisions | + +`RptBuildClasses()` forms unprotected key equivalence classes and makes +implied equality edges explicit. It retains those logical edges in +`logical_neighbor_matrix`, then applies `SkipUnfilteredTable` to the +forward view, `neighbor_matrix`. Forward-only skipping therefore cannot +erase a useful backward edge from an unfiltered root. + +Despite its name, `RptBuildForwardPlan()` constructs the shared runtime +forest for **both** passes. It selects the largest scan by estimated +cardinality as each component's root. Frontier selection considers both +graph views; forward parents use the pruned forward edges, and backward +parents use the logical edges. Original join-direction protections remain +in force. `graph`, `root_rtindexes`, `bridge_rtindexes`, +`transfer_order_rtindexes` and `reducer_order_rtindexes` hold the result. +See [rptgraph.cpp](src/rptgraph.cpp) and [rptplan.cpp](src/rptplan.cpp). + +Executable endpoints must be plain SeqScans exposing the selected key in +their output. Join keys must pass the supported built-in, canonical by-value +type/equality checks used for raw-Datum Bloom filtering. + +The backward proof runs before wrapping any scan. It permits a blocking +consumer only on a supported hash probe path whose producer is in the +build subtree and in a different slice. It checks intervening blocking +nodes, outer-join paths, scans inside SubPlans and external producer +parameters, then +applies the per-class consumer cap. Local edges use core runtime filtering; +bridge-led classes are deferred. See [rpteligible.cpp](src/rpteligible.cpp). + +[`RptInjectionState`](include/rptinject.h) holds the condition/node counters +and two maps keyed by the original scan pointer: `parents` records the +original relink point, and `tops` records the outermost wrapper so far. +Forward consumers are added first. Backward producers and consumers wrap +these current tops, preserving forward filters below the backward producer. +Preflight checks that each original parent still holds that top exactly +once and that the key remains available in the scan's output. + +These identities have distinct meanings: + +| Name | Meaning | +| --- | --- | +| `rtindex` | Relation occurrence in `PlannedStmt.rtable`; self-join aliases have different indexes | +| `relid` | Underlying relation OID; aliases of one table share it | +| `RptKey.attno` | Physical column number in that relation | +| `producer_attno` / `consumer_attno` | Key position in the scan's projected output tuple | +| `class_key` | Representative `RptKey` identifying an equivalence class; distinct from its leader's key | +| `plan_node_id` | Executable plan-node identity | +| `slice_index` | Distributed execution slice | +| `condition_id` | Filter-channel identity, allocated across both runtime passes | + +`vertex_index_by_rtindex` maps a relation occurrence to its position in +`vertices`. Relation aliases are obtained from the statement's range table +when dumping, rather than stored in each vertex. The executor receives the +modified `PlannedStmt` and its CustomScan parameters; it does not receive +these C++ analysis containers. + +## Executable plan and execution workflow + +For a selected forward chain `A → B → C`, with C as the root, the conceptual +plan is: + +```text +Reducer InitPlan: + Gather Motion to coordinator + Sequence on segments + Producer(filter A) + copied scan A + safe restriction quals + Producer(filter B) + Consumer(filter A) + copied scan B + safe restriction quals + empty Result + +Main plan, under the original joins and Motions: + root subtree: + Producer(backward filter C) [one per accepted root-led class] + Consumer(filter B) + original scan C + + accepted class-member subtree: + Consumer(backward filter C) + original member scan +``` + +The reducer excludes the root. Its scans output the required key columns +and copy restriction quals only when they are proven safe for this extra, +eager execution; otherwise the whole qual list is dropped. An intermediate +consumer sits below its producer, so the outgoing filter summarizes rows +that survived incoming filters. + +`RptAttachReducerInitPlan()` in [rptinject.cpp](src/rptinject.cpp) adds the +Gather/Sequence/empty-Result tree to `PlannedStmt.subplans`, attaches a +`SubPlan` to the main tree's `initPlan`, adds a `PARAM_EXEC` slot and extends +`slices`, `subplan_sliceIds` and `numSlices`. It runs after both injection +passes. Backward-only injection adds no reducer or extra scan. If forward +is refused, backward can still build a filter from the plain root scan. + +### Core executor and RPT callbacks + +RPT installs a planner hook, not an executor hook. Its execution work is +performed by the injected CustomScan callbacks registered at preload. +The coordinator's core execution path is: + +```text +ExecutorStart() + standard_ExecutorStart() + InitPlan() [initialize plan states] + preprocess_initplans() + ExecSetParamPlan() [dispatch/run reducer InitPlan] + CdbDispatchPlan() [dispatch main plan] +ExecutorRun() + anser_executor_run() [Anser executor hook] + standard_ExecutorRun() + execute main plan +ExecutorFinish() + anser_executor_finish() + standard_ExecutorFinish() +ExecutorEnd() + anser_executor_end() [channel cleanup] + standard_ExecutorEnd() +``` + +The reducer's segment `ExecSequence()` drains its children in order before +returning the final empty Result. Its useful output is the filters, not +query rows. Forward filters are produced before the main plan starts; +Anser keeps them available across the InitPlan/main-plan dispatch boundary. + +During main execution, the root scan passes through its forward consumers +and then its backward producers. Each backward producer accumulates keys +from those surviving rows and publishes on exhaustion. A member consumer +uses the existing proof's `defer_first` flag: where the hash join can fetch +an outer tuple before building, it passes that first tuple unfiltered and +receives the filter on the next call. Backward adds no scan of either root +or member. The original joins still enforce the exact SQL conditions. + +### Local handoff and Anser exchange + +A policy-local hop consumed only inside the reducer uses +`RptBuildLocalProducerScan()` / `RptBuildLocalConsumerScan()` and the +[rptlocal.c](src/rptlocal.c) callbacks. The producer stores a Bloom filter +under `(EState, condition_id)` and marks it complete when drained. The +consumer uses only a complete filter; otherwise it passes rows through. + +Other hops use Anser's producer/consumer builders. Policy locality alone +does not permit in-process handoff across separate dispatches: forward +filters consumed by the main-plan root use Anser. Intermediate main-plan +scans do not consume forward filters; eligible members can receive the +root's backward filter. + +```text +QE ExecProcNode() + anser_produce_exec() → anser_produce_next() + ExecProcNode(child), then ExecAnserBloomFilterProduceAddDatum() + on exhaustion: ExecAnserBloomFilterProducePublish() + AnserSidebandPublish() + coordinator AnserDispatchNotifyHandler() + combine producer parts, then deliver the completed filter + +QE ExecProcNode() + anser_consume_exec() → anser_consume_next() + anser_consume_receive() → ExecAnserBloomFilterConsume() + push filter into the scan when supported, otherwise probe returned tuples +``` + +These are distributed call paths, not one process's stack. The transport +uses existing dispatch connections. Cancelled, unavailable or timed-out +exchanges leave consumers unfiltered; local backward edges are not given +blocking Anser consumers. See [Anser's scan callbacks](../anser/src/anserplanexec.c). + +Both Anser and local wrappers keep identity `INDEX_VAR` output lists for +execution. Their deparse lists are built through +[`AnserFilterScanTlist()`](../anser/include/ansertlist.h), which resolves a +child CustomScan's output expressions before copying them. This allows +`EXPLAIN`, including `VERBOSE`, to describe stacked filters without a +self-referencing deparse list. + +## Diagnostic workflow + +`rpt_transfer_plan(text)` plans one SELECT and returns its analysis without +executing the supplied query or injecting filters into its plan: + +```text +rpt_transfer_plan() + rpt_transfer_plan_impl() + pg_parse_query() + pg_analyze_and_rewrite_fixedparams() + pg_plan_query() [automatic RPT injection suppressed] + rpt_analyze() + RptCollectGraph() + RptBuildClasses() + RptBuildTransferPlan() [filtered-root survey schedule] + RptDumpTransferPlan() + RptInject(materialize=false) [shared largest-root runtime schedule] + return text +``` + +The survey schedule retains its filtered-root heuristic for comparison; +it is not the schedule used for runtime injection. `forward component`, +`runtime survey`, `backward class` and `backward consumer ... root=` lines +show current runtime decisions on the largest-root forest, including +refusal reasons. Dry runs honor the forward/backward switches and Anser +settings even when `rpt.enable` is off. + +With `rpt.debug=on`, the planner also calls `rpt_log_analysis()` before +actual injection. That log builds its own survey and dry-run analysis; +runtime injection does not reuse those objects. Debug mode therefore adds +planning work. + +## Runtime controls and cost model + +`rpt.enable` is the normal user-facing switch and defaults to `off`. +`rpt.forward` and `rpt.backward` are developer controls for testing, +measurement and diagnosis. Both default to `on`, remain session-settable +(`PGC_USERSET`), and are marked `GUC_NOT_IN_SAMPLE`. Normal use leaves both +enabled. With the master switch enabled, developers can isolate either pass: + +| Settings | Runtime work | +| --- | --- | +| `rpt.forward=on`, `rpt.backward=on` | Selected forward reducer, then selected in-plan backward filters | +| `rpt.forward=on`, `rpt.backward=off` | Forward reducer and main-plan consumers | +| `rpt.forward=off`, `rpt.backward=on` | In-plan backward filters; no reducer | +| Both off, or `rpt.enable=off` | No runtime injection | + +`rpt.forward_min_root_rows` (10000, per segment) gates only the forward +reducer, and `rpt.max_chain_depth` (4) bounds forward dependency hops. +Forward filters are always applied only to the root in the main plan. +Intermediate forward consumers exist only inside the reducer. + +The backward producer is sized from the root's estimated per-segment rows +multiplied by the accepted forward component's estimated selectivity, or by +1 when no forward component was accepted. These estimates affect selection and sizing, not +SQL correctness. + +Both passes use the exchange model: + +```text +collection_ms_per_MiB × producer_processes × filter_MiB + + delivery_ms_per_MiB × consumer_processes × filter_MiB +``` + +`rpt.forward_ms_per_mib_part` (3.5) controls collection, and +`rpt.delivery_ms_per_mib` (0.8) controls delivery. The defaults come from the +2026-11-10 measurement on one host with three primaries. The existing +`rpt.forward_max_exchange_ms` (100 ms) and +`rpt.forward_max_selectivity` (0.35) names are retained and apply to both +passes. The exchange limit is per forward chain or backward class; `-1` disables it. A selectivity threshold of 1 disables +the benefit gate. Unknown backward NDV is treated as a kept fraction of 1. +`rpt.max_backward_consumers` (4) caps each class after the proof, preferring +larger estimated rows removed. + +Backward process counts come from the actual producer and consumer slices, +including direct dispatch, singleton gangs and parallel workers. Reducer +scans remain nonparallel; a worker's partial filter must never be treated as +a complete segment-local filter. + +## Worked example: collect a four-table star + +The worked example will use real output from your development cluster. +The procedure below gathers the evidence for its graph, selection, +injection and final `EXPLAIN ANALYZE`; the annotated execution plan and +measured timings will be added from those artifacts. + +### 1. Build the current code and prepare the run + +Build and install both Anser and RPT on all hosts, and restart the cluster +so the preloaded libraries include the latest changes. Use an otherwise +idle cluster and a dedicated database, with a role allowed to create the +extension and tables and call `rpt_transfer_plan(text)`. + +Use your normal cluster connection environment. Run the collector from an +empty output directory and use a fresh database name. Set `rpt_repo` to +this checkout's absolute path on the development machine: + +```sh +rpt_repo=/path/to/cloudberry-leborchuk +rpt_output=/tmp/rpt-star-example +mkdir -p "$rpt_output" +createdb rpt_readme_example +cd "$rpt_output" +git -C "$rpt_repo" rev-parse HEAD > revision.txt +git -C "$rpt_repo" diff -- gpcontrib/anser gpcontrib/robust_predicate_transfer > source.diff +cp "$rpt_repo/gpcontrib/robust_predicate_transfer/scripts/collect_star_example.sql" collector.sql +set -o pipefail +psql -X -q -d rpt_readme_example \ + -v fact_rows=1000000 -v warmups=2 -v samples=7 \ + -f collector.sql \ + 2>&1 | tee collection.log +``` + +[`collect_star_example.sql`](scripts/collect_star_example.sql) creates the +`rpt_readme_star` schema and four randomly distributed tables. It stops +rather than overwrite an existing fixture schema. It keeps the fixture for +inspection after collection. The fact has one million rows by default; +customer, product and store have 1009, 503 and 211 rows. Fact keys cycle +through those three domains, and each dimension has a separate restriction +attribute cycling from 0 through 9. + +The script disables Anser's independent injection policy and debug logging. +It keeps the core runtime-filter settings and RPT cost/selectivity gates +at their current values and records them. The baseline therefore includes +whatever core filtering those settings enable. Each statement has a +120-second timeout. If the run stops, keep the partial directory and log. + +### 2. Inspect the query and input data + +The query is saved as `query.sql`: + +```sql +SELECT count(*) AS matched_rows, + coalesce(sum(f.amount_cents), 0) AS total_amount_cents +FROM rpt_readme_star.fact f +JOIN rpt_readme_star.customer c ON f.customer_key = c.k +JOIN rpt_readme_star.product p ON f.product_key = p.k +JOIN rpt_readme_star.store s ON f.store_key = s.k +WHERE c.region < 2 AND p.category < 2 AND s.zone < 2; +``` + +`cardinalities.csv` records the dimensions' full/selected row counts and +key NDVs. Their predicates select 202 customer keys, 102 product keys and +43 store keys. `fact-cardinalities.csv` records actual fact rows and key +NDVs, while `statistics.csv` contains the catalog estimates used to explain +planner decisions. `distribution.csv` records actual segment counts and +policies. `environment.txt` records the build, topology and effective +settings. + +### 3. Read the graph and forward selection + +For each optimizer, inspect `off-full.transfer.txt` or +`on-full.transfer.txt`. Here `off`/`on` names the requested **optimizer** +setting; `full` means both RPT passes are enabled. + +Resolve `vertex` numbers using aliases `f`, `c`, `p`, `s`. Range-table +indexes can differ between optimizers. The logical graph is: + +```text +customer c.k ── fact f.customer_key +product p.k ── fact f.product_key +store s.k ── fact f.store_key +``` + +These are three separate equivalence classes, each containing a dimension +key and its corresponding fact key. The filtered dimensions are forward +candidates; the fact is the expected runtime root because its scan has the +largest estimated cardinality. Read the root from `forward component`, +not from the filtered-root survey's earlier `leader=` lines. + +`RptBuildClasses()` preserves the logical graph and prepares its forward +view. `RptBuildForwardPlan()` builds the shared forest, and +`RptSelectForward()` evaluates the dimension-to-fact channels using scan +estimates, NDVs, filter sizing, selectivity and exchange cost. Record: + +- The runtime root, reducer order and three source/target key pairs. +- `est_sel`, `est_cost_ms`, filter bytes and the final `decision`. +- Each backward class/member decision and its refusal reason, if any. + +The classes do not equate customer, product and store keys to one another, +so this example should not be illustrated as a dimension-to-dimension +chain. The dump's `chain=[...]` lists the reducer scans; the selected +channels show the actual dependencies. + +Do not force acceptance for the first run. A gate refusal is useful data, +including on wider clusters where collection/delivery costs increase. +Send that dump before changing the budget or selectivity threshold. + +### 4. Match selection to the injected plan + +For accepted forward transfers, RPT creates one copied, key-only scan per +selected dimension in the reducer. Each copied scan retains its safe +restriction and gets a producer. The main-plan fact scan gets the three +corresponding consumers; the dimension scans in the main plan are not +wrapped in forward consumers. + +The injector proves backward eligibility before adding these wrappers. +It then adds any accepted root producers above the fact's forward +consumers and member consumers at proven relink points. Finally, +`RptAttachReducerInitPlan()` attaches the reducer's Gather/Sequence tree. + +In a common star plan the fact is a hash probe input and the dimensions +are build inputs. In that case the current in-plan backward proof can +refuse all three members. Record that outcome: this example should explain +the optimizer's real plan, including why a transfer is absent. + +Compare the captured plan files for the same optimizer: + +| Arm | Settings | Purpose | +| --- | --- | --- | +| `baseline` | `rpt.enable=off` | Original optimizer plan and answer | +| `forward` | RPT enabled, forward on, backward off | Reducer and root-consumer placement | +| `backward` | RPT enabled, forward off, backward on | In-plan backward decisions without a reducer | +| `full` | RPT enabled, both passes on | Combined selected plan | + +For example, `on-full.explain.txt` is the actual text +`EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY)` for the requested +ORCA run, and `on-full.explain.json` is its structured counterpart. Check +the reported optimizer to identify any fallback. These are separate +executions, so their timing and segment-level row counts can differ. + +### 5. Verify answers and measure the effect + +The collector compares count and integer sum for all eight optimizer/arm +combinations against the first baseline and stops on a mismatch. Each +answer is also saved in `*.result.csv`. + +After plan collection it runs two warmups and seven measured queries per +arm per optimizer. Arm order rotates between rounds. These are fresh, +unprepared query executions without EXPLAIN or debug logging; all arms run +in the same psql session. `collection.log` contains markers such as: + +```text +RPT_SAMPLE optimizer=on arm=full phase=sample run=1 +``` + +Use the `Time:` immediately following the two-column aggregate result as +the query sample. The log also contains timings for SET and marker +statements; exclude those and all `phase=warmup` queries. Report median +and minimum/maximum for each arm. These are warm-cache psql elapsed times, +including planning, execution and client round-trip; EXPLAIN's planning +and execution times are recorded separately, not used as substitutes. + +In the plans, inspect reducer producers, stacked fact consumers, actual +rows entering joins, filter-removal counters, buffers and any backward +producer/consumer nodes. A tuple-level filter can reduce join work while +the underlying scan still reads all heap pages; use the actual counters +rather than infer an I/O saving from fewer join rows. + +### 6. Return the evidence for the final README example + +Send the complete output directory, including `collector.sql`, `revision.txt`, +`source.diff`, `collection.log`, metadata CSVs, transfer dumps, result CSVs and both plan +formats. Also note CPU/RAM, storage type, number of physical hosts, whether +other workloads ran, and whether the data fit in cache. + +We can then annotate the real vertex/class IDs, candidate decisions, +producer/consumer placement, final `EXPLAIN ANALYZE`, row reduction and +measured overhead or benefit. After inspection, remove the dedicated +database when its fixture is no longer needed. + +## Build, test and diagnostic workflow + +Run native checks without a cluster: + +```sh +make -C gpcontrib/robust_predicate_transfer unitcheck surveycheck diagnosticcheck +``` + +After changing either preloaded library, build and install both extensions +on every host, then restart the cluster so backends load the new code: + +```sh +make -C gpcontrib/anser install +make -C gpcontrib/robust_predicate_transfer install +``` + +With the updated libraries preloaded on a running cluster: + +```sh +make -C gpcontrib/anser installcheck +make -C gpcontrib/robust_predicate_transfer installcheck +``` + +RPT's `installcheck` installs Anser's `anser_test` extension before running +regressions. The fault tests also require `gp_inject_fault`. + +`rpt_backward` checks runtime shape, row reduction, gates and result equality +under both optimizers. `rpt_backward_faults` requires `gp_inject_fault` and +checks held delivery, consumer walk-away, oversized-producer cancellation, +query cancellation and a succeeding next command. Cancellation cases prepare +and verify an injected plan before starting the short timeout. Assertions use dynamic +SQL so changes to RPT settings cannot silently reuse an earlier plan. + +For a step-by-step run in an isolated test database: + +```sh +python3 gpcontrib/robust_predicate_transfer/scripts/diagnose_rpt.py \ + --suite backward --database rpt_test --output /tmp/rpt-backward +``` + +The runner saves SQL, client output, per-query decisions in `decisions.json`, +and watchdog/cancellation evidence. `--suite forward` and +`--suite backward_faults` run the other workloads. The legacy +`diagnose_forward.py` entry point remains supported. + +For benchmark planning surveys, use [scripts/survey.py](scripts/survey.py). +It plans queries without executing them and reports both the filtered-root +survey and largest-root runtime decisions. Run as a superuser or a role +granted EXECUTE on `rpt_transfer_plan(text)`. TPC-DS 14/23/24/39 contain two +SELECTs each: split them into a/b variants or retain two SELECT statements +per file. Use the CTE form of TPC-H Q15. Expected query counts are statements +(103 for TPC-DS, 22 for TPC-H), not SQL file counts. + +A reducer-side backward fallback, bridge-chained backward filters, dynamic +abandonment and sharing delivered filters between consumers in one process +remain deferred. The reducer fallback would scan the largest table again; +it is not enabled or selected automatically. diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_backward.out b/gpcontrib/robust_predicate_transfer/expected/rpt_backward.out new file mode 100644 index 00000000000..64fb1383841 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_backward.out @@ -0,0 +1,282 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Backward producers run in the main plan above forward consumers. Every +-- result query is dynamic SQL, so GUC changes cannot reuse an earlier plan. +CREATE EXTENSION robust_predicate_transfer; +SET anser.runtime_filter = off; +SET anser.timeout_ms = 600000; +-- This timeout covers the entire assertion DO block, under both optimizers. +SET statement_timeout = '120s'; +SET work_mem = '1MB'; +SET enable_nestloop = off; +SET enable_mergejoin = off; +SET gp_enable_hashjoin_size_heuristic = on; +SET join_collapse_limit = 1; +SET optimizer_join_order = query; +CREATE TABLE rpt_bw_root (k int, s int) DISTRIBUTED BY (k); +CREATE TABLE rpt_bw_member (k int, pad text) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_bw_dim (s int, v int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_bw_bridge (k int, s int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_bw_empty (k int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_bw_local (k int) DISTRIBUTED BY (k); +CREATE TABLE rpt_bw_replica (k int) DISTRIBUTED REPLICATED; +CREATE TABLE rpt_bw_part (k int, pad text) DISTRIBUTED RANDOMLY + PARTITION BY RANGE (k) (START (0) END (2000) EVERY (1000)); +-- The root has more rows but a smaller hash footprint than the wide member. +-- Duplicate root keys also make its backward filter selective without a +-- root restriction that would change the largest-root choice. +INSERT INTO rpt_bw_root SELECT CASE WHEN i % 101 = 0 THEN NULL ELSE i % 10 END, + i % 100 FROM generate_series(1, 10000) i; +-- Incompressible padding keeps the member's hash footprint genuinely wide. +INSERT INTO rpt_bw_member SELECT i, + (SELECT string_agg(md5(i::text || ':' || j::text), '') FROM generate_series(1, 100) j) + FROM generate_series(0, 999) i; +INSERT INTO rpt_bw_dim SELECT i, i FROM generate_series(0, 99) i; +INSERT INTO rpt_bw_bridge SELECT i, i % 100 FROM generate_series(0, 999) i; +INSERT INTO rpt_bw_local SELECT i FROM generate_series(0, 999) i; +INSERT INTO rpt_bw_replica SELECT i FROM generate_series(0, 999) i; +INSERT INTO rpt_bw_part SELECT * FROM rpt_bw_member; +ANALYZE rpt_bw_root; +ANALYZE rpt_bw_member; +ANALYZE rpt_bw_dim; +ANALYZE rpt_bw_bridge; +ANALYZE rpt_bw_empty; +ANALYZE rpt_bw_local; +ANALYZE rpt_bw_replica; +ANALYZE rpt_bw_part; +CREATE FUNCTION pg_temp.rpt_bw_result(q text, enabled boolean, backward boolean) +RETURNS text LANGUAGE plpgsql AS $$ +DECLARE + r text; +BEGIN + PERFORM set_config('rpt.enable', CASE WHEN enabled THEN 'on' ELSE 'off' END, true); + PERFORM set_config('rpt.backward', CASE WHEN backward THEN 'on' ELSE 'off' END, true); + q := format('SELECT coalesce(string_agg(t::text, %L ORDER BY t::text), %L) FROM (%s) t', + ';', '', q); + EXECUTE q INTO r; + PERFORM set_config('rpt.enable', 'on', true); + PERFORM set_config('rpt.backward', 'on', true); + RETURN r; +END +$$; +CREATE FUNCTION pg_temp.rpt_bw_plan(q text) RETURNS jsonb LANGUAGE plpgsql AS $$ +DECLARE + plan jsonb; +BEGIN + EXECUTE 'EXPLAIN (ANALYZE, VERBOSE, FORMAT JSON) ' || q INTO plan; + RETURN plan; +END +$$; +CREATE FUNCTION pg_temp.rpt_bw_nodes(plan jsonb) +RETURNS SETOF jsonb LANGUAGE sql AS $$ + WITH RECURSIVE walk(node, in_init) AS ( + SELECT plan->0->'Plan', false + UNION ALL + SELECT child, in_init OR coalesce(child->>'Parent Relationship', '') = 'InitPlan' + FROM walk CROSS JOIN LATERAL jsonb_array_elements(coalesce(node->'Plans', '[]')) child + ) SELECT node FROM walk WHERE NOT in_init +$$; +CREATE FUNCTION pg_temp.rpt_bw_expect(q text, relation_name text, reason text) +RETURNS void LANGUAGE plpgsql AS $$ +DECLARE + dump text := rpt_transfer_plan(q); + member_rtindex text; +BEGIN + member_rtindex := substring(dump FROM ('vertex ([0-9]+) ' || relation_name || ' rows=')); + IF member_rtindex IS NULL OR NOT EXISTS ( + SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('backward consumer ' || member_rtindex || ' root=%') + AND line LIKE ('% decision=' || reason)) THEN + RAISE EXCEPTION 'missing backward decision % for %, optimizer=%: %', + reason, relation_name, current_setting('optimizer'), dump; + END IF; +END +$$; +DO $$ +DECLARE + opt text; + q text; + dump text; + plan jsonb; + baseline text; + answer text; + cursor_name refcursor; + first_row record; + plain constant text := 'SELECT r.k, m.pad FROM rpt_bw_member m JOIN rpt_bw_root r ON m.k = r.k'; + chained constant text := 'SELECT r.k, m.pad FROM rpt_bw_member m JOIN + (rpt_bw_root r JOIN rpt_bw_dim d ON r.s = d.s) ON m.k = r.k WHERE d.v < 5'; +BEGIN + PERFORM set_config('rpt.enable', 'on', true); + PERFORM set_config('rpt.forward_min_root_rows', '0', true); + PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + PERFORM set_config('rpt.forward', 'off', true); + PERFORM pg_temp.rpt_bw_expect(plain, 'm', 'inject'); + -- Forward skipping must not suppress an unfiltered root's broadcast. + -- Redundant safe predicates must not be required to recover its edges. + q := plain || ' WHERE m.k >= 0 AND r.s >= 0'; + PERFORM pg_temp.rpt_bw_expect(q, 'm', 'inject'); + IF pg_temp.rpt_bw_result(plain, true, true) IS DISTINCT FROM + pg_temp.rpt_bw_result(q, true, true) THEN + RAISE EXCEPTION 'redundant qualifiers changed backward results'; + END IF; + plan := pg_temp.rpt_bw_plan(plain); + IF NOT EXISTS (SELECT FROM pg_temp.rpt_bw_nodes(plan) n + WHERE n->>'Custom Plan Provider' = 'Anser Bloom Producer') OR + NOT EXISTS (SELECT FROM pg_temp.rpt_bw_nodes(plan) n + WHERE n->>'Custom Plan Provider' = 'Anser Bloom Consumer') OR + plan::text LIKE '%InitPlan%' THEN + RAISE EXCEPTION 'backward-only shape wrong (optimizer=%): %', opt, plan; + END IF; + IF (SELECT coalesce(sum(coalesce((n->>'Rows Removed by Bloom Filter')::float8, 0) + + coalesce((n->>'Rows Removed by Pushdown Runtime Filter')::float8, 0)), 0) + FROM pg_temp.rpt_bw_nodes(plan) n) <= 0 THEN + RAISE EXCEPTION 'backward consumer removed no rows (optimizer=%): %', opt, plan; + END IF; + -- The root producer must directly wrap the forward consumer stack. + PERFORM set_config('rpt.forward', 'on', true); + PERFORM pg_temp.rpt_bw_expect(chained, 'm', 'inject'); + plan := pg_temp.rpt_bw_plan(chained); + IF plan::text NOT LIKE '%InitPlan%' OR NOT EXISTS ( + SELECT FROM pg_temp.rpt_bw_nodes(plan) n + WHERE n->>'Custom Plan Provider' = 'Anser Bloom Producer' + AND n->'Plans'->0->>'Custom Plan Provider' = 'Anser Bloom Consumer') THEN + RAISE EXCEPTION 'backward producer is not above forward consumer (optimizer=%): %', opt, plan; + END IF; + -- Refusing forward must not suppress a useful backward filter. + PERFORM set_config('rpt.forward_min_root_rows', '1e12', true); + PERFORM pg_temp.rpt_bw_expect(chained, 'm', 'inject'); + IF rpt_transfer_plan(chained) NOT LIKE '%decision=root rows below rpt.forward_min_root_rows%' THEN + RAISE EXCEPTION 'forward gate fixture did not actually refuse'; + END IF; + PERFORM set_config('rpt.forward_min_root_rows', '0', true); + -- Runtime gates, with the same fixture known to inject above. + PERFORM set_config('rpt.forward_max_selectivity', '0', true); + PERFORM pg_temp.rpt_bw_expect(plain, 'm', + 'estimated member selectivity not below rpt.forward_max_selectivity'); + PERFORM set_config('rpt.forward_max_selectivity', '0.35', true); + PERFORM set_config('rpt.forward_max_exchange_ms', '0', true); + PERFORM pg_temp.rpt_bw_expect(plain, 'm', + 'estimated exchange cost above rpt.forward_max_exchange_ms'); + PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); + PERFORM pg_temp.rpt_bw_expect( + 'SELECT * FROM rpt_bw_root r JOIN rpt_bw_local l ON r.k = l.k', + 'l', 'local edge uses core runtime filtering'); + -- Narrow projection makes the smaller member the hash build side. + PERFORM pg_temp.rpt_bw_expect( + 'SELECT count(*) FROM rpt_bw_root r JOIN rpt_bw_member m ON r.k = m.k', + 'm', 'producer is not on a qualifying hash build side'); + -- LEFT's preserved root cannot be filtered by its nullable member. + -- Its member has no unprotected equivalence class for broadcasting. + PERFORM pg_temp.rpt_bw_expect( + 'SELECT r.k, m.pad FROM rpt_bw_root r LEFT JOIN rpt_bw_member m ON r.k = m.k', + 'm', 'edge has no unprotected equivalence class'); + dump := rpt_transfer_plan('SELECT * FROM rpt_bw_root r JOIN rpt_bw_bridge b + ON r.k = b.k JOIN rpt_bw_dim d ON b.s = d.s WHERE d.v < 5'); + IF dump NOT LIKE '%backward class %decision=bridge-led class is deferred%' THEN + RAISE EXCEPTION 'bridge-led backward class was not deferred (optimizer=%): %', opt, dump; + END IF; + -- Both joins consume the narrow root subtree on their build side. + -- Cross-relation join filters keep wide member columns in each join's + -- input, while only r.k survives into the next hash build. + PERFORM set_config('rpt.max_backward_consumers', '1', true); + dump := rpt_transfer_plan('SELECT count(*) FROM + (rpt_bw_member m1 JOIN rpt_bw_root r ON m1.k = r.k AND length(m1.pad) > r.k) + JOIN rpt_bw_member m2 ON m2.k = r.k AND length(m2.pad) > r.k'); + IF (SELECT count(*) FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE 'backward consumer % root=% decision=inject') <> 1 OR + dump NOT LIKE '%root=%decision=backward consumer cap exceeded%' THEN + RAISE EXCEPTION 'runtime backward fan-out cap not exercised (optimizer=%): %', opt, dump; + END IF; + PERFORM set_config('rpt.max_backward_consumers', '4', true); + -- Every runtime proof refusal is retained verbatim in the new dump; + -- real-plan proof cases also run in rpt_backward_eligibility. + dump := rpt_transfer_plan('SELECT r.k, (SELECT count(*) FROM rpt_bw_member m + JOIN rpt_bw_dim d ON m.k = d.s WHERE m.k < r.k AND random() < 1 OFFSET 0) + FROM rpt_bw_root r'); + IF dump NOT LIKE '%decision=scan inside a SubPlan%' THEN + RAISE EXCEPTION 'correlated producer was not refused (optimizer=%): %', opt, dump; + END IF; + FOREACH q IN ARRAY ARRAY[ + plain, chained, + 'SELECT count(*), sum(r.k) FROM rpt_bw_root r LEFT JOIN rpt_bw_member m ON r.k = m.k', + 'SELECT count(*), sum(r.k) FROM rpt_bw_member m RIGHT JOIN rpt_bw_root r ON r.k = m.k', + 'SELECT count(*), sum(r.k) FROM rpt_bw_root r FULL JOIN rpt_bw_member m ON r.k = m.k', + 'SELECT count(*), sum(r.k) FROM rpt_bw_root r WHERE EXISTS (SELECT FROM rpt_bw_member m WHERE m.k = r.k)', + 'SELECT count(*), sum(r.k) FROM rpt_bw_root r WHERE NOT EXISTS (SELECT FROM rpt_bw_member m WHERE m.k = r.k)', + 'SELECT count(*) FROM rpt_bw_root r JOIN rpt_bw_empty e ON r.k = e.k', + 'SELECT count(*) FROM rpt_bw_empty e JOIN rpt_bw_member m ON e.k = m.k', + 'SELECT count(*) FROM rpt_bw_root r JOIN rpt_bw_replica p ON r.k = p.k', + 'SELECT count(*) FROM rpt_bw_replica p JOIN rpt_bw_member m ON p.k = m.k', + 'SELECT count(*) FROM rpt_bw_root r JOIN rpt_bw_part p ON r.k = p.k', + 'SELECT r.k, m.pad FROM rpt_bw_member m JOIN rpt_bw_root r ON m.k = r.k LIMIT 1', + 'SELECT r.k, m.pad FROM rpt_bw_member m JOIN rpt_bw_root r ON m.k = r.k LIMIT 0' + ] LOOP + -- LIMIT 1 has no SQL ordering guarantee; compare a count instead. + IF q LIKE '%LIMIT 1' THEN q := 'SELECT count(*) FROM (' || q || ') limited'; END IF; + baseline := pg_temp.rpt_bw_result(q, false, false); + IF pg_temp.rpt_bw_result(q, true, false) IS DISTINCT FROM baseline OR + pg_temp.rpt_bw_result(q, true, true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward result differs (optimizer=%): %', opt, q; + END IF; + END LOOP; + q := 'SELECT count(*)::text || ''/'' || sum(length(m.pad || r.k::text))::text + FROM rpt_bw_member m JOIN rpt_bw_root r ON m.k = r.k'; + PERFORM pg_temp.rpt_bw_expect(q, 'm', 'inject'); + baseline := pg_temp.rpt_bw_result(q, false, false); + EXECUTE 'PREPARE rpt_bw_prepared AS ' || q; + FOR i IN 1..2 LOOP + EXECUTE 'EXECUTE rpt_bw_prepared' INTO answer; + IF '(' || answer || ')' IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward prepared answer differs'; + END IF; + END LOOP; + EXECUTE 'DEALLOCATE rpt_bw_prepared'; + cursor_name := 'rpt_bw_cursor'; + OPEN cursor_name FOR EXECUTE plain; + FETCH cursor_name INTO first_row; + IF NOT FOUND THEN RAISE EXCEPTION 'backward cursor returned no row'; END IF; + CLOSE cursor_name; + -- A new command must answer after the cursor squelches its producer. + IF pg_temp.rpt_bw_result(q, true, true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'next command after backward cursor'; + END IF; + PERFORM set_config('anser.runtime_filter', 'on', true); + dump := rpt_transfer_plan(chained); + IF dump LIKE '%key=rpt_b_%' OR dump NOT LIKE '%needs anser.enable=on and anser.runtime_filter=off%' THEN + RAISE EXCEPTION 'backward did not stand down for Anser: %', dump; + END IF; + PERFORM set_config('anser.runtime_filter', 'off', true); + END LOOP; +END +$$; +DROP TABLE rpt_bw_root, rpt_bw_member, rpt_bw_dim, rpt_bw_bridge, rpt_bw_empty, + rpt_bw_local, rpt_bw_replica, rpt_bw_part; +DROP EXTENSION robust_predicate_transfer; +RESET anser.runtime_filter; +RESET anser.timeout_ms; +RESET statement_timeout; +RESET enable_nestloop; +RESET enable_mergejoin; +RESET gp_enable_hashjoin_size_heuristic; +RESET work_mem; +RESET join_collapse_limit; +RESET optimizer_join_order; diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_backward_eligibility.out b/gpcontrib/robust_predicate_transfer/expected/rpt_backward_eligibility.out new file mode 100644 index 00000000000..b904b96cdad --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_backward_eligibility.out @@ -0,0 +1,228 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Production walker/resolver coverage. Both optimizer settings run these +-- queries; every assertion resolves vertex IDs from relation names. +CREATE EXTENSION robust_predicate_transfer; +SET rpt.enable = off; +SET anser.runtime_filter = off; +SET enable_nestloop = off; +SET enable_mergejoin = off; +SET join_collapse_limit = 1; +SET optimizer_join_order = query; +SET statement_timeout = '30s'; +CREATE TABLE rpt_elig_root (pad int, k int, v int) DISTRIBUTED BY (k); +CREATE TABLE rpt_elig_member (k int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_elig_local (k int) DISTRIBUTED BY (k); +CREATE TABLE rpt_elig_replica (k int) DISTRIBUTED REPLICATED; +CREATE TABLE rpt_elig_outer (k int) DISTRIBUTED BY (k); +CREATE TABLE rpt_elig_bridge (k1 int, k2 int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_elig_member2 (k int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_elig_member3 (k int) DISTRIBUTED RANDOMLY; +INSERT INTO rpt_elig_root SELECT 0, i, i FROM generate_series(1, 10000) i; +INSERT INTO rpt_elig_member SELECT i FROM generate_series(1, 10000) i; +INSERT INTO rpt_elig_local SELECT i FROM generate_series(1, 10000) i; +INSERT INTO rpt_elig_replica SELECT i FROM generate_series(1, 10000) i; +INSERT INTO rpt_elig_outer SELECT i FROM generate_series(1, 10) i; +INSERT INTO rpt_elig_bridge SELECT i, i FROM generate_series(1, 1000) i; +INSERT INTO rpt_elig_member2 SELECT i FROM generate_series(1, 10000) i; +INSERT INTO rpt_elig_member3 SELECT i FROM generate_series(1, 10000) i; +ANALYZE rpt_elig_root; +ANALYZE rpt_elig_member; +ANALYZE rpt_elig_local; +ANALYZE rpt_elig_replica; +ANALYZE rpt_elig_outer; +ANALYZE rpt_elig_bridge; +ANALYZE rpt_elig_member2; +ANALYZE rpt_elig_member3; +CREATE FUNCTION pg_temp.rpt_expect(q text, producer text, consumer text, + reasons text[], accepted boolean DEFAULT NULL) +RETURNS text LANGUAGE plpgsql AS $$ +DECLARE + dump text := rpt_transfer_plan(q); + producer_rtindex text; + consumer_rtindex text; + decision text; +BEGIN + producer_rtindex := substring(dump FROM ('vertex ([0-9]+) ' || producer || ' rows=')); + consumer_rtindex := substring(dump FROM ('vertex ([0-9]+) ' || consumer || ' rows=')); + IF producer_rtindex IS NULL OR consumer_rtindex IS NULL THEN + RAISE EXCEPTION 'missing fixture scans % / %: %', producer, consumer, dump; + END IF; + SELECT line INTO decision FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('backward consumer ' || consumer_rtindex || ' producer ' || producer_rtindex || ' %') + AND substring(line FROM ' reason=(.*)$') = ANY(reasons) + AND (accepted IS NULL OR line LIKE ('% eligible=' || CASE WHEN accepted THEN 'yes' ELSE 'no' END || ' %')) + LIMIT 1; + IF decision IS NULL THEN + RAISE EXCEPTION 'expected % for % -> %, optimizer=%: %', reasons, producer, consumer, current_setting('optimizer'), dump; + END IF; + RETURN dump; +END +$$; +CREATE FUNCTION pg_temp.rpt_explain(q text) RETURNS jsonb LANGUAGE plpgsql AS $$ +DECLARE + plan jsonb; +BEGIN + EXECUTE 'EXPLAIN (FORMAT JSON) ' || q INTO plan; + RETURN plan; +END +$$; +CREATE FUNCTION pg_temp.rpt_scan_path(q text, relation_name text) RETURNS text[] LANGUAGE SQL AS $$ + WITH RECURSIVE walk(node, path) AS ( + SELECT pg_temp.rpt_explain(q)->0->'Plan', ARRAY[]::text[] + UNION ALL + SELECT child, path || ((node->>'Node Type') || ':' || + COALESCE(node->>'Join Type', '') || ':' || COALESCE(child->>'Parent Relationship', '')) + FROM walk CROSS JOIN LATERAL jsonb_array_elements(COALESCE(node->'Plans', '[]'::jsonb)) AS children(child) + ) + SELECT path FROM walk WHERE node->>'Relation Name' = relation_name LIMIT 1 +$$; +DO $$ +DECLARE + opt text; + q text; + dump text; + path text[]; + producer_path text[]; + split_at integer; + producer_rtindex text; + correlated text; +BEGIN + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + -- Expressions force the real walker through targetlists and quals. + -- k is physical column 2; the resolver must use its output position. + PERFORM pg_temp.rpt_expect( + 'SELECT rpt_elig_root.k + rpt_elig_member.k, rpt_elig_root.v FROM rpt_elig_root JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < 100 AND rpt_elig_root.v % 2 = 0', + 'rpt_elig_root', 'rpt_elig_member', ARRAY['eligible'], true); + PERFORM pg_temp.rpt_expect( + 'SELECT * FROM rpt_elig_root JOIN rpt_elig_local USING (k) WHERE rpt_elig_root.v < 100', + 'rpt_elig_root', 'rpt_elig_local', ARRAY['local edge uses core runtime filtering'], false); + PERFORM pg_temp.rpt_expect( + 'SELECT * FROM rpt_elig_root JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < 100 AND rpt_elig_member.k > 0', + 'rpt_elig_member', 'rpt_elig_root', ARRAY['producer is not on a qualifying hash build side'], false); + PERFORM pg_temp.rpt_expect( + 'SELECT * FROM rpt_elig_root JOIN rpt_elig_replica USING (k) WHERE rpt_elig_root.v < 100', + 'rpt_elig_root', 'rpt_elig_replica', ARRAY['same or unknown slice'], false); + PERFORM pg_temp.rpt_expect( + 'SELECT * FROM rpt_elig_root LEFT JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < 100', + 'rpt_elig_root', 'rpt_elig_member', ARRAY['edge has no unprotected equivalence class'], false); + PERFORM pg_temp.rpt_expect( + 'SELECT * FROM rpt_elig_root JOIN rpt_elig_bridge ON rpt_elig_root.k = rpt_elig_bridge.k1 JOIN rpt_elig_member ON rpt_elig_bridge.k2 = rpt_elig_member.k WHERE rpt_elig_root.v < 9000', + 'rpt_elig_bridge', 'rpt_elig_member', ARRAY['bridge-led class is deferred'], false); + -- ORDER BY can be discarded by ORCA. Assert the refusal only when + -- the Sort remains below the qualifying hash join; otherwise require + -- the unblocked query to qualify rather than silently skipping it. + q := 'SELECT * FROM (SELECT * FROM rpt_elig_member ORDER BY k) rpt_elig_member JOIN rpt_elig_root USING (k) WHERE rpt_elig_root.v < 100'; + path := pg_temp.rpt_scan_path(q, 'rpt_elig_member'); + IF path IS NULL THEN + RAISE EXCEPTION 'sort fixture lost its member scan'; + END IF; + IF EXISTS (SELECT FROM unnest(path) WITH ORDINALITY h(tag, n), unnest(path) WITH ORDINALITY s(tag, n) + WHERE h.tag LIKE 'Hash Join:%:Outer' AND s.tag LIKE 'Sort:%' AND h.n < s.n) THEN + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', + ARRAY['sort between consumer and join', 'unsupported consumer key path'], false); + ELSE + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['eligible'], true); + END IF; + -- A strict upper join may simplify the LEFT join to INNER. If it + -- survives, the nullable path must be refused by the production proof. + q := 'SELECT * FROM (rpt_elig_outer LEFT JOIN rpt_elig_member USING (k)) JOIN rpt_elig_root ON rpt_elig_root.k = rpt_elig_member.k WHERE rpt_elig_root.v < 100'; + path := pg_temp.rpt_scan_path(q, 'rpt_elig_member'); + IF path IS NULL THEN + RAISE EXCEPTION 'nullable-side fixture lost its member scan'; + END IF; + IF EXISTS (SELECT FROM unnest(path) tag WHERE tag LIKE '%:Left:Inner' OR tag LIKE '%:Right:Outer') THEN + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['nullable side of outer join'], false); + ELSE + -- Simplification does not fix hash build/probe order. Compare + -- the first differing path step: the shared parent is the join + -- separating these named scans, regardless of range-table IDs. + producer_path := pg_temp.rpt_scan_path(q, 'rpt_elig_root'); + IF producer_path IS NULL THEN + RAISE EXCEPTION 'nullable-side fixture lost its root scan'; + END IF; + SELECT min(n) INTO split_at FROM generate_subscripts(path, 1) n + WHERE path[n] IS DISTINCT FROM producer_path[n]; + IF split_at IS NULL THEN + RAISE EXCEPTION 'nullable-side fixture has no separating join'; + END IF; + IF producer_path[split_at] LIKE 'Hash Join:%:Outer' + AND path[split_at] LIKE 'Hash Join:%:Inner' THEN + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', + ARRAY['producer is not on a qualifying hash build side'], false); + ELSE + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['eligible'], true); + END IF; + END IF; + -- An InitPlan is evaluated once, and its presence alone does not + -- imply a nonempty extParam on the qualifying hash build. Depending + -- on planner placement it can be internal to that build or external. + -- The native test checks an explicit extParam bitmap; the correlated + -- SubPlan below exercises the rescan refusal on a real SQL plan. + q := 'SELECT * FROM rpt_elig_root JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < (SELECT max(k) FROM rpt_elig_outer)'; + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', + ARRAY['eligible', 'parameterized producer can be rescanned', + 'unsupported producer key path', 'producer is not on a qualifying hash build side']); + -- Volatility and OFFSET retain a correlated SubPlan instead of + -- allowing a decorrelating join rewrite. Its internal Motion must + -- not undo the explicit SubPlan refusal. + correlated := 'SELECT rpt_elig_outer.k, (SELECT count(*) FROM rpt_elig_root JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < rpt_elig_outer.k AND random() < 0.9 OFFSET 0) FROM rpt_elig_outer'; + IF pg_temp.rpt_explain(correlated)::text NOT LIKE '%SubPlan%' THEN + RAISE EXCEPTION 'correlated fixture no longer contains a SubPlan (optimizer=%)', opt; + END IF; + path := pg_temp.rpt_scan_path(correlated, 'rpt_elig_root'); + IF path IS NULL OR NOT EXISTS ( + SELECT FROM unnest(path) WITH ORDINALITY sp(tag, n), unnest(path) WITH ORDINALITY motion(tag, n) + WHERE sp.tag LIKE '%:SubPlan' AND motion.tag LIKE '%Motion:%' AND sp.n < motion.n) THEN + RAISE EXCEPTION 'correlated fixture lost the Motion inside its SubPlan (optimizer=%)', opt; + END IF; + dump := pg_temp.rpt_expect(correlated, 'rpt_elig_root', 'rpt_elig_member', ARRAY['scan inside a SubPlan'], false); + IF EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE 'backward consumer %' AND line LIKE '% eligible=yes %') THEN + RAISE EXCEPTION 'SubPlan produced an eligible consumer: %', dump; + END IF; + PERFORM set_config('rpt.max_backward_consumers', '1', true); + dump := rpt_transfer_plan('SELECT * FROM rpt_elig_root JOIN rpt_elig_member ON rpt_elig_root.k = rpt_elig_member.k JOIN rpt_elig_member2 ON rpt_elig_root.k = rpt_elig_member2.k JOIN rpt_elig_member3 ON rpt_elig_root.k = rpt_elig_member3.k WHERE rpt_elig_root.v < 100'); + producer_rtindex := substring(dump FROM 'vertex ([0-9]+) rpt_elig_root rows='); + IF producer_rtindex IS NULL OR + NOT EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('backward consumer % producer ' || producer_rtindex || ' %') + AND line LIKE '% reason=backward consumer cap exceeded') OR + (SELECT count(*) FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('backward consumer % producer ' || producer_rtindex || ' %') + AND line LIKE '% eligible=yes %') <> 1 THEN + RAISE EXCEPTION 'fan-out cap fixture did not retain exactly one consumer (optimizer=%): %', opt, dump; + END IF; + PERFORM set_config('rpt.max_backward_consumers', '4', true); + END LOOP; +END +$$; +DROP TABLE rpt_elig_root, rpt_elig_member, rpt_elig_local, rpt_elig_replica, + rpt_elig_outer, rpt_elig_bridge, rpt_elig_member2, rpt_elig_member3; +DROP EXTENSION robust_predicate_transfer; +RESET optimizer; +RESET rpt.enable; +RESET anser.runtime_filter; +RESET enable_nestloop; +RESET enable_mergejoin; +RESET join_collapse_limit; +RESET optimizer_join_order; +RESET statement_timeout; diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_backward_faults.out b/gpcontrib/robust_predicate_transfer/expected/rpt_backward_faults.out new file mode 100644 index 00000000000..8a07325c628 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_backward_faults.out @@ -0,0 +1,237 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Backward-only transport failures. Every armed fault must actually fire. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS gp_inject_fault; +CREATE EXTENSION IF NOT EXISTS anser_test; +-- end_ignore +CREATE EXTENSION robust_predicate_transfer; +SET anser.runtime_filter = off; +SET rpt.enable = on; +SET rpt.forward = off; +SET rpt.backward = on; +SET rpt.forward_max_exchange_ms = -1; +SET statement_timeout = '120s'; +SET work_mem = '1MB'; +SET enable_nestloop = off; +SET enable_mergejoin = off; +SET gp_enable_hashjoin_size_heuristic = on; +CREATE TABLE rpt_bwf_root (k int) DISTRIBUTED BY (k); +CREATE TABLE rpt_bwf_member (k int, pad text) DISTRIBUTED RANDOMLY; +INSERT INTO rpt_bwf_root SELECT i % 10 FROM generate_series(1, 10000) i; +INSERT INTO rpt_bwf_member SELECT i, + (SELECT string_agg(md5(i::text || ':' || j::text), '') FROM generate_series(1, 100) j) + FROM generate_series(0, 999) i; +ANALYZE rpt_bwf_root; +ANALYZE rpt_bwf_member; +CREATE FUNCTION pg_temp.rpt_bwf_query() RETURNS text LANGUAGE sql AS $$ + SELECT 'SELECT count(*)::text || ''/'' || sum(length(m.pad || r.k::text))::text + FROM rpt_bwf_member m JOIN rpt_bwf_root r ON m.k = r.k' +$$; +CREATE FUNCTION pg_temp.rpt_bwf_run(enabled boolean) RETURNS text LANGUAGE plpgsql AS $$ +DECLARE + q text := pg_temp.rpt_bwf_query(); + r text; +BEGIN + PERFORM set_config('rpt.enable', CASE WHEN enabled THEN 'on' ELSE 'off' END, true); + EXECUTE q INTO r; + PERFORM set_config('rpt.enable', 'on', true); + RETURN r; +END +$$; +-- How often a fault fired, so a case cannot pass without exercising it. +CREATE FUNCTION pg_temp.rpt_bwf_hits(fault text, coordinator boolean) +RETURNS int LANGUAGE sql AS $$ + SELECT coalesce(sum(substring(gp_inject_fault(fault, 'status', dbid) + FROM 'num times hit:''([0-9]+)''')::int), 0)::int + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator +$$; +-- First occurrence only; on the coordinator a 'sleep' fault sleeps 1 s. +CREATE FUNCTION pg_temp.rpt_bwf_fault(fault text, action text, coordinator boolean) +RETURNS void LANGUAGE plpgsql AS $$ +BEGIN + IF action = 'reset' THEN + PERFORM gp_inject_fault(fault, 'reset', dbid) + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator; + ELSE + PERFORM gp_inject_fault(fault, action, '', '', '', 1, 1, + CASE WHEN coordinator THEN 1 ELSE 0 END, dbid) + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator; + END IF; +END +$$; +DO $$ +DECLARE + opt text; + baseline text; + answer text; + plan jsonb; + deliveries_before bigint; +BEGIN + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + baseline := pg_temp.rpt_bwf_run(false); + IF rpt_transfer_plan(pg_temp.rpt_bwf_query()) NOT LIKE '%backward class %decision=inject%' THEN + RAISE EXCEPTION 'backward fault fixture not selected (optimizer=%)', opt; + END IF; + PERFORM set_config('anser.timeout_ms', '100', true); + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'sleep', true); + answer := pg_temp.rpt_bwf_run(true); + IF pg_temp.rpt_bwf_hits('anser_before_deliver', true) = 0 THEN + RAISE EXCEPTION 'backward delivery sleep never fired'; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'reset', true); + IF answer IS DISTINCT FROM baseline OR pg_temp.rpt_bwf_run(true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward timeout or next command changed results'; + END IF; + PERFORM set_config('anser.timeout_ms', '1000', true); + PERFORM set_config('anser.debug_delivery_chunk_bytes', '8192', true); + PERFORM set_config('anser.debug_min_filter_bytes', '8388608', true); + PERFORM pg_temp.rpt_bwf_fault('anser_after_chunk', 'skip', false); + answer := pg_temp.rpt_bwf_run(true); + IF pg_temp.rpt_bwf_hits('anser_after_chunk', false) = 0 THEN + RAISE EXCEPTION 'backward consumer walk-away never fired'; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_after_chunk', 'reset', false); + IF answer IS DISTINCT FROM baseline OR pg_temp.rpt_bwf_run(true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward walk-away or next command changed results'; + END IF; + -- Plan with an 8 MiB filter, then lower the runtime payload cap. + -- EXPLAIN EXECUTE forces the generic plan before the cap changes; + -- the second EXPLAIN proves the cached producer was not resized away. + EXECUTE 'PREPARE rpt_bwf_oversized AS ' || pg_temp.rpt_bwf_query(); + EXECUTE 'EXPLAIN (FORMAT JSON) EXECUTE rpt_bwf_oversized' INTO plan; + IF plan::text NOT LIKE '%8388608%' THEN + RAISE EXCEPTION 'oversized producer fixture was not planned: %', plan; + END IF; + PERFORM set_config('anser.max_info_size', '2097152', true); + EXECUTE 'EXPLAIN (FORMAT JSON) EXECUTE rpt_bwf_oversized' INTO plan; + IF plan::text NOT LIKE '%8388608%' THEN + RAISE EXCEPTION 'oversized producer fixture was replanned: %', plan; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'skip', true); + deliveries_before := anser_test_deliveries_completed(); + EXECUTE 'EXECUTE rpt_bwf_oversized' INTO answer; + -- The fault proves delivery was attempted; only the counter can + -- distinguish a cancellation from a successfully delivered filter. + IF anser_test_deliveries_completed() <> deliveries_before THEN + RAISE EXCEPTION 'oversized producer delivered a filter instead of cancelling'; + END IF; + IF pg_temp.rpt_bwf_hits('anser_before_deliver', true) = 0 THEN + RAISE EXCEPTION 'oversized producer never delivered its cancellation'; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'reset', true); + EXECUTE 'DEALLOCATE rpt_bwf_oversized'; + PERFORM set_config('anser.max_info_size', '68157440', true); + PERFORM set_config('anser.debug_min_filter_bytes', '0', true); + PERFORM set_config('anser.debug_delivery_chunk_bytes', '0', true); + IF answer IS DISTINCT FROM baseline OR pg_temp.rpt_bwf_run(true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward producer cancellation changed results'; + END IF; + END LOOP; +END +$$; +CREATE FUNCTION pg_temp.rpt_bwf_cancel() RETURNS void LANGUAGE plpgsql AS $$ +DECLARE + cancelled boolean := false; + answer text; +BEGIN + BEGIN + EXECUTE 'EXECUTE rpt_bwf_cancel_plan' INTO answer; + EXCEPTION WHEN query_canceled THEN + cancelled := true; + END; + IF NOT cancelled THEN + RAISE EXCEPTION 'backward wait was not cancelled (optimizer=%, delivery hits=%)', + current_setting('optimizer'), pg_temp.rpt_bwf_hits('anser_before_deliver', true); + END IF; +END +$$; +-- Prepare and verify each optimizer's plan before starting the short timer. +-- The cancellation check then measures executor/transport cleanup, not planning. +CREATE FUNCTION pg_temp.rpt_bwf_prepare_cancel() RETURNS void LANGUAGE plpgsql AS $$ +DECLARE + plan jsonb; +BEGIN + IF rpt_transfer_plan(pg_temp.rpt_bwf_query()) NOT LIKE '%backward class %decision=inject%' THEN + RAISE EXCEPTION 'cancel fixture has no backward injection (optimizer=%)', current_setting('optimizer'); + END IF; + EXECUTE 'PREPARE rpt_bwf_cancel_plan AS ' || pg_temp.rpt_bwf_query(); + EXECUTE 'EXPLAIN (VERBOSE, FORMAT JSON) EXECUTE rpt_bwf_cancel_plan' INTO plan; + IF plan::text NOT LIKE '%Anser Bloom Producer%' OR plan::text NOT LIKE '%Anser Bloom Consumer%' THEN + RAISE EXCEPTION 'cancel fixture lost its runtime nodes: %', plan; + END IF; +END +$$; +-- Separate top-level commands arm statement_timeout for each optimizer. +SET optimizer = off; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_prepare_cancel(); END $$; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'sleep', true); END $$; +SET anser.timeout_ms = 600000; +SET statement_timeout = '300ms'; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_cancel(); END $$; +SET statement_timeout = '120s'; +DO $$ +BEGIN + IF pg_temp.rpt_bwf_hits('anser_before_deliver', true) = 0 THEN + RAISE EXCEPTION 'cancel test never reached backward delivery'; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'reset', true); + EXECUTE 'DEALLOCATE rpt_bwf_cancel_plan'; + IF pg_temp.rpt_bwf_run(true) IS DISTINCT FROM pg_temp.rpt_bwf_run(false) THEN + RAISE EXCEPTION 'session failed after backward cancellation'; + END IF; +END +$$; +SET optimizer = on; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_prepare_cancel(); END $$; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'sleep', true); END $$; +SET statement_timeout = '300ms'; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_cancel(); END $$; +SET statement_timeout = '120s'; +DO $$ +BEGIN + IF pg_temp.rpt_bwf_hits('anser_before_deliver', true) = 0 THEN + RAISE EXCEPTION 'ORCA cancel test never reached backward delivery'; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'reset', true); + EXECUTE 'DEALLOCATE rpt_bwf_cancel_plan'; + IF pg_temp.rpt_bwf_run(true) IS DISTINCT FROM pg_temp.rpt_bwf_run(false) THEN + RAISE EXCEPTION 'ORCA session failed after backward cancellation'; + END IF; +END +$$; +DROP TABLE rpt_bwf_root, rpt_bwf_member; +DROP EXTENSION robust_predicate_transfer; +RESET rpt.enable; +RESET rpt.forward; +RESET rpt.backward; +RESET rpt.forward_max_exchange_ms; +RESET anser.runtime_filter; +RESET anser.timeout_ms; +RESET optimizer; +RESET statement_timeout; +RESET enable_nestloop; +RESET enable_mergejoin; +RESET gp_enable_hashjoin_size_heuristic; +RESET work_mem; diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out b/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out new file mode 100644 index 00000000000..f57fb56709a --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out @@ -0,0 +1,349 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Forward reducer, end to end: the InitPlan is built, its filters +-- reach the main plan across the dispatch boundary, intermediate hops work +-- both through Anser and in-process, and results never change. Both +-- optimizers; every check raises with the plan or dump that failed it. +-- Faults live in rpt_forward_faults. +CREATE EXTENSION robust_predicate_transfer; +SET anser.runtime_filter = off; +SET rpt.backward = off; +SET statement_timeout = '120s'; +-- Star: two dimensions on two different fact keys (two hops into the root). +CREATE TABLE rpt_fwd_fact (id int, d1 int, d2 int, v int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwd_d1 (k int, a int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwd_d2 (k int, b int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwd_empty (k int, a int) DISTRIBUTED RANDOMLY; +-- Snowflake: fact -> dimension -> subdimension, a chain of two hops whose +-- first hop is consumed inside the reducer. rpt_fwd_dl and rpt_fwd_sub are +-- distributed on their join columns, so that hop is local (in-process); +-- rpt_fwd_dr is the same dimension distributed randomly, so the hop is global. +CREATE TABLE rpt_fwd_dl (k int, s int) DISTRIBUTED BY (s); +CREATE TABLE rpt_fwd_dr (k int, s int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwd_sub (k int, c int) DISTRIBUTED BY (k); +INSERT INTO rpt_fwd_fact + SELECT i, CASE WHEN i % 97 = 0 THEN NULL ELSE i % 1000 END, i % 500, i + FROM generate_series(1, 200000) i; +INSERT INTO rpt_fwd_d1 SELECT i, i FROM generate_series(0, 999) i; +INSERT INTO rpt_fwd_d2 SELECT i, i FROM generate_series(0, 499) i; +INSERT INTO rpt_fwd_dl SELECT i, i % 100 FROM generate_series(0, 999) i; +INSERT INTO rpt_fwd_dr SELECT i, i % 100 FROM generate_series(0, 999) i; +INSERT INTO rpt_fwd_sub SELECT i, i FROM generate_series(0, 99) i; +ANALYZE rpt_fwd_fact; +ANALYZE rpt_fwd_d1; +ANALYZE rpt_fwd_d2; +ANALYZE rpt_fwd_empty; +ANALYZE rpt_fwd_dl; +ANALYZE rpt_fwd_dr; +ANALYZE rpt_fwd_sub; +-- Legal LEAKPROOF function: the error reveals nothing about its argument. +-- It is still unsafe for an eager reducer when the main scan never executes. +CREATE FUNCTION pg_temp.rpt_fwd_constant_error(integer) RETURNS boolean +LANGUAGE plpgsql IMMUTABLE LEAKPROOF AS $$ +BEGIN + RAISE EXCEPTION 'constant failure'; +END +$$; +CREATE FUNCTION pg_temp.rpt_fwd_plan(q text, do_analyze boolean) RETURNS jsonb +LANGUAGE plpgsql AS $$ +DECLARE + plan jsonb; +BEGIN + EXECUTE format('EXPLAIN (%s VERBOSE, FORMAT JSON) %s', + CASE WHEN do_analyze THEN 'ANALYZE,' ELSE '' END, q) INTO plan; + RETURN plan; +END +$$; +-- Result of q as one comparable text value, with RPT on or off. +CREATE FUNCTION pg_temp.rpt_fwd_result(q text, rpt boolean) RETURNS text +LANGUAGE plpgsql AS $$ +DECLARE + r text; +BEGIN + PERFORM set_config('rpt.enable', CASE WHEN rpt THEN 'on' ELSE 'off' END, true); + EXECUTE format('SELECT string_agg(t::text, %L ORDER BY t::text) FROM (%s) t', ';', q) INTO r; + PERFORM set_config('rpt.enable', 'on', true); + RETURN coalesce(r, ''); +END +$$; +-- Nodes of a provider inside InitPlans (reducer) or outside them (main plan); +-- a NULL provider matches every node. +CREATE FUNCTION pg_temp.rpt_fwd_nodes(plan jsonb, provider text, in_reducer boolean) +RETURNS SETOF jsonb LANGUAGE sql AS $$ + WITH RECURSIVE walk(node, in_init) AS ( + SELECT plan->0->'Plan', false + UNION ALL + SELECT child, in_init OR coalesce(child->>'Parent Relationship', '') = 'InitPlan' + FROM walk CROSS JOIN LATERAL jsonb_array_elements(coalesce(node->'Plans', '[]')) child + ) + SELECT node FROM walk + WHERE in_init = in_reducer AND (provider IS NULL OR node->>'Custom Plan Provider' = provider) +$$; +CREATE FUNCTION pg_temp.rpt_fwd_count(plan jsonb, provider text, in_reducer boolean) +RETURNS bigint LANGUAGE sql AS $$ + SELECT count(*) FROM pg_temp.rpt_fwd_nodes(plan, provider, in_reducer) +$$; +-- Rows removed by forward filters in the main plan: by a consumer itself, or +-- by the Seq Scan it pushed the filter into. +CREATE FUNCTION pg_temp.rpt_fwd_removed(plan jsonb) RETURNS float8 +LANGUAGE sql AS $$ + SELECT coalesce(sum(coalesce((node->>'Rows Removed by Bloom Filter')::float8, 0) + + coalesce((node->>'Rows Removed by Pushdown Runtime Filter')::float8, 0)), 0) + FROM pg_temp.rpt_fwd_nodes(plan, NULL, false) node +$$; +DO $$ +DECLARE + opt text; + q text; + selectivity_limit text; + dump text; + plan jsonb; + prep_n bigint; + prep_s bigint; + star constant text := + 'SELECT count(*) AS n, sum(f.v) AS s FROM rpt_fwd_fact f + JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k JOIN rpt_fwd_d2 d2 ON f.d2 = d2.k + WHERE d1.a < 50 AND d2.b < 100'; + flake_local constant text := + 'SELECT count(*) AS n, sum(f.v) AS s FROM rpt_fwd_fact f + JOIN rpt_fwd_dl d ON f.d1 = d.k JOIN rpt_fwd_sub u ON d.s = u.k + WHERE u.c < 5'; + flake_global constant text := + 'SELECT count(*) AS n, sum(f.v) AS s FROM rpt_fwd_fact f + JOIN rpt_fwd_dr d ON f.d1 = d.k JOIN rpt_fwd_sub u ON d.s = u.k + WHERE u.c < 5'; +BEGIN + PERFORM set_config('rpt.enable', 'on', true); + PERFORM set_config('rpt.forward_min_root_rows', '0', true); + PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + + -- Shape: a reducer InitPlan with producers, a consumer in the main plan. + dump := rpt_transfer_plan(star); + IF dump NOT LIKE '%decision=inject%' THEN + RAISE EXCEPTION 'star not selected (optimizer=%): %', opt, dump; + END IF; + plan := pg_temp.rpt_fwd_plan(star, false); + IF pg_temp.rpt_fwd_count(plan, 'Anser Bloom Producer', true) = 0 OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', false) = 0 THEN + RAISE EXCEPTION 'reducer shape missing (optimizer=%): %', opt, plan; + END IF; + + -- Two root consumers must really be stacked. VERBOSE deparses their + -- INDEX_VAR outputs; copying the child's identity tlist here used to + -- make EXPLAIN loop forever in resolve_special_varno. + IF NOT EXISTS (SELECT FROM pg_temp.rpt_fwd_nodes(plan, 'Anser Bloom Consumer', false) n + WHERE n->'Plans'->0->>'Custom Plan Provider' = 'Anser Bloom Consumer') THEN + RAISE EXCEPTION 'star did not exercise stacked consumers (optimizer=%): %', opt, plan; + END IF; + + -- The property the design rests on: filters produced in the InitPlan's + -- dispatch are applied by the main plan's dispatch. + plan := pg_temp.rpt_fwd_plan(star, true); + IF pg_temp.rpt_fwd_removed(plan) <= 0 THEN + RAISE EXCEPTION 'no rows removed by a forward filter in the main plan (optimizer=%): %', opt, plan; + END IF; + + -- Multi-hop, global: the intermediate hop is an Anser consumer + -- inside the reducer, fed by a producer of the same dispatch. + dump := rpt_transfer_plan(flake_global); + IF dump NOT LIKE '%decision=inject%' OR dump NOT LIKE '%channels=2 %' OR + dump LIKE '%in-process%' THEN + RAISE EXCEPTION 'global chain not selected as two Anser hops (optimizer=%): %', opt, dump; + END IF; + plan := pg_temp.rpt_fwd_plan(flake_global, true); + IF pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', true) = 0 OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', false) <> 1 OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Producer', true) < 2 OR + pg_temp.rpt_fwd_removed(plan) <= 0 THEN + RAISE EXCEPTION 'global chain shape or effect missing (optimizer=%): %', opt, plan; + END IF; + + -- Multi-hop, local: the intermediate hop never leaves the QE. + dump := rpt_transfer_plan(flake_local); + IF dump NOT LIKE '%decision=inject%' OR dump NOT LIKE '%local in-process%' THEN + RAISE EXCEPTION 'local hop not in-process (optimizer=%): %', opt, dump; + END IF; + plan := pg_temp.rpt_fwd_plan(flake_local, true); + IF pg_temp.rpt_fwd_count(plan, 'RPT Local Bloom Producer', true) <> 1 OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', false) <> 1 OR + pg_temp.rpt_fwd_count(plan, 'RPT Local Bloom Consumer', true) <> 1 OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', true) <> 0 OR + (SELECT sum((n->>'Rows Removed by Local Bloom Filter')::float8) + FROM pg_temp.rpt_fwd_nodes(plan, 'RPT Local Bloom Consumer', true) n) <= 0 OR + pg_temp.rpt_fwd_removed(plan) <= 0 THEN + RAISE EXCEPTION 'local chain shape or effect missing (optimizer=%): %', opt, plan; + END IF; + + -- Results never change: star, chains, empty + -- filters, NULL keys, a volatile qual (dropped whole in the reducer), + -- an empty dimension, a preserved side. + FOREACH q IN ARRAY ARRAY[ + star, + flake_local, + flake_global, + 'SELECT f.id, f.v FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 3', + -- Existing scalar InitPlans and correlated expression SubPlans + -- must be walked with the statement's subplan lookup context. + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 50 AND f.v <= (SELECT max(v) FROM rpt_fwd_fact)', + 'SELECT f.id, (SELECT max(b) FROM rpt_fwd_d2 d2 WHERE d2.k = f.d2) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 50 AND f.id < 10', + + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 0', + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a IN (0,1,2,3,4,5,6,7,8,9)', + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a NOT IN (0,1,2,3,4,5,6,7,8,9)', + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 50 AND random() >= 0', + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_empty e ON f.d1 = e.k WHERE e.a < 50', + 'SELECT count(*) FROM rpt_fwd_fact f LEFT JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k AND d1.a < 50', + 'SELECT d.k, count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_dl d ON f.d1 = d.k + JOIN rpt_fwd_sub u ON d.s = u.k WHERE u.c IN (1, 7) GROUP BY d.k' + ] LOOP + IF pg_temp.rpt_fwd_result(q, true) IS DISTINCT FROM pg_temp.rpt_fwd_result(q, false) THEN + RAISE EXCEPTION 'results differ with RPT (optimizer=%): %', opt, q; + END IF; + END LOOP; + + -- A prepared statement keeps its reducer and its answer. + EXECUTE 'PREPARE rpt_fwd_prep AS ' || star; + FOR i IN 1..2 LOOP + EXECUTE 'EXECUTE rpt_fwd_prep' INTO prep_n, prep_s; + IF row(prep_n, prep_s)::text IS DISTINCT FROM pg_temp.rpt_fwd_result(star, false) THEN + RAISE EXCEPTION 'prepared statement result differs (optimizer=%, run %)', opt, i; + END IF; + END LOOP; + EXECUTE 'DEALLOCATE rpt_fwd_prep'; + + -- Gates: below the root-row threshold, nothing is injected. + PERFORM set_config('rpt.forward_min_root_rows', '1e12', true); + IF rpt_transfer_plan(star) NOT LIKE '%decision=root rows below rpt.forward_min_root_rows%' OR + pg_temp.rpt_fwd_count(pg_temp.rpt_fwd_plan(star, false), NULL, true) <> 0 THEN + RAISE EXCEPTION 'root-row gate did not refuse (optimizer=%)', opt; + END IF; + PERFORM set_config('rpt.forward_min_root_rows', '0', true); + + -- Gates: an exchange budget below the estimate refuses the chain. + PERFORM set_config('rpt.forward_max_exchange_ms', '0.001', true); + IF rpt_transfer_plan(star) NOT LIKE '%decision=estimated exchange cost above%' THEN + RAISE EXCEPTION 'cost gate did not refuse (optimizer=%)', opt; + END IF; + PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); + + -- Gates: a dimension keeping 90% of its keys is not worth a reducer. + q := 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 900'; + IF rpt_transfer_plan(q) NOT LIKE '%decision=estimated root selectivity not below%' OR + pg_temp.rpt_fwd_count(pg_temp.rpt_fwd_plan(q, false), NULL, true) <> 0 THEN + RAISE EXCEPTION 'selectivity gate did not refuse (optimizer=%)', opt; + END IF; + + -- A qual that can fail is never run by the reducer. The + -- reducer runs before the main plan and over rows the main plan may + -- never read; with LIMIT 0 the main plan reads none, so a reducer + -- evaluating 100 / a would raise division by zero (a = 0 exists), and + -- so would an I/O cast it cannot prove safe. With lambda the + -- qual-less chain is refused; with lambda off it is built with no + -- quals. Either way: no error, the same answer. + q := 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE 100 / d1.a > 1'; + dump := rpt_transfer_plan(q); + IF dump NOT LIKE '%dropped in the reducer (not provably error-free)%' OR + dump NOT LIKE '%decision=estimated root selectivity not below%' THEN + RAISE EXCEPTION 'unsafe qual not dropped, or chain not refused (optimizer=%): %', opt, dump; + END IF; + q := 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE pg_temp.rpt_fwd_constant_error(d1.a)'; + IF rpt_transfer_plan(q) NOT LIKE '%dropped in the reducer (not provably error-free)%' THEN + RAISE EXCEPTION 'leakproof user function accepted by reducer (optimizer=%)', opt; + END IF; + FOREACH selectivity_limit IN ARRAY ARRAY['0.35', '1'] LOOP + PERFORM set_config('rpt.forward_max_selectivity', selectivity_limit, true); + -- The planner keeps the LIMIT 0 join, so with lambda off the + -- reducer really runs (GPORCA may fold the query away instead). + q := 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE 100 / d1.a > 1 LIMIT 0'; + IF selectivity_limit = '1' AND opt = 'off' AND rpt_transfer_plan(q) NOT LIKE '%decision=inject%' THEN + RAISE EXCEPTION 'LIMIT 0 reducer not built with lambda off: %', rpt_transfer_plan(q); + END IF; + FOREACH q IN ARRAY ARRAY[ + 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE 100 / d1.a > 1 LIMIT 0', + 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 50 AND 100 / d1.a > 1 LIMIT 0', + 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE (d1.a::text)::int < 50 LIMIT 0', + 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE pg_temp.rpt_fwd_constant_error(d1.a) LIMIT 0' + ] LOOP + IF pg_temp.rpt_fwd_result(q, true) <> '' THEN + RAISE EXCEPTION 'LIMIT 0 returned rows (optimizer=%): %', opt, q; + END IF; + END LOOP; + END LOOP; + PERFORM set_config('rpt.forward_max_selectivity', '0.35', true); + -- ... while provably safe quals are kept. + IF rpt_transfer_plan(star) LIKE '%dropped in the reducer%' THEN + RAISE EXCEPTION 'safe quals dropped (optimizer=%)', opt; + END IF; + + -- Exclusivity: with Anser's own runtime filter on, RPT+ stands down. + PERFORM set_config('anser.runtime_filter', 'on', true); + IF rpt_transfer_plan(star) NOT LIKE '%forward: needs anser.enable=on and anser.runtime_filter=off%' OR + pg_temp.rpt_fwd_count(pg_temp.rpt_fwd_plan(star, false), NULL, true) <> 0 THEN + RAISE EXCEPTION 'RPT+ did not stand down for anser.runtime_filter (optimizer=%)', opt; + END IF; + PERFORM set_config('anser.runtime_filter', 'off', true); + END LOOP; +END +$$; +-- PL/pgSQL (SPI) runs the reducer like any other caller. Its static +-- statements keep the plan of their first call, so the function runs once, +-- with RPT on, and the baseline comes from uncached dynamic SQL. +CREATE FUNCTION pg_temp.rpt_fwd_spi() RETURNS bigint LANGUAGE plpgsql AS $$ +DECLARE + total bigint := 0; + r record; +BEGIN + FOR r IN SELECT f.v FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 50 LOOP + total := total + r.v; + END LOOP; + FOR r IN SELECT f.v FROM rpt_fwd_fact f JOIN rpt_fwd_dl d ON f.d1 = d.k + JOIN rpt_fwd_sub u ON d.s = u.k WHERE u.c < 5 LOOP + total := total + r.v; + END LOOP; + RETURN total; +END +$$; +DO $$ +DECLARE + with_rpt bigint; + without bigint; +BEGIN + PERFORM set_config('rpt.forward_min_root_rows', '0', true); + PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); + PERFORM set_config('rpt.enable', 'on', true); + with_rpt := pg_temp.rpt_fwd_spi(); + PERFORM set_config('rpt.enable', 'off', true); + EXECUTE 'SELECT (SELECT sum(f.v) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k + WHERE d1.a < 50) + + (SELECT sum(f.v) FROM rpt_fwd_fact f JOIN rpt_fwd_dl d ON f.d1 = d.k + JOIN rpt_fwd_sub u ON d.s = u.k WHERE u.c < 5)' INTO without; + IF with_rpt IS DISTINCT FROM without THEN + RAISE EXCEPTION 'SPI result differs: % vs %', with_rpt, without; + END IF; +END +$$; +DROP TABLE rpt_fwd_fact, rpt_fwd_d1, rpt_fwd_d2, rpt_fwd_empty, + rpt_fwd_dl, rpt_fwd_dr, rpt_fwd_sub; +DROP EXTENSION robust_predicate_transfer; +RESET anser.runtime_filter; +RESET rpt.backward; +RESET statement_timeout; diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_forward_faults.out b/gpcontrib/robust_predicate_transfer/expected/rpt_forward_faults.out new file mode 100644 index 00000000000..8da7e0d5e2f --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_forward_faults.out @@ -0,0 +1,181 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Forward reducer under forced transport failures. Kept apart from +-- rpt_forward because gp_inject_fault exists only with the debug extensions, +-- and the regression driver skips any file that calls it. +-- +-- The chain is fact <- dimension <- subdimension with a random dimension, so +-- both hops are Anser channels and the first one has its consumer inside the +-- reducer InitPlan. Every case must return the RPT-off answer, and the next +-- command on the same gangs must answer: a reducer's dispatcher releases its +-- gangs before ExecutorEnd. If one of those connections is destroyed, libpq's +-- PGEVT_CONNDESTROY callback must remove Anser's references before it is freed. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS gp_inject_fault; +-- end_ignore +CREATE EXTENSION robust_predicate_transfer; +SET optimizer = off; +SET anser.runtime_filter = off; +SET rpt.backward = off; +SET anser.debug = on; +SET rpt.enable = on; +SET rpt.forward_min_root_rows = 0; +SET rpt.forward_max_exchange_ms = -1; +SET statement_timeout = '120s'; +CREATE TABLE rpt_fwf_fact (id int, d int, v int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwf_dim (k int, s int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwf_sub (k int, c int) DISTRIBUTED RANDOMLY; +INSERT INTO rpt_fwf_fact SELECT i, i % 1000, i FROM generate_series(1, 200000) i; +INSERT INTO rpt_fwf_dim SELECT i, i % 100 FROM generate_series(0, 999) i; +INSERT INTO rpt_fwf_sub SELECT i, i FROM generate_series(0, 99) i; +ANALYZE rpt_fwf_fact; +ANALYZE rpt_fwf_dim; +ANALYZE rpt_fwf_sub; +-- Dynamic SQL: a static statement would keep the plan of its first call, +-- with or without the reducer, whatever rpt.enable says later. +CREATE FUNCTION pg_temp.rpt_fwf_run(rpt boolean) RETURNS text LANGUAGE plpgsql AS $$ +DECLARE + r text; +BEGIN + PERFORM set_config('rpt.enable', CASE WHEN rpt THEN 'on' ELSE 'off' END, true); + EXECUTE 'SELECT count(*)::text || ''/'' || sum(f.v)::text + FROM rpt_fwf_fact f JOIN rpt_fwf_dim d ON f.d = d.k + JOIN rpt_fwf_sub u ON d.s = u.k WHERE u.c < 5' INTO r; + PERFORM set_config('rpt.enable', 'on', true); + RETURN r; +END +$$; +-- How often a fault fired, so a case cannot pass without exercising it. +CREATE FUNCTION pg_temp.rpt_fwf_hits(fault text, coordinator boolean) +RETURNS int LANGUAGE sql AS $$ + SELECT coalesce(sum(substring(gp_inject_fault(fault, 'status', dbid) + FROM 'num times hit:''([0-9]+)''')::int), 0)::int + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator +$$; +-- First occurrence only; on the coordinator a 'sleep' fault sleeps 1 s. +CREATE FUNCTION pg_temp.rpt_fwf_fault(fault text, action text, coordinator boolean) +RETURNS void LANGUAGE plpgsql AS $$ +BEGIN + IF action = 'reset' THEN + PERFORM gp_inject_fault(fault, 'reset', dbid) + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator; + ELSE + PERFORM gp_inject_fault(fault, action, '', '', '', 1, 1, + CASE WHEN coordinator THEN 1 ELSE 0 END, dbid) + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator; + END IF; +END +$$; +DO $$ +DECLARE + expected text := pg_temp.rpt_fwf_run(false); + got text; +BEGIN + IF rpt_transfer_plan('SELECT count(*) FROM rpt_fwf_fact f JOIN rpt_fwf_dim d ON f.d = d.k + JOIN rpt_fwf_sub u ON d.s = u.k WHERE u.c < 5') + NOT LIKE '%channels=2 %decision=inject%' THEN + RAISE EXCEPTION 'fixture chain not selected'; + END IF; + + -- 1. The coordinator holds the first delivery for a second; every + -- consumer waiting on it, the reducer's included, gives up at 100 ms and + -- runs unfiltered. + PERFORM set_config('anser.timeout_ms', '100', true); + PERFORM pg_temp.rpt_fwf_fault('anser_before_deliver', 'sleep', true); + got := pg_temp.rpt_fwf_run(true); + IF pg_temp.rpt_fwf_hits('anser_before_deliver', true) = 0 THEN + RAISE EXCEPTION 'held delivery: fault never fired'; + END IF; + PERFORM pg_temp.rpt_fwf_fault('anser_before_deliver', 'reset', true); + IF got IS DISTINCT FROM expected THEN + RAISE EXCEPTION 'held delivery: % <> %', got, expected; + END IF; + IF pg_temp.rpt_fwf_run(true) IS DISTINCT FROM expected THEN + RAISE EXCEPTION 'next command after held delivery'; + END IF; + + -- 2. The first consumer on each segment -- the reducer's -- walks away + -- after the first chunk of a 64 MB filter, leaving its delivery + -- unfinished on a connection of the InitPlan's gang, which is released + -- before the main plan runs. + PERFORM set_config('anser.timeout_ms', '500', true); + PERFORM set_config('anser.debug_delivery_chunk_bytes', '8192', true); + PERFORM set_config('anser.debug_min_filter_bytes', '67108864', true); + PERFORM pg_temp.rpt_fwf_fault('anser_after_chunk', 'skip', false); + got := pg_temp.rpt_fwf_run(true); + IF pg_temp.rpt_fwf_hits('anser_after_chunk', false) = 0 THEN + RAISE EXCEPTION 'walk-away: fault never fired'; + END IF; + PERFORM pg_temp.rpt_fwf_fault('anser_after_chunk', 'reset', false); + IF got IS DISTINCT FROM expected THEN + RAISE EXCEPTION 'walk-away: % <> %', got, expected; + END IF; + FOR i IN 1..2 LOOP + IF pg_temp.rpt_fwf_run(true) IS DISTINCT FROM expected THEN + RAISE EXCEPTION 'next command % after walk-away', i; + END IF; + END LOOP; + PERFORM set_config('anser.debug_delivery_chunk_bytes', '0', true); + PERFORM set_config('anser.debug_min_filter_bytes', '0', true); +END +$$; +-- 3. The statement is cancelled while the reducer waits for a delivery, so +-- the reducer's dispatcher is torn down on the abort path. Whether the +-- cancel lands before the query finishes is timing, so its output is not +-- compared; the session and its gangs must survive with the right answers. +DO $$ +BEGIN + PERFORM pg_temp.rpt_fwf_fault('anser_before_deliver', 'sleep', true); +END +$$; +SET anser.timeout_ms = 5000; +SET statement_timeout = '300ms'; +-- start_ignore +SELECT pg_temp.rpt_fwf_run(true) IS NOT NULL AS finished; +-- end_ignore +SET statement_timeout = '120s'; +RESET anser.timeout_ms; +DO $$ +BEGIN + PERFORM pg_temp.rpt_fwf_fault('anser_before_deliver', 'reset', true); + IF pg_temp.rpt_fwf_run(true) IS DISTINCT FROM pg_temp.rpt_fwf_run(false) THEN + RAISE EXCEPTION 'next command after cancel'; + END IF; +END +$$; +SELECT count(*) AS dim_rows FROM rpt_fwf_dim; + dim_rows +---------- + 1000 +(1 row) + +DROP TABLE rpt_fwf_fact, rpt_fwf_dim, rpt_fwf_sub; +DROP EXTENSION robust_predicate_transfer; +RESET rpt.enable; +RESET rpt.forward_min_root_rows; +RESET rpt.forward_max_exchange_ms; +RESET anser.debug; +RESET anser.runtime_filter; +RESET rpt.backward; +RESET optimizer; +RESET statement_timeout; diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_graph.out b/gpcontrib/robust_predicate_transfer/expected/rpt_graph.out new file mode 100644 index 00000000000..3e2fe5c29a9 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_graph.out @@ -0,0 +1,132 @@ +-- Join graph and transfer plan: equivalence classes, root choice, forward +-- chain shape, and consumer eligibility. Assertions are over structural +-- properties of the dump (LIKE/strpos), so the test does not depend on +-- estimate printouts or tie-breaking cosmetics. +CREATE EXTENSION robust_predicate_transfer; +CREATE TABLE rpt_a (a int, b int) DISTRIBUTED BY (a); +CREATE TABLE rpt_b (a int) DISTRIBUTED BY (a); +CREATE TABLE rpt_c (a int) DISTRIBUTED BY (a); +CREATE TABLE rpt_d (x int, y int) DISTRIBUTED BY (x); +CREATE TABLE rpt_r (a int) DISTRIBUTED REPLICATED; +INSERT INTO rpt_a SELECT i, i FROM generate_series(1, 100) i; +INSERT INTO rpt_b SELECT i FROM generate_series(1, 10) i; +INSERT INTO rpt_c SELECT i FROM generate_series(1, 50) i; +INSERT INTO rpt_d SELECT i, i FROM generate_series(1, 20) i; +ANALYZE rpt_a; +ANALYZE rpt_b; +ANALYZE rpt_c; +ANALYZE rpt_d; +INSERT INTO rpt_r SELECT i FROM generate_series(1, 10) i; +ANALYZE rpt_r; +DO $$ +DECLARE + opt text; + dump text; + q text; + distributed_rtindex text; + replicated_rtindex text; + preserved_name text; + preserved_rtindex text; + expected_members text; + member_count int; +BEGIN + IF EXISTS (SELECT FROM pg_proc p, LATERAL aclexplode(p.proacl) acl + WHERE p.oid = 'rpt_transfer_plan(text)'::regprocedure + AND acl.grantee = 0 AND acl.privilege_type = 'EXECUTE') THEN + RAISE EXCEPTION 'diagnostic function is executable by PUBLIC'; + END IF; + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + FOREACH q IN ARRAY ARRAY[ + 'SELECT * FROM rpt_a JOIN rpt_b ON rpt_a.a = rpt_b.a JOIN rpt_c ON rpt_b.a = rpt_c.a WHERE rpt_a.b > 50', + 'SELECT * FROM rpt_a JOIN rpt_b ON rpt_a.a = rpt_b.a JOIN rpt_c ON rpt_a.a = rpt_c.a WHERE rpt_a.b > 50', + 'SELECT * FROM rpt_a JOIN rpt_b ON rpt_a.a = rpt_b.a JOIN rpt_c ON rpt_b.a = rpt_c.a AND rpt_a.a = rpt_c.a WHERE rpt_a.b > 50' + ] LOOP + dump := rpt_transfer_plan(q); + distributed_rtindex := substring(dump FROM 'vertex ([0-9]+) rpt_a rows='); + SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) + INTO member_count, expected_members + FROM regexp_matches(dump, 'vertex ([0-9]+) (rpt_a|rpt_b|rpt_c) rows=', 'g') AS vertices(parts); + IF distributed_rtindex IS NULL OR member_count <> 3 OR + dump NOT LIKE ('%members=' || expected_members || ' leader=' || distributed_rtindex || chr(10) || '%') THEN + RAISE EXCEPTION 'class/root mismatch (optimizer=%): %', opt, dump; + END IF; + IF dump NOT LIKE ('%reducer order:' || chr(10) || '%') THEN + RAISE EXCEPTION 'unfiltered tables entered reducer: %', dump; + END IF; + END LOOP; + -- A co-distributed star must stay local after Algorithm 1 creates + -- implied dimension-to-dimension hops, in both transfer stages. + dump := rpt_transfer_plan('SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON a.a = c.a JOIN rpt_d d ON a.a = d.x WHERE a.b > 0 AND b.a > 0 AND c.a > 0 AND d.y > 0'); + IF dump NOT LIKE '%forward hop%' OR dump NOT LIKE '%backward hop%' OR + dump LIKE '%merge=global%' OR + EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE 'consumer_ok % yes') THEN + RAISE EXCEPTION 'local star uses global/blocking filters (optimizer=%): %', opt, dump; + END IF; + -- Replicated -> hash is local; the reverse broadcast stays global. + dump := rpt_transfer_plan('SELECT * FROM rpt_a JOIN rpt_r ON rpt_a.a = rpt_r.a WHERE rpt_a.b > 0 AND rpt_r.a > 0'); + -- Resolve table names from vertex records: ORCA can reorder rtindexes. + distributed_rtindex := substring(dump FROM 'vertex ([0-9]+) rpt_a rows='); + replicated_rtindex := substring(dump FROM 'vertex ([0-9]+) rpt_r rows='); + IF distributed_rtindex IS NULL OR replicated_rtindex IS NULL OR + dump NOT LIKE ('%forward hop ' || replicated_rtindex || '->' || distributed_rtindex || ' merge=local%') OR + dump NOT LIKE ('%backward hop ' || distributed_rtindex || '->' || replicated_rtindex || ' merge=global%') THEN + RAISE EXCEPTION 'replicated direction mismatch (optimizer=%): %', opt, dump; + END IF; + dump := rpt_transfer_plan('SELECT * FROM rpt_a JOIN rpt_d ON rpt_a.a = rpt_d.x AND rpt_a.b = rpt_d.y WHERE rpt_a.b > 50'); + SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) + INTO member_count, expected_members + FROM regexp_matches(dump, 'vertex ([0-9]+) (rpt_a|rpt_d) rows=', 'g') AS vertices(parts); + IF member_count <> 2 OR + (SELECT count(*) FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('class %: members=' || expected_members || ' leader=%')) <> 2 THEN + RAISE EXCEPTION 'expected two compound-key classes: %', dump; + END IF; + FOR q, preserved_name IN + SELECT * FROM (VALUES + ('SELECT * FROM rpt_a LEFT JOIN rpt_b ON rpt_a.a = rpt_b.a WHERE rpt_a.b > 50', 'rpt_a'), + ('SELECT * FROM rpt_b RIGHT JOIN rpt_a ON rpt_a.a = rpt_b.a WHERE rpt_a.b > 50', 'rpt_a'), + ('SELECT * FROM rpt_c LEFT JOIN (rpt_b JOIN rpt_a ON rpt_a.a = rpt_b.a AND rpt_b.a > 0) ON rpt_c.a = rpt_a.a WHERE rpt_c.a > 1', 'rpt_c') + ) AS cases(query, preserved) + LOOP + dump := rpt_transfer_plan(q); + preserved_rtindex := substring(dump FROM ('vertex ([0-9]+) ' || preserved_name || ' rows=')); + IF preserved_rtindex IS NULL THEN + RAISE EXCEPTION 'missing preserved table % (optimizer=%): %', preserved_name, opt, dump; + END IF; + IF EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('vertex ' || preserved_rtindex || ' %') + AND line LIKE '%apply from%') THEN + RAISE EXCEPTION 'preserved side reduced: %', dump; + END IF; + -- The first LEFT JOIN must filter the nullable side, so an + -- entirely empty transfer plan cannot satisfy this safety test. + IF q LIKE 'SELECT * FROM rpt_a LEFT JOIN%' AND + NOT EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE 'vertex %' AND line LIKE '%apply from%') THEN + RAISE EXCEPTION 'LEFT JOIN produced no filter applications (optimizer=%): %', opt, dump; + END IF; + END LOOP; + FOREACH q IN ARRAY ARRAY[ + 'SELECT * FROM rpt_a a FULL JOIN rpt_b b ON a.a = b.a', + 'SELECT a.a IN (SELECT b.a FROM rpt_b b) FROM rpt_a a', + 'SELECT * FROM rpt_a a WHERE NOT EXISTS (SELECT FROM rpt_b b WHERE a.a = b.a)', + 'SELECT * FROM rpt_a a JOIN (SELECT a::bigint aa FROM rpt_b) s ON a.a = s.aa' + ] LOOP + IF rpt_transfer_plan(q) <> ('no transfer regions' || chr(10)) THEN + RAISE EXCEPTION 'unsupported join/key accepted (optimizer=%): %', opt, q; + END IF; + END LOOP; + dump := rpt_transfer_plan('SELECT * FROM rpt_a WHERE rpt_a.b > 50 AND rpt_a.a IN (SELECT a FROM rpt_b)'); + SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) + INTO member_count, expected_members + FROM regexp_matches(dump, 'vertex ([0-9]+) (rpt_a|rpt_b) rows=', 'g') AS vertices(parts); + IF member_count <> 2 OR dump NOT LIKE ('%members=' || expected_members || ' leader=%') THEN + RAISE EXCEPTION 'missing SEMI class: %', dump; + END IF; + END LOOP; +END +$$; +DROP TABLE rpt_a, rpt_b, rpt_c, rpt_d, rpt_r; +DROP EXTENSION robust_predicate_transfer; diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_key_types.out b/gpcontrib/robust_predicate_transfer/expected/rpt_key_types.out new file mode 100644 index 00000000000..b3859499d0e --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_key_types.out @@ -0,0 +1,92 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Equality modulo 10 equates different raw Datum representations. RPT must +-- reject it even though the type is by-value and has default operator classes. +BEGIN; +SET LOCAL client_min_messages = warning; +CREATE EXTENSION robust_predicate_transfer; +CREATE SCHEMA rpt_eq_repro; +SET LOCAL search_path=rpt_eq_repro,pg_catalog; +SET LOCAL optimizer=off; +SET LOCAL statement_timeout='30s'; +SET LOCAL anser.runtime_filter=off; +SET LOCAL rpt.enable=off; +-- Legal by-value type whose equality identifies congruent integers mod 10. +CREATE TYPE modint; +CREATE FUNCTION modint_in(cstring) RETURNS modint AS 'int4in' LANGUAGE internal IMMUTABLE STRICT; +CREATE FUNCTION modint_out(modint) RETURNS cstring AS 'int4out' LANGUAGE internal IMMUTABLE STRICT; +CREATE TYPE modint(INPUT=modint_in,OUTPUT=modint_out,INTERNALLENGTH=4,PASSEDBYVALUE,ALIGNMENT=int4); +CREATE CAST (modint AS integer) WITHOUT FUNCTION AS IMPLICIT; +CREATE CAST (integer AS modint) WITHOUT FUNCTION AS IMPLICIT; +CREATE FUNCTION modint_eq(modint,modint) RETURNS boolean LANGUAGE plpgsql IMMUTABLE STRICT + AS $$ BEGIN RETURN ($1::integer % 10)=($2::integer % 10); END $$; +CREATE FUNCTION modint_hash(modint) RETURNS integer LANGUAGE sql IMMUTABLE STRICT + AS 'SELECT hashint4($1::integer % 10)'; +CREATE OPERATOR = (LEFTARG=modint,RIGHTARG=modint,FUNCTION=modint_eq,COMMUTATOR='=',HASHES); +CREATE OPERATOR CLASS modint_hash_ops DEFAULT FOR TYPE modint USING hash AS + OPERATOR 1 = (modint,modint), FUNCTION 1 modint_hash(modint); +-- Supply the type's own btree family as well: otherwise PostgreSQL can +-- choose integer's default family via the explicitly binary-compatible cast. +CREATE FUNCTION modint_cmp(modint,modint) RETURNS integer LANGUAGE sql IMMUTABLE STRICT + AS 'SELECT CASE WHEN $1::integer % 10 < $2::integer % 10 THEN -1 WHEN $1::integer % 10 > $2::integer % 10 THEN 1 ELSE 0 END'; +CREATE FUNCTION modint_lt(modint,modint) RETURNS boolean LANGUAGE sql IMMUTABLE STRICT AS 'SELECT modint_cmp($1,$2)<0'; +CREATE FUNCTION modint_le(modint,modint) RETURNS boolean LANGUAGE sql IMMUTABLE STRICT AS 'SELECT modint_cmp($1,$2)<=0'; +CREATE FUNCTION modint_gt(modint,modint) RETURNS boolean LANGUAGE sql IMMUTABLE STRICT AS 'SELECT modint_cmp($1,$2)>0'; +CREATE FUNCTION modint_ge(modint,modint) RETURNS boolean LANGUAGE sql IMMUTABLE STRICT AS 'SELECT modint_cmp($1,$2)>=0'; +CREATE OPERATOR < (LEFTARG=modint,RIGHTARG=modint,FUNCTION=modint_lt); +CREATE OPERATOR <= (LEFTARG=modint,RIGHTARG=modint,FUNCTION=modint_le); +CREATE OPERATOR > (LEFTARG=modint,RIGHTARG=modint,FUNCTION=modint_gt); +CREATE OPERATOR >= (LEFTARG=modint,RIGHTARG=modint,FUNCTION=modint_ge); +CREATE OPERATOR CLASS modint_btree_ops DEFAULT FOR TYPE modint USING btree AS + OPERATOR 1 <, OPERATOR 2 <=, OPERATOR 3 =, OPERATOR 4 >=, OPERATOR 5 >, FUNCTION 1 modint_cmp(modint,modint); +CREATE TABLE fact(k modint) DISTRIBUTED RANDOMLY; +CREATE TABLE dim(k modint,a int) DISTRIBUTED RANDOMLY; +INSERT INTO fact SELECT i::modint FROM generate_series(11,1010)i; +INSERT INTO dim SELECT i::modint,i FROM generate_series(1,10)i; +ANALYZE fact; +ANALYZE dim; +SET LOCAL rpt.forward_min_root_rows=0; +SET LOCAL rpt.forward_max_exchange_ms=-1; +SET LOCAL rpt.forward_max_selectivity=1; +DO $$ +DECLARE + opt text; + baseline bigint; + filtered bigint; + dump text; + q constant text := 'SELECT count(*) FROM fact f JOIN dim d ON f.k = d.k WHERE d.a < 11'; +BEGIN + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + PERFORM set_config('rpt.enable', 'off', true); + EXECUTE q INTO baseline; + PERFORM set_config('rpt.enable', 'on', true); + dump := public.rpt_transfer_plan(q); + EXECUTE q INTO filtered; + IF baseline <> 1000 OR filtered IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'custom equality lost rows (optimizer=%): % vs %', opt, filtered, baseline; + END IF; + IF dump NOT LIKE 'no transfer regions%' THEN + RAISE EXCEPTION 'custom equality was accepted (optimizer=%): %', opt, dump; + END IF; + END LOOP; +END +$$; +ROLLBACK; diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_regions.out b/gpcontrib/robust_predicate_transfer/expected/rpt_regions.out new file mode 100644 index 00000000000..57912fd02df --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_regions.out @@ -0,0 +1,78 @@ +-- Region discovery: transfer regions are connected components of the join +-- graph over base scans, and every operator the key resolver cannot descend +-- (aggregate, set op, window, limit, SRF/UNNEST, CteScan, partitioned +-- table) is a boundary -- queries whose only join key comes through one have +-- no region at all. +CREATE EXTENSION robust_predicate_transfer; +CREATE TABLE rpt_a (a int, b int) DISTRIBUTED BY (a); +CREATE TABLE rpt_b (a int) DISTRIBUTED BY (a); +CREATE TABLE rpt_c (a int) DISTRIBUTED BY (a); +INSERT INTO rpt_a SELECT i, i FROM generate_series(1, 100) i; +INSERT INTO rpt_b SELECT i FROM generate_series(1, 10) i; +INSERT INTO rpt_c SELECT i FROM generate_series(1, 50) i; +ANALYZE rpt_a; +ANALYZE rpt_b; +ANALYZE rpt_c; +-- A partitioned table joins through an Append, which resolution cannot cross. +CREATE TABLE rpt_p (a int) DISTRIBUTED BY (a) + PARTITION BY RANGE (a) (START (1) END (101) EVERY (50)); +INSERT INTO rpt_p SELECT i FROM generate_series(1, 100) i; +ANALYZE rpt_p; +DO $$ +DECLARE + opt text; + q text; + dump text; + expected_members text; + member_count int; +BEGIN + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + FOREACH q IN ARRAY ARRAY[ + 'SELECT 1', + 'SELECT * FROM rpt_a a JOIN (SELECT a, count(*) c FROM rpt_b GROUP BY a) s ON a.a = s.a WHERE s.c > 1', + 'SELECT * FROM rpt_a a JOIN (SELECT a FROM rpt_b UNION SELECT a FROM rpt_c) u ON a.a = u.a', + 'SELECT * FROM rpt_a a JOIN (SELECT a, row_number() OVER () r FROM rpt_b) w ON a.a = w.a WHERE w.r > 1', + 'SELECT * FROM rpt_a a JOIN (SELECT a FROM rpt_b ORDER BY a LIMIT 5) l ON a.a = l.a', + 'SELECT * FROM rpt_a a JOIN (SELECT u FROM rpt_b, unnest(ARRAY[a]) u) s ON a.a = s.u', + 'SELECT * FROM rpt_a a JOIN rpt_p p ON a.a = p.a' + ] LOOP + dump := rpt_transfer_plan(q); + IF dump <> ('no transfer regions' || chr(10)) THEN + RAISE EXCEPTION 'region boundary crossed (optimizer=%): %: %', opt, q, dump; + END IF; + END LOOP; + -- PostgreSQL retains a CteScan boundary here. ORCA exposes the + -- underlying base scans, so its unfiltered class is expected. + q := 'WITH w AS MATERIALIZED (SELECT a FROM rpt_b) SELECT * FROM rpt_a a JOIN w ON a.a = w.a'; + dump := rpt_transfer_plan(q); + IF opt = 'off' THEN + IF dump <> ('no transfer regions' || chr(10)) THEN + RAISE EXCEPTION 'CteScan boundary crossed: %', dump; + END IF; + ELSE + SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) + INTO member_count, expected_members + FROM regexp_matches(dump, 'vertex ([0-9]+) (a|rpt_b) rows=', 'g') AS vertices(parts); + IF member_count <> 2 OR + dump NOT LIKE ('%members=' || expected_members || ' leader=%') OR + dump NOT LIKE ('%reducer order:' || chr(10) || '%') OR + dump LIKE '%build for%' THEN + RAISE EXCEPTION 'unexpected ORCA materialized CTE plan: %', dump; + END IF; + END IF; + dump := rpt_transfer_plan('SELECT * FROM rpt_b JOIN rpt_a ON rpt_b.a = rpt_a.a'); + SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) + INTO member_count, expected_members + FROM regexp_matches(dump, 'vertex ([0-9]+) (rpt_a|rpt_b) rows=', 'g') AS vertices(parts); + IF member_count <> 2 OR dump NOT LIKE ('%members=' || expected_members || ' leader=%') OR + dump NOT LIKE ('%reducer order:' || chr(10) || '%') OR + dump LIKE '%build for%' THEN + RAISE EXCEPTION 'unfiltered pair produced transfers: %', dump; + END IF; + END LOOP; +END +$$; +DROP TABLE rpt_p; +DROP TABLE rpt_a, rpt_b, rpt_c; +DROP EXTENSION robust_predicate_transfer; diff --git a/gpcontrib/robust_predicate_transfer/include/rpt.h b/gpcontrib/robust_predicate_transfer/include/rpt.h new file mode 100644 index 00000000000..4d6a9bf1782 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/include/rpt.h @@ -0,0 +1,427 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rpt.h + * Shared data model for the robust_predicate_transfer extension. + * + * The structures here are a port of the RPT+ artifact's transfer graph + * (embryo-labs/dynamic-predicate-transfer, MIT license, Copyright 2018-2025 + * Stichting DuckDB Foundation; see licenses/LICENSE-mit-duckdb-rpt.txt): + * DuckDB's ColumnBinding becomes RptKey (rtindex, attno), EdgeInfo becomes + * RptEdgeInfo, JoinKeyTableGroup becomes RptKeyGroup, and dag.hpp's + * GraphNode/GraphEdge become RptGraphNode/RptGraphEdge. What does not exist + * there is ours: the extraction of the join graph from a finished + * PlannedStmt (rptregion.cpp) replaces DuckDB's TableOperatorManager, and + * the consumer-eligibility check answers an MPP question (Anser's deadlock + * rule) the single-node original never had. + * + * STL containers own malloc-backed storage. PostgreSQL errors must be + * translated to C++ exceptions before unwinding those containers, then + * rethrown at the C boundary. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/include/rpt.h + * + *------------------------------------------------------------------------- + */ +#ifndef RPT_H +#define RPT_H + +/* libstdc++ locale headers may include libintl.h. Parse them before + * postgres.h defines gettext macros in builds without NLS. */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern "C" +{ +#include "postgres.h" + +#include "nodes/plannodes.h" +#include "nodes/primnodes.h" +#include "lib/stringinfo.h" +} + +/* + * Like gpopt's GP_WRAP: never longjmp over a live C++ container. Only call + * PostgreSQL through this boundary; callbacks must do the same for their + * own PostgreSQL calls. The guard also restores PG's stacks when a callback + * throws a C++ exception through a C walker. + */ +struct RptPgError {}; + +struct RptPgStackGuard +{ + sigjmp_buf *exception_stack; + ErrorContextCallback *context_stack; + + RptPgStackGuard() : exception_stack(PG_exception_stack), + context_stack(error_context_stack) {} + ~RptPgStackGuard() + { + PG_exception_stack = exception_stack; + error_context_stack = context_stack; + } +}; + +template +auto RptPgCall(const F &call) -> decltype(call()) +{ + RptPgStackGuard guard; + sigjmp_buf local; + int saved_dispatch_nest_level = dispatch_nest_level; + + if (sigsetjmp(local, 0) != 0) + { + dispatch_nest_level = saved_dispatch_nest_level; + throw RptPgError(); + } + PG_exception_stack = &local; + return call(); +} + +/* GUCs (defined in rptinit.cpp). */ +extern bool rpt_enable; +extern bool rpt_debug; +extern bool rpt_backward; +extern int rpt_max_backward_consumers; +extern int rpt_max_chain_depth; + +/* + * One join key: a column of a base relation. Ports DuckDB's ColumnBinding; + * the union-find element for equivalence classes. + */ +struct RptKey +{ + int rtindex; /* rangetable index of the base relation */ + AttrNumber attno; /* column in that relation */ +}; + +inline bool +operator<(const RptKey &a, const RptKey &b) +{ + return a.rtindex < b.rtindex || + (a.rtindex == b.rtindex && a.attno < b.attno); +} + +inline bool +operator==(const RptKey &a, const RptKey &b) +{ + return a.rtindex == b.rtindex && a.attno == b.attno; +} + +/* + * A base scan taking part in at least one resolved equijoin edge; the join + * graph's vertex. Accepted scan types are the ones a reducer pass could + * replay: SeqScan (heap and AO/AOCO), IndexScan, IndexOnlyScan, + * BitmapHeapScan. Everything else never becomes a vertex, because no key + * resolution reaches it. + */ +struct RptVertexInfo +{ + int rtindex; /* range-table index of the scan */ + Plan *scan; /* the base scan node */ + Oid relid = InvalidOid; + int64 cardinality; /* scan->plan_rows */ + bool filtered; /* the scan carries restriction quals */ + bool intermediate; /* touches >1 equivalence class */ + bool unfiltered; /* neither filtered nor intermediate */ + bool replicated = false; + int numsegments = 0; + std::vector distkeys; + std::vector distopclasses; + std::set join_keys; /* this table's keys over all its edges */ +}; + +/* + * One equijoin edge between two vertices. Ports EdgeInfo. All equality + * clauses of one join over the same vertex pair merge into one edge, so + * clauses.size() is the shared-attribute count the paper uses as the edge + * weight; each clause is one (left key, right key) pair. + * + * protect_left/protect_right name the protected destination: the preserved side of an + * outer join keeps its rows whether or not they match, so it must never be + * reduced by the other side's keys. LEFT protects left and RIGHT + * protects right; unsupported join kinds, ANTI and FULL create no edge. + */ +struct RptEdgeInfo +{ + int left_rtindex; + int right_rtindex; /* range-table indexes (RptVertexInfo.rtindex) */ + Oid key_type; /* the equality operator's input type */ + std::vector > clauses; + bool protect_left; + bool protect_right; +}; + +/* + * One equivalence class of join keys. Ports JoinKeyTableGroup: members are + * the vertices touching the class, and the leader is the vertex the backward + * broadcast builds its single filter on (registered in Prim insertion + * order, so it is the class's earliest-placed vertex). + */ +struct RptKeyGroup +{ + std::set member_rtindexes; + int leader_rtindex; /* -1 until registered */ + RptKey leader_key; + Oid key_type; +}; + +/* Ports dag.hpp's GraphEdge: one transfer edge of the built plan. */ +struct RptGraphEdge +{ + int other_rtindex; /* producer for in-edges, consumer for out-edges */ + std::vector left_keys; /* keys on the smaller table's side */ + std::vector right_keys; /* keys on the larger table's side */ + std::vector key_types; + bool local = false; /* policy permits local merge; never use QD transport */ +}; + +/* Ports dag.hpp's GraphNode: one vertex's full transfer plan. */ +struct RptGraphNode +{ + int rtindex; + int32_t cardinality_order; /* Prim insertion order (0 = root) */ + + struct + { + std::vector in; + std::vector out; + } forward_stage, + backward_stage; +}; + +/* One class/member decision. Relink points and tuple-output positions are + * recorded only after the complete consumer and producer paths are proven. */ +struct RptBackwardConsumer +{ + RptKey class_key = {0, 0}; + RptKey producer_key = {0, 0}; + RptKey consumer_key = {0, 0}; + int producer_rtindex = 0; + int consumer_rtindex = 0; + Plan *producer_parent = NULL; + Plan *consumer_parent = NULL; + AttrNumber producer_attno = 0; + AttrNumber consumer_attno = 0; + int producer_slice_index = -1; + int consumer_slice_index = -1; + bool local = false; + bool rule9 = false; + bool strict_eligible = false; + bool eligible = false; + bool defer_first = true; + double estimated_rows_removed = -1; /* unknown without column statistics */ + std::string reason; +}; + +/* + * The whole analysis. Ports TransferGraphManager; the neighbor matrix is + * symmetric (both [a][b] and [b][a] name the same edge) as in the original. + */ +struct RptTransferMgr +{ + /* rptregion.cpp fills these from the PlannedStmt. */ + std::vector vertices; + /* rtindex -> vector index in vertices */ + std::map vertex_index_by_rtindex; + /* rtindex -> (rtindex -> edge); rewritten/pruned for forward transfers */ + std::map > neighbor_matrix; + /* Logical edges before forward-only SkipUnfilteredTable pruning. */ + std::map > logical_neighbor_matrix; + + /* rptgraph.cpp: classes and classification. */ + std::map groups; /* by class representative key */ + std::map uf_parent; /* union-find over keys */ + + /* rptplan.cpp: the transfer plan. */ + std::map graph; + std::set root_rtindexes; + std::set bridge_rtindexes; + std::vector reducer_order_rtindexes; + std::vector transfer_order_rtindexes; /* Prim insertion order of range-table indexes */ + + /* Eligibility for blocking global backward consumers only. Every global + * producer must be on a hash join's build side in a different slice from + * the probe-side consumer. Local edges never wait and do not participate; + * a vertex with no global in-edges has consumer_ok_by_rtindex=false. */ + std::map consumer_ok_by_rtindex; + std::vector backward_consumers; +}; + +/* + * Forward reducer. A channel is one forward transfer edge: the + * reducer scan of source_rtindex builds a bloom filter on source_key, and + * every scan of target_rtindex (in the reducer or main plan) applies it on + * target_key. Only the first clause of a multi-key edge is transferred; + * one key of a conjunctive equality still over-approximates, so this loses + * selectivity, never rows. + */ +struct RptForwardChannel +{ + int source_rtindex = 0; + int target_rtindex = 0; + RptKey source_key = {0, 0}; + RptKey target_key = {0, 0}; + bool local = false; /* policy-local (rptplan.cpp, rpt_local_hop) */ + bool in_process = false; /* local and consumed only inside the reducer: + * handed over in-process, no Anser channel */ + uint32 condition_id = 0; + std::string condition_key; + int64 total_elems = 0; + int64 max_payload = 0; + int64 planned_bytes = 0; + double est_cost_ms = 0; +}; + +/* One root's forward chain and the decision about it. */ +struct RptForwardComponent +{ + int root_rtindex = 0; + std::vector chain_rtindexes; /* reducer scans, producers before consumers */ + std::vector channels; + double est_cost_ms = 0; /* sum of hop estimates */ + double est_selectivity = 1; /* estimated fraction of root rows kept */ + bool accepted = false; + std::string reason; /* "inject", or why not */ +}; + +struct RptForwardParams +{ + int width = 0; /* reducer gang size: producers per channel */ + double delivery_ms_per_mib = 0; + double ms_per_mib_part = 0; /* c in c * n_producers * filter_MiB */ + double max_exchange_ms = 0; /* component budget; < 0 disables the limit */ + double min_root_rows = 0; /* root scan plan_rows below this: no reducer */ + double max_selectivity = 1; /* lambda; >= 1 disables the check */ + std::map main_consumer_processes; /* rtindex -> main scan width */ +}; + +/* + * What the reducer scan of one vertex is expected to keep, from the plan and + * the catalog (rptforward.cpp). `rows` is per segment, before quals; it sizes + * the vertex's filters together with `selectivity`, the fraction its own + * reducer quals keep -- 1 when they were dropped. Vertices with no + * entry use their plan_rows and selectivity 1. + */ +struct RptForwardVertexEst +{ + double rows = -1; + double selectivity = 1; + bool quals_dropped = false; +}; + +/* Distinct values of a column over the whole table, or < 0 when unknown. */ +typedef double (*RptNdvFn) (const RptVertexInfo &vertex, AttrNumber attno); + +/* Anser's bloom sizing (AnserRuntimeFilterSize), injectable for unit tests. */ +typedef bool (*RptFilterSizeFn) (double est_rows, int64 *total_elems, + int64 *max_payload, int64 *planned_bytes); + +/* Backward selection preserves the survey's per-edge proof and refusal. */ +struct RptBackwardTarget +{ + RptBackwardConsumer proof; + int processes = 0; + double estimated_rows_removed = -1; + bool accepted = false; + std::string reason; +}; + +struct RptBackwardClass +{ + RptKey class_key = {0, 0}; + int producer_rtindex = 0; + RptKey producer_key = {0, 0}; + int producer_processes = 0; + int64 total_elems = 0; + int64 max_payload = 0; + int64 planned_bytes = 0; + uint32 condition_id = 0; + std::string condition_key; + double estimated_producer_rows = 0; + double est_cost_ms = 0; + bool accepted = false; + std::string reason; + std::vector consumers; +}; + +extern void RptSelectBackward(const RptTransferMgr &schedule, + const std::vector &forward, + const RptForwardParams ¶ms, + const std::map &slice_processes, + RptFilterSizeFn size_fn, + std::vector *out); +extern void RptDumpBackward(const std::vector &classes, + std::string *out); + +/* rptforwardplan.cpp: pure selection over a forward schedule. */ +extern void RptSelectForward(const RptTransferMgr &forward, + const RptForwardParams ¶ms, + const std::map &est, + RptFilterSizeFn size_fn, RptNdvFn ndv_fn, + std::vector *out); +extern void RptDumpForward(const std::vector &components, + std::string *out); + +/* rptforward.cpp: the planner-hook pass. Returns a new statement, or stmt + * itself when nothing is injected; *trace receives the decision dump. May + * throw; the caller fails open. */ +extern PlannedStmt *RptInject(PlannedStmt *stmt, bool materialize, + std::string *trace); + +/* Forward reducer GUCs (rptinit.cpp). */ +extern bool rpt_forward; +extern double rpt_forward_ms_per_mib_part; +extern double rpt_delivery_ms_per_mib; +extern double rpt_forward_max_exchange_ms; +extern double rpt_forward_min_root_rows; +extern double rpt_forward_max_selectivity; + +/* rptregion.cpp: extract the join graph from the finished statement. */ +extern void RptCollectGraph(PlannedStmt *stmt, RptTransferMgr *mgr); + +extern RptKey RptFindKeyRoot(const RptKey &key, + std::map &uf_parent); + +/* rptgraph.cpp: union-find classes, classification, SkipUnfilteredTable. */ +extern void RptBuildClasses(RptTransferMgr *mgr); + +/* rptplan.cpp: the Prim forest (LargestRootUpdated), forward/backward stage + * assignment (CreateTransferPlanUpdated), consumer eligibility, and dumps. */ +extern void RptBuildTransferPlan(PlannedStmt *stmt, RptTransferMgr *mgr); +extern void RptBuildForwardPlan(const RptTransferMgr *analysis, + RptTransferMgr *forward); +extern void RptAttachReducerInitPlan(PlannedStmt *stmt, List *children, int width); +extern void RptCheckBackwardConsumers(PlannedStmt *stmt, RptTransferMgr *mgr); +extern double RptEstimateDistinct(const RptVertexInfo &vertex, AttrNumber attno); +extern void RptDumpTransferPlan(const PlannedStmt *stmt, + const RptTransferMgr *mgr, StringInfo out); + +#endif /* RPT_H */ diff --git a/gpcontrib/robust_predicate_transfer/include/rptinject.h b/gpcontrib/robust_predicate_transfer/include/rptinject.h new file mode 100644 index 00000000000..cffdd73dec7 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/include/rptinject.h @@ -0,0 +1,50 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptinject.h + * Shared scan-stack state and backward injection interface. + * + *------------------------------------------------------------------------- + */ +#ifndef RPTINJECT_H +#define RPTINJECT_H + +#include "rpt.h" + +/* One allocator and one relink point per original scan, shared by both passes. */ +struct RptInjectionState +{ + uint32 next_condition_id = 0; + int next_plan_node_id = 0; + std::map parents; + std::map tops; +}; + +extern AttrNumber RptScanKeyPosition(Plan *scan, const RptKey &key); +extern bool RptStackMatches(const RptInjectionState &state, Plan *scan, + Plan *expected_parent); +extern void RptRelinkStack(RptInjectionState *state, Plan *scan, Plan *top); +extern void RptInjectBackward(PlannedStmt *stmt, const RptTransferMgr &schedule, + const std::vector &forward, + const RptForwardParams ¶ms, + RptFilterSizeFn size_fn, bool materialize, + RptInjectionState *state, std::string *trace, + bool *injected); + +#endif /* RPTINJECT_H */ diff --git a/gpcontrib/robust_predicate_transfer/include/rptlocal.h b/gpcontrib/robust_predicate_transfer/include/rptlocal.h new file mode 100644 index 00000000000..0e3d552254e --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/include/rptlocal.h @@ -0,0 +1,40 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptlocal.h + * In-process bloom handoff for local reducer hops. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/include/rptlocal.h + * + *------------------------------------------------------------------------- + */ +#ifndef RPTLOCAL_H +#define RPTLOCAL_H + +#include "nodes/plannodes.h" + +extern void RptRegisterLocalMethods(void); +extern CustomScan *RptBuildLocalProducerScan(Plan *child, AttrNumber key_attno, + uint32 condition_id, int64 total_elems); +extern CustomScan *RptBuildLocalConsumerScan(Plan *child, AttrNumber key_attno, + uint32 condition_id, int64 total_elems); +extern bool RptIsLocalScan(const Plan *plan); + +#endif /* RPTLOCAL_H */ diff --git a/gpcontrib/robust_predicate_transfer/include/rptqual.h b/gpcontrib/robust_predicate_transfer/include/rptqual.h new file mode 100644 index 00000000000..9a3e3733a7f --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/include/rptqual.h @@ -0,0 +1,130 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptqual.h + * Trusted built-in functions for eager reducer quals. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/include/rptqual.h + * + *------------------------------------------------------------------------- + */ +#ifndef RPTQUAL_H +#define RPTQUAL_H + +#include "utils/fmgroids.h" + +/* OIDs of built-in comparisons with no argument-dependent or unconditional + * evaluation errors. LEAKPROOF alone does not promise this for user functions. + * Keep arithmetic, coercions and arbitrary extension functions out. */ +static inline bool +RptQualFunctionSafe(Oid function) +{ + switch (function) + { + case F_BOOLEQ: + case F_BOOLNE: + case F_BOOLLT: + case F_BOOLLE: + case F_BOOLGT: + case F_BOOLGE: + case F_INT2EQ: + case F_INT2NE: + case F_INT2LT: + case F_INT2LE: + case F_INT2GT: + case F_INT2GE: + case F_INT4EQ: + case F_INT4NE: + case F_INT4LT: + case F_INT4LE: + case F_INT4GT: + case F_INT4GE: + case F_INT8EQ: + case F_INT8NE: + case F_INT8LT: + case F_INT8LE: + case F_INT8GT: + case F_INT8GE: + case F_INT24EQ: + case F_INT24NE: + case F_INT24LT: + case F_INT24LE: + case F_INT24GT: + case F_INT24GE: + case F_INT42EQ: + case F_INT42NE: + case F_INT42LT: + case F_INT42LE: + case F_INT42GT: + case F_INT42GE: + case F_INT28EQ: + case F_INT28NE: + case F_INT28LT: + case F_INT28LE: + case F_INT28GT: + case F_INT28GE: + case F_INT82EQ: + case F_INT82NE: + case F_INT82LT: + case F_INT82LE: + case F_INT82GT: + case F_INT82GE: + case F_INT48EQ: + case F_INT48NE: + case F_INT48LT: + case F_INT48LE: + case F_INT48GT: + case F_INT48GE: + case F_INT84EQ: + case F_INT84NE: + case F_INT84LT: + case F_INT84LE: + case F_INT84GT: + case F_INT84GE: + case F_OIDEQ: + case F_OIDNE: + case F_OIDLT: + case F_OIDLE: + case F_OIDGT: + case F_OIDGE: + return true; + default: + return false; + } +} + +/* ScalarArrayOpExpr can call these in addition to its comparison function. */ +static inline bool +RptQualHashFunctionSafe(Oid function) +{ + switch (function) + { + case F_HASHINT2: + case F_HASHINT4: + case F_HASHINT8: + case F_HASHOID: + case F_HASHCHAR: + return true; + default: + return false; + } +} + +#endif /* RPTQUAL_H */ diff --git a/gpcontrib/robust_predicate_transfer/robust_predicate_transfer--1.0.sql b/gpcontrib/robust_predicate_transfer/robust_predicate_transfer--1.0.sql new file mode 100644 index 00000000000..caf6c091678 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/robust_predicate_transfer--1.0.sql @@ -0,0 +1,12 @@ +/* gpcontrib/robust_predicate_transfer/robust_predicate_transfer--1.0.sql */ + +-- Plan a query and dump its RPT+ transfer plan (regions, join graph, +-- equivalence classes, forward chain, backward broadcast, consumer +-- eligibility). Analysis only: nothing is injected or executed. +CREATE FUNCTION rpt_transfer_plan(query text) +RETURNS text +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- Planning exposes estimates without executing the query permission checks. +REVOKE ALL ON FUNCTION rpt_transfer_plan(text) FROM PUBLIC; diff --git a/gpcontrib/robust_predicate_transfer/robust_predicate_transfer.control b/gpcontrib/robust_predicate_transfer/robust_predicate_transfer.control new file mode 100644 index 00000000000..0b8f92d5982 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/robust_predicate_transfer.control @@ -0,0 +1,5 @@ +# robust_predicate_transfer extension +comment = 'RPT+ transfer-plan analysis over Anser runtime filters' +default_version = '1.0' +module_pathname = '$libdir/robust_predicate_transfer' +relocatable = true diff --git a/gpcontrib/robust_predicate_transfer/scripts/collect_star_example.sql b/gpcontrib/robust_predicate_transfer/scripts/collect_star_example.sql new file mode 100644 index 00000000000..38ff36dd184 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/scripts/collect_star_example.sql @@ -0,0 +1,437 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Run from an empty output directory; see the README's worked-example guide. +-- Use a dedicated database. Existing fixture schemas are never overwritten. +\set ON_ERROR_STOP on +\pset pager off +\pset format unaligned +\pset tuples_only on +\timing off +\if :{?fact_rows} +\else +\set fact_rows 1000000 +\endif +\if :{?samples} +\else +\set samples 7 +\endif +\if :{?warmups} +\else +\set warmups 2 +\endif +SELECT :'fact_rows'::bigint >= 100000 AND :'samples'::int >= 3 + AND :'warmups'::int >= 1 AS valid_input \gset +\if :valid_input +\else +\echo 'Require fact_rows >= 100000, samples >= 3, warmups >= 1.' +DO $$ BEGIN RAISE EXCEPTION 'invalid collection settings or preload configuration'; END $$; +\endif +SELECT coalesce(current_setting('anser.enable', true) = 'on' + AND array_position(libraries, 'anser') + < array_position(libraries, 'robust_predicate_transfer'), false) AS ready +FROM (SELECT string_to_array(replace(current_setting('shared_preload_libraries'), ' ', ''), ',') + AS libraries) preload \gset +\if :ready +\else +\echo 'Preload anser,robust_predicate_transfer in that order and enable Anser; restart first.' +DO $$ BEGIN RAISE EXCEPTION 'invalid collection settings or preload configuration'; END $$; +\endif +SET application_name = 'rpt_readme_star_capture'; +SET statement_timeout = '120s'; +SET anser.runtime_filter = off; +SET anser.debug = off; +SET rpt.enable = off; +SET rpt.debug = off; +\if :{?exchange_budget_ms} +SET rpt.forward_max_exchange_ms = :'exchange_budget_ms'; +\endif +CREATE EXTENSION IF NOT EXISTS robust_predicate_transfer WITH SCHEMA public; +\echo RPT_STAR setup +BEGIN; +CREATE SCHEMA rpt_readme_star; +CREATE TABLE rpt_readme_star.fact ( + sale_id int, customer_key int, product_key int, store_key int, amount_cents int +) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_readme_star.customer (k int, region int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_readme_star.product (k int, category int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_readme_star.store (k int, zone int) DISTRIBUTED RANDOMLY; +INSERT INTO rpt_readme_star.customer SELECT k, k % 10 FROM generate_series(0, 1008) k; +INSERT INTO rpt_readme_star.product SELECT k, k % 10 FROM generate_series(0, 502) k; +INSERT INTO rpt_readme_star.store SELECT k, k % 10 FROM generate_series(0, 210) k; +INSERT INTO rpt_readme_star.fact + SELECT i, (i - 1) % 1009, (i - 1) % 503, (i - 1) % 211, i % 10000 + FROM generate_series(1, :'fact_rows'::int) i; +ANALYZE rpt_readme_star.fact; +ANALYZE rpt_readme_star.customer; +ANALYZE rpt_readme_star.product; +ANALYZE rpt_readme_star.store; +COMMIT; +SELECT $query$ +SELECT count(*) AS matched_rows, + coalesce(sum(f.amount_cents), 0) AS total_amount_cents +FROM rpt_readme_star.fact f +JOIN rpt_readme_star.customer c ON f.customer_key = c.k +JOIN rpt_readme_star.product p ON f.product_key = p.k +JOIN rpt_readme_star.store s ON f.store_key = s.k +WHERE c.region < 2 AND p.category < 2 AND s.zone < 2 +$query$ AS star_query \gset +\o query.sql +\qecho :star_query ; +\o +\pset tuples_only off +\o environment.txt +SELECT version(); +SELECT pg_backend_pid() AS backend_pid; +SELECT count(*) FILTER (WHERE content >= 0 AND role = 'p') AS primaries, + count(DISTINCT hostname) FILTER (WHERE content >= 0 AND role = 'p') AS primary_hosts +FROM gp_segment_configuration; +SELECT content, role, status, mode FROM gp_segment_configuration WHERE role = 'p' ORDER BY content; +SELECT extname, extversion FROM pg_extension +WHERE extname IN ('robust_predicate_transfer', 'anser') ORDER BY extname; +SELECT name, setting, unit, source FROM pg_settings +WHERE name LIKE 'rpt.%' OR name LIKE 'anser.%' OR name IN ( + 'shared_preload_libraries', 'optimizer', 'optimizer_join_order', + 'gp_enable_hashjoin_size_heuristic', 'gp_enable_runtime_filter', + 'gp_enable_runtime_filter_pushdown', 'work_mem', 'statement_mem', + 'max_parallel_workers_per_gather', 'max_parallel_workers', + 'gp_resource_manager', 'gp_interconnect_type', 'statement_timeout' +) ORDER BY name; +\o +\pset format csv +\pset tuples_only off +\pset footer off +\o distribution.csv +SELECT c.relname, p.policytype, p.numsegments, p.distkey::text, p.distclass::text +FROM gp_distribution_policy p JOIN pg_class c ON c.oid = p.localoid +WHERE c.relnamespace = 'rpt_readme_star'::regnamespace ORDER BY c.relname; +\o +\o statistics.csv +SELECT tablename, attname, null_frac, avg_width, n_distinct, + most_common_vals::text, most_common_freqs::text +FROM pg_stats WHERE schemaname = 'rpt_readme_star' ORDER BY tablename, attname; +\o +\o cardinalities.csv +SELECT 'customer' AS relation, count(*) AS rows, count(*) FILTER (WHERE region < 2) AS selected, + count(DISTINCT k) AS key_ndv, count(DISTINCT k) FILTER (WHERE region < 2) AS selected_ndv +FROM rpt_readme_star.customer +UNION ALL +SELECT 'product', count(*), count(*) FILTER (WHERE category < 2), + count(DISTINCT k), count(DISTINCT k) FILTER (WHERE category < 2) FROM rpt_readme_star.product +UNION ALL +SELECT 'store', count(*), count(*) FILTER (WHERE zone < 2), + count(DISTINCT k), count(DISTINCT k) FILTER (WHERE zone < 2) FROM rpt_readme_star.store; +\o +\o fact-cardinalities.csv +SELECT count(*) AS rows, count(DISTINCT customer_key) AS customer_ndv, + count(DISTINCT product_key) AS product_ndv, count(DISTINCT store_key) AS store_ndv +FROM rpt_readme_star.fact; +\o +\pset format unaligned +\pset tuples_only on + +\echo RPT_STAR optimizer=off +SET optimizer = off; +\echo RPT_STAR capture=off-baseline +SET rpt.enable = off; +SET rpt.forward = on; +SET rpt.backward = on; +:star_query \gset got_ +\set expected_matched_rows :got_matched_rows +\set expected_total_amount_cents :got_total_amount_cents +SELECT :got_matched_rows::bigint = :expected_matched_rows::bigint + AND :got_total_amount_cents::bigint = :expected_total_amount_cents::bigint AS answer_matches \gset +\if :answer_matches +\else +\echo 'Result mismatch; retain all artifacts and stop.' +DO $$ BEGIN RAISE EXCEPTION 'star example result mismatch'; END $$; +\endif +\pset format csv +\pset tuples_only off +\o off-baseline.result.csv +SELECT 'off' AS optimizer_requested, 'baseline' AS arm, + :got_matched_rows::bigint AS matched_rows, + :got_total_amount_cents::bigint AS total_amount_cents; +\o +\pset format unaligned +\pset tuples_only on +\o off-baseline.explain.txt +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY) :star_query; +\o +\o off-baseline.explain.json +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY, FORMAT JSON) :star_query; +\o +\echo RPT_STAR capture=off-forward +SET rpt.enable = on; +SET rpt.forward = on; +SET rpt.backward = off; +:star_query \gset got_ +SELECT :got_matched_rows::bigint = :expected_matched_rows::bigint + AND :got_total_amount_cents::bigint = :expected_total_amount_cents::bigint AS answer_matches \gset +\if :answer_matches +\else +\echo 'Result mismatch; retain all artifacts and stop.' +DO $$ BEGIN RAISE EXCEPTION 'star example result mismatch'; END $$; +\endif +\pset format csv +\pset tuples_only off +\o off-forward.result.csv +SELECT 'off' AS optimizer_requested, 'forward' AS arm, + :got_matched_rows::bigint AS matched_rows, + :got_total_amount_cents::bigint AS total_amount_cents; +\o +\pset format unaligned +\pset tuples_only on +\o off-forward.transfer.txt +SELECT public.rpt_transfer_plan(:'star_query'); +\o +\o off-forward.explain.txt +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY) :star_query; +\o +\o off-forward.explain.json +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY, FORMAT JSON) :star_query; +\o +\echo RPT_STAR capture=off-backward +SET rpt.enable = on; +SET rpt.forward = off; +SET rpt.backward = on; +:star_query \gset got_ +SELECT :got_matched_rows::bigint = :expected_matched_rows::bigint + AND :got_total_amount_cents::bigint = :expected_total_amount_cents::bigint AS answer_matches \gset +\if :answer_matches +\else +\echo 'Result mismatch; retain all artifacts and stop.' +DO $$ BEGIN RAISE EXCEPTION 'star example result mismatch'; END $$; +\endif +\pset format csv +\pset tuples_only off +\o off-backward.result.csv +SELECT 'off' AS optimizer_requested, 'backward' AS arm, + :got_matched_rows::bigint AS matched_rows, + :got_total_amount_cents::bigint AS total_amount_cents; +\o +\pset format unaligned +\pset tuples_only on +\o off-backward.transfer.txt +SELECT public.rpt_transfer_plan(:'star_query'); +\o +\o off-backward.explain.txt +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY) :star_query; +\o +\o off-backward.explain.json +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY, FORMAT JSON) :star_query; +\o +\echo RPT_STAR capture=off-full +SET rpt.enable = on; +SET rpt.forward = on; +SET rpt.backward = on; +:star_query \gset got_ +SELECT :got_matched_rows::bigint = :expected_matched_rows::bigint + AND :got_total_amount_cents::bigint = :expected_total_amount_cents::bigint AS answer_matches \gset +\if :answer_matches +\else +\echo 'Result mismatch; retain all artifacts and stop.' +DO $$ BEGIN RAISE EXCEPTION 'star example result mismatch'; END $$; +\endif +\pset format csv +\pset tuples_only off +\o off-full.result.csv +SELECT 'off' AS optimizer_requested, 'full' AS arm, + :got_matched_rows::bigint AS matched_rows, + :got_total_amount_cents::bigint AS total_amount_cents; +\o +\pset format unaligned +\pset tuples_only on +\o off-full.transfer.txt +SELECT public.rpt_transfer_plan(:'star_query'); +\o +\o off-full.explain.txt +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY) :star_query; +\o +\o off-full.explain.json +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY, FORMAT JSON) :star_query; +\o +\echo RPT_STAR timings optimizer=off +\timing on +-- gexec sends each column as a separate statement: settings, marker, query. +-- Only the Time line following the two-column aggregate result is a query sample. +SELECT format('SET rpt.enable = %L', enabled), + format('SET rpt.forward = %L', forward), + format('SET rpt.backward = %L', backward), + format('SELECT %L', format('RPT_SAMPLE optimizer=off arm=%s phase=%s run=%s', arm, + CASE WHEN iteration < :'warmups'::int THEN 'warmup' ELSE 'sample' END, + CASE WHEN iteration < :'warmups'::int THEN iteration + 1 + ELSE iteration - :'warmups'::int + 1 END)), + :'star_query' +FROM generate_series(0, :'warmups'::int + :'samples'::int - 1) iteration +CROSS JOIN (VALUES + (0, 'baseline', 'off', 'on', 'on'), + (1, 'forward', 'on', 'on', 'off'), + (2, 'backward', 'on', 'off', 'on'), + (3, 'full', 'on', 'on', 'on') +) arms(arm_index, arm, enabled, forward, backward) +ORDER BY iteration, (arm_index + iteration) % 4 +\gexec +\timing off + +\echo RPT_STAR optimizer=on +SET optimizer = on; +\echo RPT_STAR capture=on-baseline +SET rpt.enable = off; +SET rpt.forward = on; +SET rpt.backward = on; +:star_query \gset got_ +SELECT :got_matched_rows::bigint = :expected_matched_rows::bigint + AND :got_total_amount_cents::bigint = :expected_total_amount_cents::bigint AS answer_matches \gset +\if :answer_matches +\else +\echo 'Result mismatch; retain all artifacts and stop.' +DO $$ BEGIN RAISE EXCEPTION 'star example result mismatch'; END $$; +\endif +\pset format csv +\pset tuples_only off +\o on-baseline.result.csv +SELECT 'on' AS optimizer_requested, 'baseline' AS arm, + :got_matched_rows::bigint AS matched_rows, + :got_total_amount_cents::bigint AS total_amount_cents; +\o +\pset format unaligned +\pset tuples_only on +\o on-baseline.explain.txt +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY) :star_query; +\o +\o on-baseline.explain.json +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY, FORMAT JSON) :star_query; +\o +\echo RPT_STAR capture=on-forward +SET rpt.enable = on; +SET rpt.forward = on; +SET rpt.backward = off; +:star_query \gset got_ +SELECT :got_matched_rows::bigint = :expected_matched_rows::bigint + AND :got_total_amount_cents::bigint = :expected_total_amount_cents::bigint AS answer_matches \gset +\if :answer_matches +\else +\echo 'Result mismatch; retain all artifacts and stop.' +DO $$ BEGIN RAISE EXCEPTION 'star example result mismatch'; END $$; +\endif +\pset format csv +\pset tuples_only off +\o on-forward.result.csv +SELECT 'on' AS optimizer_requested, 'forward' AS arm, + :got_matched_rows::bigint AS matched_rows, + :got_total_amount_cents::bigint AS total_amount_cents; +\o +\pset format unaligned +\pset tuples_only on +\o on-forward.transfer.txt +SELECT public.rpt_transfer_plan(:'star_query'); +\o +\o on-forward.explain.txt +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY) :star_query; +\o +\o on-forward.explain.json +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY, FORMAT JSON) :star_query; +\o +\echo RPT_STAR capture=on-backward +SET rpt.enable = on; +SET rpt.forward = off; +SET rpt.backward = on; +:star_query \gset got_ +SELECT :got_matched_rows::bigint = :expected_matched_rows::bigint + AND :got_total_amount_cents::bigint = :expected_total_amount_cents::bigint AS answer_matches \gset +\if :answer_matches +\else +\echo 'Result mismatch; retain all artifacts and stop.' +DO $$ BEGIN RAISE EXCEPTION 'star example result mismatch'; END $$; +\endif +\pset format csv +\pset tuples_only off +\o on-backward.result.csv +SELECT 'on' AS optimizer_requested, 'backward' AS arm, + :got_matched_rows::bigint AS matched_rows, + :got_total_amount_cents::bigint AS total_amount_cents; +\o +\pset format unaligned +\pset tuples_only on +\o on-backward.transfer.txt +SELECT public.rpt_transfer_plan(:'star_query'); +\o +\o on-backward.explain.txt +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY) :star_query; +\o +\o on-backward.explain.json +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY, FORMAT JSON) :star_query; +\o +\echo RPT_STAR capture=on-full +SET rpt.enable = on; +SET rpt.forward = on; +SET rpt.backward = on; +:star_query \gset got_ +SELECT :got_matched_rows::bigint = :expected_matched_rows::bigint + AND :got_total_amount_cents::bigint = :expected_total_amount_cents::bigint AS answer_matches \gset +\if :answer_matches +\else +\echo 'Result mismatch; retain all artifacts and stop.' +DO $$ BEGIN RAISE EXCEPTION 'star example result mismatch'; END $$; +\endif +\pset format csv +\pset tuples_only off +\o on-full.result.csv +SELECT 'on' AS optimizer_requested, 'full' AS arm, + :got_matched_rows::bigint AS matched_rows, + :got_total_amount_cents::bigint AS total_amount_cents; +\o +\pset format unaligned +\pset tuples_only on +\o on-full.transfer.txt +SELECT public.rpt_transfer_plan(:'star_query'); +\o +\o on-full.explain.txt +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY) :star_query; +\o +\o on-full.explain.json +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY, FORMAT JSON) :star_query; +\o +\echo RPT_STAR timings optimizer=on +\timing on +-- gexec sends each column as a separate statement: settings, marker, query. +-- Only the Time line following the two-column aggregate result is a query sample. +SELECT format('SET rpt.enable = %L', enabled), + format('SET rpt.forward = %L', forward), + format('SET rpt.backward = %L', backward), + format('SELECT %L', format('RPT_SAMPLE optimizer=on arm=%s phase=%s run=%s', arm, + CASE WHEN iteration < :'warmups'::int THEN 'warmup' ELSE 'sample' END, + CASE WHEN iteration < :'warmups'::int THEN iteration + 1 + ELSE iteration - :'warmups'::int + 1 END)), + :'star_query' +FROM generate_series(0, :'warmups'::int + :'samples'::int - 1) iteration +CROSS JOIN (VALUES + (0, 'baseline', 'off', 'on', 'on'), + (1, 'forward', 'on', 'on', 'off'), + (2, 'backward', 'on', 'off', 'on'), + (3, 'full', 'on', 'on', 'on') +) arms(arm_index, arm, enabled, forward, backward) +ORDER BY iteration, (arm_index + iteration) % 4 +\gexec +\timing off + +SET rpt.enable = off; +\echo RPT_STAR complete +-- The fixture remains for inspection. Drop the dedicated database when finished. diff --git a/gpcontrib/robust_predicate_transfer/scripts/diagnose_forward.py b/gpcontrib/robust_predicate_transfer/scripts/diagnose_forward.py new file mode 100644 index 00000000000..5436a4b1d3e --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/scripts/diagnose_forward.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""Compatibility entry point; use diagnose_rpt.py for all RPT suites.""" +from diagnose_rpt import instrument_sql, main + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/gpcontrib/robust_predicate_transfer/scripts/diagnose_rpt.py b/gpcontrib/robust_predicate_transfer/scripts/diagnose_rpt.py new file mode 100644 index 00000000000..5d3a1775e45 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/scripts/diagnose_rpt.py @@ -0,0 +1,212 @@ +#!/usr/bin/env python3 +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""Run RPT regressions with decision summaries and an independent watchdog. + +Use an isolated test database with both libraries installed and preloaded. +The runner never terminates a server backend or restarts the cluster. +""" +import argparse +import json +import os +import re +from collections import Counter +from pathlib import Path +import subprocess +import time +import uuid + + +def instrument_sql(source, suite="forward"): + """Keep regression assertions, but identify every dynamic query first.""" + source = source.replace("rpt_transfer_plan(", "pg_temp.rpt_diag_transfer_plan(") + anchor = "DO $$\nDECLARE\n opt text;" + if suite == "forward" and source.count(anchor) != 1: + raise ValueError("forward regression assertion block changed") + helper = """CREATE FUNCTION pg_temp.rpt_diag_transfer_plan(q text) RETURNS text +LANGUAGE plpgsql AS $diag$ +DECLARE + dump text; +BEGIN + RAISE NOTICE 'RPT STEP transfer_plan SQL=%', q; + dump := public.rpt_transfer_plan(q); + RAISE NOTICE 'RPT DUMP BEGIN SQL=%', replace(q, chr(10), ' '); + RAISE NOTICE '%', dump; + RAISE NOTICE 'RPT DUMP END'; + RETURN dump; +END +$diag$; +""" + if suite != "forward": + if "CREATE EXTENSION robust_predicate_transfer;" not in source: + raise ValueError("backward regression assertion block changed") + source = source.replace("CREATE EXTENSION robust_predicate_transfer;", + "CREATE EXTENSION robust_predicate_transfer;\n" + helper, 1) + source = source.replace(" EXECUTE q INTO r;", + " RAISE NOTICE 'RPT STEP results SQL=%', q;\n EXECUTE q INTO r;") + source = source.replace(" EXECUTE 'EXPLAIN (ANALYZE, VERBOSE, FORMAT JSON) ' || q INTO plan;", + " RAISE NOTICE 'RPT STEP explain SQL=%', q;\n" + " EXECUTE 'EXPLAIN (ANALYZE, VERBOSE, FORMAT JSON) ' || q INTO plan;") + return source + source = source.replace(anchor, helper + anchor) + edits = { + " EXECUTE format('EXPLAIN (%s VERBOSE, FORMAT JSON) %s',": + " RAISE NOTICE 'RPT STEP explain analyze=% SQL=%', do_analyze, q;\n", + " EXECUTE format('SELECT string_agg(t::text, %L ORDER BY t::text) FROM (%s) t',": + " RAISE NOTICE 'RPT STEP results rpt=% SQL=%', rpt, q;\n", + " EXECUTE 'PREPARE rpt_fwd_prep AS ' || star;": + " RAISE NOTICE 'RPT STEP prepare optimizer=%', opt;\n", + " EXECUTE 'EXECUTE rpt_fwd_prep' INTO prep_n, prep_s;": + " RAISE NOTICE 'RPT STEP execute prepared optimizer=% run=%', opt, i;\n", + " FOR r IN SELECT f.v FROM rpt_fwd_fact f JOIN rpt_fwd_d1": + " RAISE NOTICE 'RPT STEP SPI dimension scan';\n", + " FOR r IN SELECT f.v FROM rpt_fwd_fact f JOIN rpt_fwd_dl": + " RAISE NOTICE 'RPT STEP SPI local chain';\n", + } + for anchor, marker in edits.items(): + if source.count(anchor) != 1: + raise ValueError(f"forward regression instrumentation anchor changed: {anchor}") + source = source.replace(anchor, marker + anchor) + return source + + +def summarize_dump(dump): + """Summarize only runtime decisions, excluding the legacy survey lines.""" + classes = [] + reasons = Counter() + injected = 0 + cost = 0.0 + for line in dump.splitlines(): + if line.startswith("backward class "): + classes.append(line) + match = re.search(r"est_cost_ms=([0-9.]+)", line) + if match and line.endswith("decision=inject"): + cost += float(match.group(1)) + elif line.startswith("backward consumer ") and " decision=" in line: + reason = line.split(" decision=", 1)[1] + if reason == "inject": + injected += 1 + else: + reasons[reason] += 1 + return {"classes": len(classes), "injected_consumers": injected, + "refused_consumers": dict(reasons), "estimated_cost_ms": cost, + "forward": [line for line in dump.splitlines() + if line.startswith("forward component ")]} + + +def summarize_log(log): + reports = [] + query = None + lines = [] + for line in log.splitlines(): + # psql prefixes the first line of a NOTICE, but not continuation lines. + line = re.sub(r"^.*NOTICE:\s+", "", line) + if line.startswith("RPT DUMP BEGIN SQL="): + query = line.split("SQL=", 1)[1] + lines = [] + elif line == "RPT DUMP END" and query is not None: + reports.append(dict(sql=query, **summarize_dump("\n".join(lines)))) + query = None + elif query is not None: + lines.append(line) + return reports + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--suite", choices=("forward", "backward", "backward_faults"), + default="forward") + parser.add_argument("--database", default="postgres") + parser.add_argument("--output", required=True, type=Path) + parser.add_argument("--psql", default="psql") + parser.add_argument("--wall-timeout", type=float, default=180) + parser.add_argument("--cancel-grace", type=float, default=15) + parser.add_argument("--emit-only", action="store_true") + args = parser.parse_args() + if args.wall_timeout <= 0 or args.cancel_grace < 0: + parser.error("wall-timeout must be positive and cancel-grace nonnegative") + args.output.mkdir(parents=True, exist_ok=True) + source = Path(__file__).resolve().parents[1] / "sql" / ("rpt_" + args.suite + ".sql") + application = "rpt_diagnostic_" + uuid.uuid4().hex + workload = args.output / "workload.sql" + prefix = ("\\set ON_ERROR_STOP on\n\\timing on\n" + f"SET application_name = '{application}';\n" + "SET client_min_messages = notice;\n" + "SELECT pg_backend_pid() AS diagnostic_backend_pid;\n") + workload.write_text(prefix + instrument_sql(source.read_text(), args.suite)) + if args.emit_only: + print(workload) + return 0 + + command = [args.psql, "-X", "-v", "ON_ERROR_STOP=1", "-d", args.database] + env = dict(os.environ) + env["PGCONNECT_TIMEOUT"] = "5" + + def monitor(filename, sql): + try: + result = subprocess.run(command + ["-A", "-t", "-c", sql], + env=env, capture_output=True, text=True, timeout=8) + (args.output / filename).write_text(result.stdout + result.stderr) + return result.returncode + except subprocess.TimeoutExpired as error: + (args.output / filename).write_text(f"monitor timed out: {error.timeout}s\n") + return -1 + + with (args.output / "client.log").open("w") as output: + proc = subprocess.Popen(command + ["-e", "-f", str(workload)], + env=env, stdout=output, stderr=subprocess.STDOUT) + start = time.monotonic() + timed_out = False + try: + proc.wait(timeout=args.wall_timeout) + except subprocess.TimeoutExpired: + timed_out = True + monitor("activity-before-cancel.json", "SELECT coalesce(json_agg(t), '[]'::json) FROM " + "(SELECT pid, application_name, state, wait_event_type, wait_event, " + "query_start, query FROM pg_stat_activity WHERE application_name = " + f"'{application}') t;") + monitor("cancel.txt", "SELECT pid, pg_cancel_backend(pid) FROM pg_stat_activity " + f"WHERE application_name = '{application}';") + try: + proc.wait(timeout=args.cancel_grace) + except subprocess.TimeoutExpired: + monitor("activity-after-cancel.json", "SELECT coalesce(json_agg(t), '[]'::json) FROM " + "(SELECT pid, state, wait_event_type, wait_event, query FROM pg_stat_activity " + f"WHERE application_name = '{application}') t;") + # Stop only this client. Leave a stuck server available for inspection. + proc.terminate() + try: + proc.wait(timeout=5) + except subprocess.TimeoutExpired: + proc.kill() + proc.wait() + result = {"application_name": application, "timed_out": timed_out, + "client_returncode": proc.returncode, + "elapsed_seconds": time.monotonic() - start} + (args.output / "result.json").write_text(json.dumps(result, indent=2) + "\n") + reports = summarize_log((args.output / "client.log").read_text()) + (args.output / "decisions.json").write_text(json.dumps(reports, indent=2) + "\n") + print(f"Saved diagnostic output in {args.output}") + if timed_out: + print("Watchdog expired. Server backends were not terminated; inspect activity and server logs.") + return 2 + return proc.returncode + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/gpcontrib/robust_predicate_transfer/scripts/survey.py b/gpcontrib/robust_predicate_transfer/scripts/survey.py new file mode 100644 index 00000000000..955057e9693 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/scripts/survey.py @@ -0,0 +1,374 @@ +#!/usr/bin/env python3 +#------------------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +"""Plan benchmark queries only; compare RPT eligibility to Anser injection. + +Files may contain multiple SELECT/WITH statements, counted separately. +TPC-H Q15 must use a read-only CTE instead of CREATE/DROP VIEW. +The caller must load schemas/statistics and install/preload both extensions. +No ANALYZE, query execution, schema creation or persistent GUC writes occur. +""" +import argparse +import csv +import json +from pathlib import Path +import re +import subprocess +import uuid + +EXPECTED = {"JOB": 113, "TPC-H": 22, "TPC-DS": 103} +BEGIN_DUMP = "__RPT_SURVEY_DUMP__" +BEGIN_ANSER = "__RPT_SURVEY_ANSER__" +END = "__RPT_SURVEY_END__" + + +def split_select_statements(sql): + """Split instantiated benchmark SQL without splitting quoted semicolons. + + Keep comments in the statement text. Comment-only fragments are ignored. + Reject DDL up front, so standard Q15 gets an actionable normalization error. + This is a lexer, not a SQL parser; the server validates each SELECT later. + """ + statements = [] + start = i = 0 + state = "normal" + depth = 0 + dollar = "" + escape_string = False + first_keyword = None + while i < len(sql): + ch = sql[i] + pair = sql[i:i + 2] + if state == "line": + if ch == "\n": + state = "normal" + elif state == "block": + if pair == "/*": + depth += 1 + i += 1 + elif pair == "*/": + depth -= 1 + i += 1 + if depth == 0: + state = "normal" + elif state == "dollar": + if sql.startswith(dollar, i): + i += len(dollar) - 1 + state = "normal" + elif state in ("single", "double"): + quote = "'" if state == "single" else '"' + if ch == quote: + if i + 1 < len(sql) and sql[i + 1] == quote: + i += 1 + else: + state = "normal" + elif state == "single" and escape_string and ch == "\\": + i += 1 + elif pair == "--": + state = "line" + i += 1 + elif pair == "/*": + state = "block" + depth = 1 + i += 1 + elif ch == ";": + if first_keyword is not None: + statements.append((sql[start:i].strip(), first_keyword)) + start = i + 1 + first_keyword = None + elif not ch.isspace(): + if first_keyword is None: + word = re.match(r"[A-Za-z_]+", sql[i:]) + first_keyword = word.group().upper() if word else "" + if ch == "'": + state = "single" + escape_string = (i > 0 and sql[i - 1] in "eE" and + (i < 2 or not (sql[i - 2].isalnum() or sql[i - 2] == "_"))) + elif ch == '"': + state = "double" + elif ch == "$": + tag = re.match(r"\$(?:[A-Za-z_][A-Za-z0-9_]*)?\$", sql[i:]) + if tag: + dollar = tag.group() + state = "dollar" + i += len(dollar) - 1 + i += 1 + if state not in ("normal", "line"): + raise ValueError("unterminated SQL quote or comment") + if first_keyword is not None: + statements.append((sql[start:].strip(), first_keyword)) + for statement, keyword in statements: + if keyword not in ("SELECT", "WITH"): + raise ValueError("benchmark statements must be SELECT/WITH; use the read-only CTE form of TPC-H Q15 instead of view DDL") + return [statement for statement, keyword in statements] + + +def load_queries(directory): + result = [] + for path in sorted(directory.rglob("*.sql")): + try: + statements = split_select_statements(path.read_text(encoding="utf-8")) + except ValueError as error: + raise ValueError(str(path) + ": " + str(error)) from error + for index, statement in enumerate(statements, 1): + result.append(dict(source_file=str(path.relative_to(directory)), + statement=index, sql=statement)) + return result + + +def sql_literal(value): + return "'" + value.replace("'", "''") + "'" + + +def survey_sql(query, optimizer, timeout_ms, search_path): + tag = "$rpt_" + uuid.uuid4().hex + "$" + query = query.strip().rstrip(";") + return "\n".join([ + "BEGIN READ ONLY;", + "SET LOCAL standard_conforming_strings = on;", + "SET LOCAL statement_timeout = " + str(timeout_ms) + ";", + "SET LOCAL optimizer = " + sql_literal(optimizer) + ";", + "SELECT set_config('search_path', " + sql_literal(search_path) + ", true);", + "SET LOCAL rpt.enable = off;", + "SET LOCAL rpt.backward = on;", + "SET LOCAL anser.enable = on;", + "SET LOCAL anser.runtime_filter = off;", + "SELECT " + sql_literal(BEGIN_DUMP) + ";", + "SELECT rpt_transfer_plan(" + tag + query + tag + ");", + "SELECT " + sql_literal(BEGIN_ANSER) + ";", + "SET LOCAL anser.runtime_filter = on;", + "EXPLAIN (FORMAT JSON) " + query + "\n;", + "SELECT " + sql_literal(END) + ";", + "ROLLBACK;", + ]) + + +def parse_dump(dump): + if dump.strip() == "no transfer regions": + return dict(classes=0, root_classes=0, global_edges=0, rule9=0, + strict=0, eligible=0, known_rows_removed=0.0, + unknown_estimates=0, runtime_survey_available=True, + runtime_classes=0, runtime_root_classes=0, runtime_strict=0, + runtime_eligible=0, runtime_injected_consumers=0, runtime_refusals={}) + lines = [line for line in dump.splitlines() if line.startswith("survey ")] + if len(lines) != 1: + raise ValueError("missing survey metrics; install the analysis/survey build") + metrics = dict(re.findall(r"([a-z0-9_]+)=([^ ]+)", lines[0])) + fields = ("classes", "root_classes", "global_edges", "rule9", "strict", + "eligible", "unknown_estimates") + result = {key: int(metrics[key]) for key in fields} + result["known_rows_removed"] = float(metrics["known_rows_removed"]) + if (any(value < 0 for value in result.values()) or + result["root_classes"] > result["classes"] or + not result["eligible"] <= result["strict"] <= result["rule9"] <= result["global_edges"] or + result["unknown_estimates"] > result["strict"]): + raise ValueError("inconsistent survey metrics") + runtime = [line for line in dump.splitlines() if line.startswith("runtime survey ")] + result["runtime_survey_available"] = bool(runtime) + if runtime: + if len(runtime) != 1: + raise ValueError("duplicate runtime survey metrics") + fields = dict(re.findall(r"([a-z_]+)=([0-9]+)", runtime[0])) + for key in ("classes", "root_classes", "strict", "eligible"): + result["runtime_" + key] = int(fields[key]) + consumers = [line for line in dump.splitlines() + if line.startswith("backward consumer ") and " decision=" in line] + result["runtime_injected_consumers"] = sum(line.endswith(" decision=inject") for line in consumers) + reasons = {} + for line in consumers: + reason = line.split(" decision=", 1)[1] + if reason != "inject": + reasons[reason] = reasons.get(reason, 0) + 1 + result["runtime_refusals"] = reasons + return result + + +def count_anser_producers(value): + if isinstance(value, dict): + own = int(value.get("Custom Plan Provider") == "Anser Bloom Producer") + return own + sum(count_anser_producers(child) for child in value.values()) + if isinstance(value, list): + return sum(count_anser_producers(child) for child in value) + return 0 + + +def optimizer_used(value): + if isinstance(value, dict): + if "Optimizer" in value: + return str(value["Optimizer"]) + for child in value.values(): + found = optimizer_used(child) + if found: + return found + elif isinstance(value, list): + for child in value: + found = optimizer_used(child) + if found: + return found + return "" + + +def parse_output(output): + before, sep, rest = output.partition(BEGIN_DUMP + "\n") + if not sep: + raise ValueError("missing RPT output marker") + dump, sep, rest = rest.partition(BEGIN_ANSER + "\n") + if not sep: + raise ValueError("missing Anser output marker") + explain, sep, after = rest.partition(END) + if not sep: + raise ValueError("missing completion marker") + plan = json.loads(explain) + metrics = parse_dump(dump) + metrics["anser_joins"] = count_anser_producers(plan) + metrics["anser_optimizer_used"] = optimizer_used(plan) + return metrics, dump, plan + + +def summarize(records, complete): + lines = ["# RPT backward eligibility survey", "", + "Survey complete: " + ("yes" if complete else "no"), "", + "Planning only. Each SELECT statement is counted separately, not each file. " + "No benchmark query was executed. Counts labelled " + "strict precede the per-class fan-out cap; eligible follows it.", "", + "| Suite | Optimizer | Planned/total | RPT strict SELECTs | RPT capped SELECTs | Anser SELECTs |", + "| --- | --- | --- | --- | --- | --- |"] + for suite in EXPECTED: + for optimizer in ("off", "on"): + group = [row for row in records if row["suite"] == suite and + row["optimizer"] == optimizer] + good = [row for row in group if row["status"] == "ok"] + strict = sum(row["strict"] > 0 for row in good) + eligible = sum(row["eligible"] > 0 for row in good) + anser = sum(row["anser_joins"] > 0 for row in good) + lines.append("| %s | %s | %d/%d | %d/%d | %d/%d | %d/%d |" % ( + suite, optimizer, len(good), len(group), strict, len(group), + eligible, len(group), anser, len(group))) + lines += ["", "Failures remain in the denominator. See results.json/CSV " + "and raw artifacts for refusals, errors and optimizer fallback.", "", + "NDV estimates are rough: known_rows_removed sums scan-plan-row " + "estimates across strict edges, so classes can double-count a " + "table. unknown_estimates counts missing statistics; it is not zero benefit.", "", + "The survey is evidence for the plan's go/no-go decision; this " + "script never authorizes runtime injection."] + runtime = [row for row in records if row.get("runtime_survey_available")] + if runtime: + lines += ["", "Largest-root runtime schedule (successful runtime dumps only):", "", + "| Suite | Optimizer | Queries | Root classes | Proven consumers | Selected consumers |", + "| --- | --- | --- | --- | --- | --- |"] + for suite in sorted({row["suite"] for row in runtime}): + for optimizer in ("off", "on"): + group = [row for row in runtime if row["suite"] == suite and row["optimizer"] == optimizer] + lines.append("| %s | %s | %d | %d | %d | %d |" % ( + suite, optimizer, len(group), sum(row["runtime_root_classes"] for row in group), + sum(row["runtime_strict"] for row in group), + sum(row["runtime_injected_consumers"] for row in group))) + lines += ["", "Runtime refusal reasons are retained per query in results.json."] + return "\n".join(lines) + "\n" + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + for flag in ("job", "tpch", "tpcds"): + parser.add_argument("--" + flag, required=True, type=Path, + help="directory containing instantiated SELECT/WITH .sql files") + for flag in ("job", "tpch", "tpcds"): + parser.add_argument("--" + flag + "-dbname", help="override database for this suite") + parser.add_argument("--" + flag + "-search-path", help="override search_path for this suite") + parser.add_argument("--dbname", default="postgres", help="database or libpq service; credentials may use PG* environment") + parser.add_argument("--psql", default="psql") + parser.add_argument("--search-path", default="public") + parser.add_argument("--statement-timeout-ms", type=int, default=30000) + parser.add_argument("--output", required=True, type=Path) + parser.add_argument("--allow-partial", action="store_true", help="pilot only; never marks the blocking survey complete") + args = parser.parse_args() + if args.statement_timeout_ms <= 0: + parser.error("statement timeout must be positive") + suites = {"JOB": args.job, "TPC-H": args.tpch, "TPC-DS": args.tpcds} + connections = {"JOB": args.job_dbname or args.dbname, + "TPC-H": args.tpch_dbname or args.dbname, + "TPC-DS": args.tpcds_dbname or args.dbname} + schemas = {"JOB": args.job_search_path or args.search_path, + "TPC-H": args.tpch_search_path or args.search_path, + "TPC-DS": args.tpcds_search_path or args.search_path} + queries = {} + for name, directory in suites.items(): + if not directory.is_dir(): + parser.error("not a query directory: " + str(directory)) + try: + queries[name] = load_queries(directory) + except (OSError, ValueError) as error: + parser.error(str(error)) + if len(queries[name]) != EXPECTED[name] and not args.allow_partial: + parser.error("%s needs %d SELECT statements, found %d; split TPC-DS variants and use Q15 CTE form; --allow-partial is for pilots only" % + (name, EXPECTED[name], len(queries[name]))) + args.output.mkdir(parents=True, exist_ok=True) + raw = args.output / "raw" + raw.mkdir(exist_ok=True) + records = [] + for suite, statements in queries.items(): + for optimizer in ("off", "on"): + for index, statement in enumerate(statements): + identity = "%s-%s-%03d" % (suite, optimizer, index + 1) + row = dict(suite=suite, optimizer=optimizer, + query=statement["source_file"] + "#" + str(statement["statement"]), + source_file=statement["source_file"], + statement=statement["statement"], status="error") + try: + query = statement["sql"] + result = subprocess.run( + [args.psql, "-X", "-q", "-A", "-t", "--no-password", "-v", "ON_ERROR_STOP=1", "-d", connections[suite]], + input=survey_sql(query, optimizer, args.statement_timeout_ms, schemas[suite]), + text=True, capture_output=True, + timeout=2 * args.statement_timeout_ms / 1000 + 15) + if result.returncode: + # Do not record connection strings or arbitrary stderr, + # which can contain credentials. Raw query plans only. + raise ValueError("psql failed (exit %d); inspect the connection/query on the cluster" % result.returncode) + metrics, dump, explain = parse_output(result.stdout) + row.update(metrics) + row["status"] = "ok" + (raw / (identity + ".rpt.txt")).write_text(dump, encoding="utf-8") + (raw / (identity + ".anser.json")).write_text(json.dumps(explain, indent=2), encoding="utf-8") + except (OSError, ValueError, KeyError, subprocess.TimeoutExpired) as error: + if isinstance(error, subprocess.TimeoutExpired): + row["error"] = "psql timed out" + elif isinstance(error, OSError): + row["error"] = type(error).__name__ + else: + row["error"] = str(error) + records.append(row) + print("%s: %s" % (identity, row["status"]), flush=True) + complete = (not args.allow_partial and + all(len(queries[name]) == count for name, count in EXPECTED.items()) and + all(row["status"] == "ok" for row in records)) + report = dict(survey_complete=complete, expected_statements=EXPECTED, results=records) + (args.output / "results.json").write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8") + fields = sorted({field for row in records for field in row}) + with (args.output / "results.csv").open("w", newline="", encoding="utf-8") as stream: + writer = csv.DictWriter(stream, fieldnames=fields) + writer.writeheader() + writer.writerows(records) + (args.output / "summary.md").write_text(summarize(records, complete), encoding="utf-8") + return int(any(row["status"] != "ok" for row in records)) + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_backward.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_backward.sql new file mode 100644 index 00000000000..64fb1383841 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_backward.sql @@ -0,0 +1,282 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Backward producers run in the main plan above forward consumers. Every +-- result query is dynamic SQL, so GUC changes cannot reuse an earlier plan. +CREATE EXTENSION robust_predicate_transfer; +SET anser.runtime_filter = off; +SET anser.timeout_ms = 600000; +-- This timeout covers the entire assertion DO block, under both optimizers. +SET statement_timeout = '120s'; +SET work_mem = '1MB'; +SET enable_nestloop = off; +SET enable_mergejoin = off; +SET gp_enable_hashjoin_size_heuristic = on; +SET join_collapse_limit = 1; +SET optimizer_join_order = query; +CREATE TABLE rpt_bw_root (k int, s int) DISTRIBUTED BY (k); +CREATE TABLE rpt_bw_member (k int, pad text) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_bw_dim (s int, v int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_bw_bridge (k int, s int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_bw_empty (k int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_bw_local (k int) DISTRIBUTED BY (k); +CREATE TABLE rpt_bw_replica (k int) DISTRIBUTED REPLICATED; +CREATE TABLE rpt_bw_part (k int, pad text) DISTRIBUTED RANDOMLY + PARTITION BY RANGE (k) (START (0) END (2000) EVERY (1000)); +-- The root has more rows but a smaller hash footprint than the wide member. +-- Duplicate root keys also make its backward filter selective without a +-- root restriction that would change the largest-root choice. +INSERT INTO rpt_bw_root SELECT CASE WHEN i % 101 = 0 THEN NULL ELSE i % 10 END, + i % 100 FROM generate_series(1, 10000) i; +-- Incompressible padding keeps the member's hash footprint genuinely wide. +INSERT INTO rpt_bw_member SELECT i, + (SELECT string_agg(md5(i::text || ':' || j::text), '') FROM generate_series(1, 100) j) + FROM generate_series(0, 999) i; +INSERT INTO rpt_bw_dim SELECT i, i FROM generate_series(0, 99) i; +INSERT INTO rpt_bw_bridge SELECT i, i % 100 FROM generate_series(0, 999) i; +INSERT INTO rpt_bw_local SELECT i FROM generate_series(0, 999) i; +INSERT INTO rpt_bw_replica SELECT i FROM generate_series(0, 999) i; +INSERT INTO rpt_bw_part SELECT * FROM rpt_bw_member; +ANALYZE rpt_bw_root; +ANALYZE rpt_bw_member; +ANALYZE rpt_bw_dim; +ANALYZE rpt_bw_bridge; +ANALYZE rpt_bw_empty; +ANALYZE rpt_bw_local; +ANALYZE rpt_bw_replica; +ANALYZE rpt_bw_part; +CREATE FUNCTION pg_temp.rpt_bw_result(q text, enabled boolean, backward boolean) +RETURNS text LANGUAGE plpgsql AS $$ +DECLARE + r text; +BEGIN + PERFORM set_config('rpt.enable', CASE WHEN enabled THEN 'on' ELSE 'off' END, true); + PERFORM set_config('rpt.backward', CASE WHEN backward THEN 'on' ELSE 'off' END, true); + q := format('SELECT coalesce(string_agg(t::text, %L ORDER BY t::text), %L) FROM (%s) t', + ';', '', q); + EXECUTE q INTO r; + PERFORM set_config('rpt.enable', 'on', true); + PERFORM set_config('rpt.backward', 'on', true); + RETURN r; +END +$$; +CREATE FUNCTION pg_temp.rpt_bw_plan(q text) RETURNS jsonb LANGUAGE plpgsql AS $$ +DECLARE + plan jsonb; +BEGIN + EXECUTE 'EXPLAIN (ANALYZE, VERBOSE, FORMAT JSON) ' || q INTO plan; + RETURN plan; +END +$$; +CREATE FUNCTION pg_temp.rpt_bw_nodes(plan jsonb) +RETURNS SETOF jsonb LANGUAGE sql AS $$ + WITH RECURSIVE walk(node, in_init) AS ( + SELECT plan->0->'Plan', false + UNION ALL + SELECT child, in_init OR coalesce(child->>'Parent Relationship', '') = 'InitPlan' + FROM walk CROSS JOIN LATERAL jsonb_array_elements(coalesce(node->'Plans', '[]')) child + ) SELECT node FROM walk WHERE NOT in_init +$$; +CREATE FUNCTION pg_temp.rpt_bw_expect(q text, relation_name text, reason text) +RETURNS void LANGUAGE plpgsql AS $$ +DECLARE + dump text := rpt_transfer_plan(q); + member_rtindex text; +BEGIN + member_rtindex := substring(dump FROM ('vertex ([0-9]+) ' || relation_name || ' rows=')); + IF member_rtindex IS NULL OR NOT EXISTS ( + SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('backward consumer ' || member_rtindex || ' root=%') + AND line LIKE ('% decision=' || reason)) THEN + RAISE EXCEPTION 'missing backward decision % for %, optimizer=%: %', + reason, relation_name, current_setting('optimizer'), dump; + END IF; +END +$$; +DO $$ +DECLARE + opt text; + q text; + dump text; + plan jsonb; + baseline text; + answer text; + cursor_name refcursor; + first_row record; + plain constant text := 'SELECT r.k, m.pad FROM rpt_bw_member m JOIN rpt_bw_root r ON m.k = r.k'; + chained constant text := 'SELECT r.k, m.pad FROM rpt_bw_member m JOIN + (rpt_bw_root r JOIN rpt_bw_dim d ON r.s = d.s) ON m.k = r.k WHERE d.v < 5'; +BEGIN + PERFORM set_config('rpt.enable', 'on', true); + PERFORM set_config('rpt.forward_min_root_rows', '0', true); + PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + PERFORM set_config('rpt.forward', 'off', true); + PERFORM pg_temp.rpt_bw_expect(plain, 'm', 'inject'); + -- Forward skipping must not suppress an unfiltered root's broadcast. + -- Redundant safe predicates must not be required to recover its edges. + q := plain || ' WHERE m.k >= 0 AND r.s >= 0'; + PERFORM pg_temp.rpt_bw_expect(q, 'm', 'inject'); + IF pg_temp.rpt_bw_result(plain, true, true) IS DISTINCT FROM + pg_temp.rpt_bw_result(q, true, true) THEN + RAISE EXCEPTION 'redundant qualifiers changed backward results'; + END IF; + plan := pg_temp.rpt_bw_plan(plain); + IF NOT EXISTS (SELECT FROM pg_temp.rpt_bw_nodes(plan) n + WHERE n->>'Custom Plan Provider' = 'Anser Bloom Producer') OR + NOT EXISTS (SELECT FROM pg_temp.rpt_bw_nodes(plan) n + WHERE n->>'Custom Plan Provider' = 'Anser Bloom Consumer') OR + plan::text LIKE '%InitPlan%' THEN + RAISE EXCEPTION 'backward-only shape wrong (optimizer=%): %', opt, plan; + END IF; + IF (SELECT coalesce(sum(coalesce((n->>'Rows Removed by Bloom Filter')::float8, 0) + + coalesce((n->>'Rows Removed by Pushdown Runtime Filter')::float8, 0)), 0) + FROM pg_temp.rpt_bw_nodes(plan) n) <= 0 THEN + RAISE EXCEPTION 'backward consumer removed no rows (optimizer=%): %', opt, plan; + END IF; + -- The root producer must directly wrap the forward consumer stack. + PERFORM set_config('rpt.forward', 'on', true); + PERFORM pg_temp.rpt_bw_expect(chained, 'm', 'inject'); + plan := pg_temp.rpt_bw_plan(chained); + IF plan::text NOT LIKE '%InitPlan%' OR NOT EXISTS ( + SELECT FROM pg_temp.rpt_bw_nodes(plan) n + WHERE n->>'Custom Plan Provider' = 'Anser Bloom Producer' + AND n->'Plans'->0->>'Custom Plan Provider' = 'Anser Bloom Consumer') THEN + RAISE EXCEPTION 'backward producer is not above forward consumer (optimizer=%): %', opt, plan; + END IF; + -- Refusing forward must not suppress a useful backward filter. + PERFORM set_config('rpt.forward_min_root_rows', '1e12', true); + PERFORM pg_temp.rpt_bw_expect(chained, 'm', 'inject'); + IF rpt_transfer_plan(chained) NOT LIKE '%decision=root rows below rpt.forward_min_root_rows%' THEN + RAISE EXCEPTION 'forward gate fixture did not actually refuse'; + END IF; + PERFORM set_config('rpt.forward_min_root_rows', '0', true); + -- Runtime gates, with the same fixture known to inject above. + PERFORM set_config('rpt.forward_max_selectivity', '0', true); + PERFORM pg_temp.rpt_bw_expect(plain, 'm', + 'estimated member selectivity not below rpt.forward_max_selectivity'); + PERFORM set_config('rpt.forward_max_selectivity', '0.35', true); + PERFORM set_config('rpt.forward_max_exchange_ms', '0', true); + PERFORM pg_temp.rpt_bw_expect(plain, 'm', + 'estimated exchange cost above rpt.forward_max_exchange_ms'); + PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); + PERFORM pg_temp.rpt_bw_expect( + 'SELECT * FROM rpt_bw_root r JOIN rpt_bw_local l ON r.k = l.k', + 'l', 'local edge uses core runtime filtering'); + -- Narrow projection makes the smaller member the hash build side. + PERFORM pg_temp.rpt_bw_expect( + 'SELECT count(*) FROM rpt_bw_root r JOIN rpt_bw_member m ON r.k = m.k', + 'm', 'producer is not on a qualifying hash build side'); + -- LEFT's preserved root cannot be filtered by its nullable member. + -- Its member has no unprotected equivalence class for broadcasting. + PERFORM pg_temp.rpt_bw_expect( + 'SELECT r.k, m.pad FROM rpt_bw_root r LEFT JOIN rpt_bw_member m ON r.k = m.k', + 'm', 'edge has no unprotected equivalence class'); + dump := rpt_transfer_plan('SELECT * FROM rpt_bw_root r JOIN rpt_bw_bridge b + ON r.k = b.k JOIN rpt_bw_dim d ON b.s = d.s WHERE d.v < 5'); + IF dump NOT LIKE '%backward class %decision=bridge-led class is deferred%' THEN + RAISE EXCEPTION 'bridge-led backward class was not deferred (optimizer=%): %', opt, dump; + END IF; + -- Both joins consume the narrow root subtree on their build side. + -- Cross-relation join filters keep wide member columns in each join's + -- input, while only r.k survives into the next hash build. + PERFORM set_config('rpt.max_backward_consumers', '1', true); + dump := rpt_transfer_plan('SELECT count(*) FROM + (rpt_bw_member m1 JOIN rpt_bw_root r ON m1.k = r.k AND length(m1.pad) > r.k) + JOIN rpt_bw_member m2 ON m2.k = r.k AND length(m2.pad) > r.k'); + IF (SELECT count(*) FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE 'backward consumer % root=% decision=inject') <> 1 OR + dump NOT LIKE '%root=%decision=backward consumer cap exceeded%' THEN + RAISE EXCEPTION 'runtime backward fan-out cap not exercised (optimizer=%): %', opt, dump; + END IF; + PERFORM set_config('rpt.max_backward_consumers', '4', true); + -- Every runtime proof refusal is retained verbatim in the new dump; + -- real-plan proof cases also run in rpt_backward_eligibility. + dump := rpt_transfer_plan('SELECT r.k, (SELECT count(*) FROM rpt_bw_member m + JOIN rpt_bw_dim d ON m.k = d.s WHERE m.k < r.k AND random() < 1 OFFSET 0) + FROM rpt_bw_root r'); + IF dump NOT LIKE '%decision=scan inside a SubPlan%' THEN + RAISE EXCEPTION 'correlated producer was not refused (optimizer=%): %', opt, dump; + END IF; + FOREACH q IN ARRAY ARRAY[ + plain, chained, + 'SELECT count(*), sum(r.k) FROM rpt_bw_root r LEFT JOIN rpt_bw_member m ON r.k = m.k', + 'SELECT count(*), sum(r.k) FROM rpt_bw_member m RIGHT JOIN rpt_bw_root r ON r.k = m.k', + 'SELECT count(*), sum(r.k) FROM rpt_bw_root r FULL JOIN rpt_bw_member m ON r.k = m.k', + 'SELECT count(*), sum(r.k) FROM rpt_bw_root r WHERE EXISTS (SELECT FROM rpt_bw_member m WHERE m.k = r.k)', + 'SELECT count(*), sum(r.k) FROM rpt_bw_root r WHERE NOT EXISTS (SELECT FROM rpt_bw_member m WHERE m.k = r.k)', + 'SELECT count(*) FROM rpt_bw_root r JOIN rpt_bw_empty e ON r.k = e.k', + 'SELECT count(*) FROM rpt_bw_empty e JOIN rpt_bw_member m ON e.k = m.k', + 'SELECT count(*) FROM rpt_bw_root r JOIN rpt_bw_replica p ON r.k = p.k', + 'SELECT count(*) FROM rpt_bw_replica p JOIN rpt_bw_member m ON p.k = m.k', + 'SELECT count(*) FROM rpt_bw_root r JOIN rpt_bw_part p ON r.k = p.k', + 'SELECT r.k, m.pad FROM rpt_bw_member m JOIN rpt_bw_root r ON m.k = r.k LIMIT 1', + 'SELECT r.k, m.pad FROM rpt_bw_member m JOIN rpt_bw_root r ON m.k = r.k LIMIT 0' + ] LOOP + -- LIMIT 1 has no SQL ordering guarantee; compare a count instead. + IF q LIKE '%LIMIT 1' THEN q := 'SELECT count(*) FROM (' || q || ') limited'; END IF; + baseline := pg_temp.rpt_bw_result(q, false, false); + IF pg_temp.rpt_bw_result(q, true, false) IS DISTINCT FROM baseline OR + pg_temp.rpt_bw_result(q, true, true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward result differs (optimizer=%): %', opt, q; + END IF; + END LOOP; + q := 'SELECT count(*)::text || ''/'' || sum(length(m.pad || r.k::text))::text + FROM rpt_bw_member m JOIN rpt_bw_root r ON m.k = r.k'; + PERFORM pg_temp.rpt_bw_expect(q, 'm', 'inject'); + baseline := pg_temp.rpt_bw_result(q, false, false); + EXECUTE 'PREPARE rpt_bw_prepared AS ' || q; + FOR i IN 1..2 LOOP + EXECUTE 'EXECUTE rpt_bw_prepared' INTO answer; + IF '(' || answer || ')' IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward prepared answer differs'; + END IF; + END LOOP; + EXECUTE 'DEALLOCATE rpt_bw_prepared'; + cursor_name := 'rpt_bw_cursor'; + OPEN cursor_name FOR EXECUTE plain; + FETCH cursor_name INTO first_row; + IF NOT FOUND THEN RAISE EXCEPTION 'backward cursor returned no row'; END IF; + CLOSE cursor_name; + -- A new command must answer after the cursor squelches its producer. + IF pg_temp.rpt_bw_result(q, true, true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'next command after backward cursor'; + END IF; + PERFORM set_config('anser.runtime_filter', 'on', true); + dump := rpt_transfer_plan(chained); + IF dump LIKE '%key=rpt_b_%' OR dump NOT LIKE '%needs anser.enable=on and anser.runtime_filter=off%' THEN + RAISE EXCEPTION 'backward did not stand down for Anser: %', dump; + END IF; + PERFORM set_config('anser.runtime_filter', 'off', true); + END LOOP; +END +$$; +DROP TABLE rpt_bw_root, rpt_bw_member, rpt_bw_dim, rpt_bw_bridge, rpt_bw_empty, + rpt_bw_local, rpt_bw_replica, rpt_bw_part; +DROP EXTENSION robust_predicate_transfer; +RESET anser.runtime_filter; +RESET anser.timeout_ms; +RESET statement_timeout; +RESET enable_nestloop; +RESET enable_mergejoin; +RESET gp_enable_hashjoin_size_heuristic; +RESET work_mem; +RESET join_collapse_limit; +RESET optimizer_join_order; diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_backward_eligibility.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_backward_eligibility.sql new file mode 100644 index 00000000000..b904b96cdad --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_backward_eligibility.sql @@ -0,0 +1,228 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Production walker/resolver coverage. Both optimizer settings run these +-- queries; every assertion resolves vertex IDs from relation names. +CREATE EXTENSION robust_predicate_transfer; +SET rpt.enable = off; +SET anser.runtime_filter = off; +SET enable_nestloop = off; +SET enable_mergejoin = off; +SET join_collapse_limit = 1; +SET optimizer_join_order = query; +SET statement_timeout = '30s'; +CREATE TABLE rpt_elig_root (pad int, k int, v int) DISTRIBUTED BY (k); +CREATE TABLE rpt_elig_member (k int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_elig_local (k int) DISTRIBUTED BY (k); +CREATE TABLE rpt_elig_replica (k int) DISTRIBUTED REPLICATED; +CREATE TABLE rpt_elig_outer (k int) DISTRIBUTED BY (k); +CREATE TABLE rpt_elig_bridge (k1 int, k2 int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_elig_member2 (k int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_elig_member3 (k int) DISTRIBUTED RANDOMLY; +INSERT INTO rpt_elig_root SELECT 0, i, i FROM generate_series(1, 10000) i; +INSERT INTO rpt_elig_member SELECT i FROM generate_series(1, 10000) i; +INSERT INTO rpt_elig_local SELECT i FROM generate_series(1, 10000) i; +INSERT INTO rpt_elig_replica SELECT i FROM generate_series(1, 10000) i; +INSERT INTO rpt_elig_outer SELECT i FROM generate_series(1, 10) i; +INSERT INTO rpt_elig_bridge SELECT i, i FROM generate_series(1, 1000) i; +INSERT INTO rpt_elig_member2 SELECT i FROM generate_series(1, 10000) i; +INSERT INTO rpt_elig_member3 SELECT i FROM generate_series(1, 10000) i; +ANALYZE rpt_elig_root; +ANALYZE rpt_elig_member; +ANALYZE rpt_elig_local; +ANALYZE rpt_elig_replica; +ANALYZE rpt_elig_outer; +ANALYZE rpt_elig_bridge; +ANALYZE rpt_elig_member2; +ANALYZE rpt_elig_member3; +CREATE FUNCTION pg_temp.rpt_expect(q text, producer text, consumer text, + reasons text[], accepted boolean DEFAULT NULL) +RETURNS text LANGUAGE plpgsql AS $$ +DECLARE + dump text := rpt_transfer_plan(q); + producer_rtindex text; + consumer_rtindex text; + decision text; +BEGIN + producer_rtindex := substring(dump FROM ('vertex ([0-9]+) ' || producer || ' rows=')); + consumer_rtindex := substring(dump FROM ('vertex ([0-9]+) ' || consumer || ' rows=')); + IF producer_rtindex IS NULL OR consumer_rtindex IS NULL THEN + RAISE EXCEPTION 'missing fixture scans % / %: %', producer, consumer, dump; + END IF; + SELECT line INTO decision FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('backward consumer ' || consumer_rtindex || ' producer ' || producer_rtindex || ' %') + AND substring(line FROM ' reason=(.*)$') = ANY(reasons) + AND (accepted IS NULL OR line LIKE ('% eligible=' || CASE WHEN accepted THEN 'yes' ELSE 'no' END || ' %')) + LIMIT 1; + IF decision IS NULL THEN + RAISE EXCEPTION 'expected % for % -> %, optimizer=%: %', reasons, producer, consumer, current_setting('optimizer'), dump; + END IF; + RETURN dump; +END +$$; +CREATE FUNCTION pg_temp.rpt_explain(q text) RETURNS jsonb LANGUAGE plpgsql AS $$ +DECLARE + plan jsonb; +BEGIN + EXECUTE 'EXPLAIN (FORMAT JSON) ' || q INTO plan; + RETURN plan; +END +$$; +CREATE FUNCTION pg_temp.rpt_scan_path(q text, relation_name text) RETURNS text[] LANGUAGE SQL AS $$ + WITH RECURSIVE walk(node, path) AS ( + SELECT pg_temp.rpt_explain(q)->0->'Plan', ARRAY[]::text[] + UNION ALL + SELECT child, path || ((node->>'Node Type') || ':' || + COALESCE(node->>'Join Type', '') || ':' || COALESCE(child->>'Parent Relationship', '')) + FROM walk CROSS JOIN LATERAL jsonb_array_elements(COALESCE(node->'Plans', '[]'::jsonb)) AS children(child) + ) + SELECT path FROM walk WHERE node->>'Relation Name' = relation_name LIMIT 1 +$$; +DO $$ +DECLARE + opt text; + q text; + dump text; + path text[]; + producer_path text[]; + split_at integer; + producer_rtindex text; + correlated text; +BEGIN + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + -- Expressions force the real walker through targetlists and quals. + -- k is physical column 2; the resolver must use its output position. + PERFORM pg_temp.rpt_expect( + 'SELECT rpt_elig_root.k + rpt_elig_member.k, rpt_elig_root.v FROM rpt_elig_root JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < 100 AND rpt_elig_root.v % 2 = 0', + 'rpt_elig_root', 'rpt_elig_member', ARRAY['eligible'], true); + PERFORM pg_temp.rpt_expect( + 'SELECT * FROM rpt_elig_root JOIN rpt_elig_local USING (k) WHERE rpt_elig_root.v < 100', + 'rpt_elig_root', 'rpt_elig_local', ARRAY['local edge uses core runtime filtering'], false); + PERFORM pg_temp.rpt_expect( + 'SELECT * FROM rpt_elig_root JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < 100 AND rpt_elig_member.k > 0', + 'rpt_elig_member', 'rpt_elig_root', ARRAY['producer is not on a qualifying hash build side'], false); + PERFORM pg_temp.rpt_expect( + 'SELECT * FROM rpt_elig_root JOIN rpt_elig_replica USING (k) WHERE rpt_elig_root.v < 100', + 'rpt_elig_root', 'rpt_elig_replica', ARRAY['same or unknown slice'], false); + PERFORM pg_temp.rpt_expect( + 'SELECT * FROM rpt_elig_root LEFT JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < 100', + 'rpt_elig_root', 'rpt_elig_member', ARRAY['edge has no unprotected equivalence class'], false); + PERFORM pg_temp.rpt_expect( + 'SELECT * FROM rpt_elig_root JOIN rpt_elig_bridge ON rpt_elig_root.k = rpt_elig_bridge.k1 JOIN rpt_elig_member ON rpt_elig_bridge.k2 = rpt_elig_member.k WHERE rpt_elig_root.v < 9000', + 'rpt_elig_bridge', 'rpt_elig_member', ARRAY['bridge-led class is deferred'], false); + -- ORDER BY can be discarded by ORCA. Assert the refusal only when + -- the Sort remains below the qualifying hash join; otherwise require + -- the unblocked query to qualify rather than silently skipping it. + q := 'SELECT * FROM (SELECT * FROM rpt_elig_member ORDER BY k) rpt_elig_member JOIN rpt_elig_root USING (k) WHERE rpt_elig_root.v < 100'; + path := pg_temp.rpt_scan_path(q, 'rpt_elig_member'); + IF path IS NULL THEN + RAISE EXCEPTION 'sort fixture lost its member scan'; + END IF; + IF EXISTS (SELECT FROM unnest(path) WITH ORDINALITY h(tag, n), unnest(path) WITH ORDINALITY s(tag, n) + WHERE h.tag LIKE 'Hash Join:%:Outer' AND s.tag LIKE 'Sort:%' AND h.n < s.n) THEN + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', + ARRAY['sort between consumer and join', 'unsupported consumer key path'], false); + ELSE + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['eligible'], true); + END IF; + -- A strict upper join may simplify the LEFT join to INNER. If it + -- survives, the nullable path must be refused by the production proof. + q := 'SELECT * FROM (rpt_elig_outer LEFT JOIN rpt_elig_member USING (k)) JOIN rpt_elig_root ON rpt_elig_root.k = rpt_elig_member.k WHERE rpt_elig_root.v < 100'; + path := pg_temp.rpt_scan_path(q, 'rpt_elig_member'); + IF path IS NULL THEN + RAISE EXCEPTION 'nullable-side fixture lost its member scan'; + END IF; + IF EXISTS (SELECT FROM unnest(path) tag WHERE tag LIKE '%:Left:Inner' OR tag LIKE '%:Right:Outer') THEN + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['nullable side of outer join'], false); + ELSE + -- Simplification does not fix hash build/probe order. Compare + -- the first differing path step: the shared parent is the join + -- separating these named scans, regardless of range-table IDs. + producer_path := pg_temp.rpt_scan_path(q, 'rpt_elig_root'); + IF producer_path IS NULL THEN + RAISE EXCEPTION 'nullable-side fixture lost its root scan'; + END IF; + SELECT min(n) INTO split_at FROM generate_subscripts(path, 1) n + WHERE path[n] IS DISTINCT FROM producer_path[n]; + IF split_at IS NULL THEN + RAISE EXCEPTION 'nullable-side fixture has no separating join'; + END IF; + IF producer_path[split_at] LIKE 'Hash Join:%:Outer' + AND path[split_at] LIKE 'Hash Join:%:Inner' THEN + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', + ARRAY['producer is not on a qualifying hash build side'], false); + ELSE + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['eligible'], true); + END IF; + END IF; + -- An InitPlan is evaluated once, and its presence alone does not + -- imply a nonempty extParam on the qualifying hash build. Depending + -- on planner placement it can be internal to that build or external. + -- The native test checks an explicit extParam bitmap; the correlated + -- SubPlan below exercises the rescan refusal on a real SQL plan. + q := 'SELECT * FROM rpt_elig_root JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < (SELECT max(k) FROM rpt_elig_outer)'; + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', + ARRAY['eligible', 'parameterized producer can be rescanned', + 'unsupported producer key path', 'producer is not on a qualifying hash build side']); + -- Volatility and OFFSET retain a correlated SubPlan instead of + -- allowing a decorrelating join rewrite. Its internal Motion must + -- not undo the explicit SubPlan refusal. + correlated := 'SELECT rpt_elig_outer.k, (SELECT count(*) FROM rpt_elig_root JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < rpt_elig_outer.k AND random() < 0.9 OFFSET 0) FROM rpt_elig_outer'; + IF pg_temp.rpt_explain(correlated)::text NOT LIKE '%SubPlan%' THEN + RAISE EXCEPTION 'correlated fixture no longer contains a SubPlan (optimizer=%)', opt; + END IF; + path := pg_temp.rpt_scan_path(correlated, 'rpt_elig_root'); + IF path IS NULL OR NOT EXISTS ( + SELECT FROM unnest(path) WITH ORDINALITY sp(tag, n), unnest(path) WITH ORDINALITY motion(tag, n) + WHERE sp.tag LIKE '%:SubPlan' AND motion.tag LIKE '%Motion:%' AND sp.n < motion.n) THEN + RAISE EXCEPTION 'correlated fixture lost the Motion inside its SubPlan (optimizer=%)', opt; + END IF; + dump := pg_temp.rpt_expect(correlated, 'rpt_elig_root', 'rpt_elig_member', ARRAY['scan inside a SubPlan'], false); + IF EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE 'backward consumer %' AND line LIKE '% eligible=yes %') THEN + RAISE EXCEPTION 'SubPlan produced an eligible consumer: %', dump; + END IF; + PERFORM set_config('rpt.max_backward_consumers', '1', true); + dump := rpt_transfer_plan('SELECT * FROM rpt_elig_root JOIN rpt_elig_member ON rpt_elig_root.k = rpt_elig_member.k JOIN rpt_elig_member2 ON rpt_elig_root.k = rpt_elig_member2.k JOIN rpt_elig_member3 ON rpt_elig_root.k = rpt_elig_member3.k WHERE rpt_elig_root.v < 100'); + producer_rtindex := substring(dump FROM 'vertex ([0-9]+) rpt_elig_root rows='); + IF producer_rtindex IS NULL OR + NOT EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('backward consumer % producer ' || producer_rtindex || ' %') + AND line LIKE '% reason=backward consumer cap exceeded') OR + (SELECT count(*) FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('backward consumer % producer ' || producer_rtindex || ' %') + AND line LIKE '% eligible=yes %') <> 1 THEN + RAISE EXCEPTION 'fan-out cap fixture did not retain exactly one consumer (optimizer=%): %', opt, dump; + END IF; + PERFORM set_config('rpt.max_backward_consumers', '4', true); + END LOOP; +END +$$; +DROP TABLE rpt_elig_root, rpt_elig_member, rpt_elig_local, rpt_elig_replica, + rpt_elig_outer, rpt_elig_bridge, rpt_elig_member2, rpt_elig_member3; +DROP EXTENSION robust_predicate_transfer; +RESET optimizer; +RESET rpt.enable; +RESET anser.runtime_filter; +RESET enable_nestloop; +RESET enable_mergejoin; +RESET join_collapse_limit; +RESET optimizer_join_order; +RESET statement_timeout; diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_backward_faults.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_backward_faults.sql new file mode 100644 index 00000000000..8a07325c628 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_backward_faults.sql @@ -0,0 +1,237 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Backward-only transport failures. Every armed fault must actually fire. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS gp_inject_fault; +CREATE EXTENSION IF NOT EXISTS anser_test; +-- end_ignore +CREATE EXTENSION robust_predicate_transfer; +SET anser.runtime_filter = off; +SET rpt.enable = on; +SET rpt.forward = off; +SET rpt.backward = on; +SET rpt.forward_max_exchange_ms = -1; +SET statement_timeout = '120s'; +SET work_mem = '1MB'; +SET enable_nestloop = off; +SET enable_mergejoin = off; +SET gp_enable_hashjoin_size_heuristic = on; +CREATE TABLE rpt_bwf_root (k int) DISTRIBUTED BY (k); +CREATE TABLE rpt_bwf_member (k int, pad text) DISTRIBUTED RANDOMLY; +INSERT INTO rpt_bwf_root SELECT i % 10 FROM generate_series(1, 10000) i; +INSERT INTO rpt_bwf_member SELECT i, + (SELECT string_agg(md5(i::text || ':' || j::text), '') FROM generate_series(1, 100) j) + FROM generate_series(0, 999) i; +ANALYZE rpt_bwf_root; +ANALYZE rpt_bwf_member; +CREATE FUNCTION pg_temp.rpt_bwf_query() RETURNS text LANGUAGE sql AS $$ + SELECT 'SELECT count(*)::text || ''/'' || sum(length(m.pad || r.k::text))::text + FROM rpt_bwf_member m JOIN rpt_bwf_root r ON m.k = r.k' +$$; +CREATE FUNCTION pg_temp.rpt_bwf_run(enabled boolean) RETURNS text LANGUAGE plpgsql AS $$ +DECLARE + q text := pg_temp.rpt_bwf_query(); + r text; +BEGIN + PERFORM set_config('rpt.enable', CASE WHEN enabled THEN 'on' ELSE 'off' END, true); + EXECUTE q INTO r; + PERFORM set_config('rpt.enable', 'on', true); + RETURN r; +END +$$; +-- How often a fault fired, so a case cannot pass without exercising it. +CREATE FUNCTION pg_temp.rpt_bwf_hits(fault text, coordinator boolean) +RETURNS int LANGUAGE sql AS $$ + SELECT coalesce(sum(substring(gp_inject_fault(fault, 'status', dbid) + FROM 'num times hit:''([0-9]+)''')::int), 0)::int + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator +$$; +-- First occurrence only; on the coordinator a 'sleep' fault sleeps 1 s. +CREATE FUNCTION pg_temp.rpt_bwf_fault(fault text, action text, coordinator boolean) +RETURNS void LANGUAGE plpgsql AS $$ +BEGIN + IF action = 'reset' THEN + PERFORM gp_inject_fault(fault, 'reset', dbid) + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator; + ELSE + PERFORM gp_inject_fault(fault, action, '', '', '', 1, 1, + CASE WHEN coordinator THEN 1 ELSE 0 END, dbid) + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator; + END IF; +END +$$; +DO $$ +DECLARE + opt text; + baseline text; + answer text; + plan jsonb; + deliveries_before bigint; +BEGIN + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + baseline := pg_temp.rpt_bwf_run(false); + IF rpt_transfer_plan(pg_temp.rpt_bwf_query()) NOT LIKE '%backward class %decision=inject%' THEN + RAISE EXCEPTION 'backward fault fixture not selected (optimizer=%)', opt; + END IF; + PERFORM set_config('anser.timeout_ms', '100', true); + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'sleep', true); + answer := pg_temp.rpt_bwf_run(true); + IF pg_temp.rpt_bwf_hits('anser_before_deliver', true) = 0 THEN + RAISE EXCEPTION 'backward delivery sleep never fired'; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'reset', true); + IF answer IS DISTINCT FROM baseline OR pg_temp.rpt_bwf_run(true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward timeout or next command changed results'; + END IF; + PERFORM set_config('anser.timeout_ms', '1000', true); + PERFORM set_config('anser.debug_delivery_chunk_bytes', '8192', true); + PERFORM set_config('anser.debug_min_filter_bytes', '8388608', true); + PERFORM pg_temp.rpt_bwf_fault('anser_after_chunk', 'skip', false); + answer := pg_temp.rpt_bwf_run(true); + IF pg_temp.rpt_bwf_hits('anser_after_chunk', false) = 0 THEN + RAISE EXCEPTION 'backward consumer walk-away never fired'; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_after_chunk', 'reset', false); + IF answer IS DISTINCT FROM baseline OR pg_temp.rpt_bwf_run(true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward walk-away or next command changed results'; + END IF; + -- Plan with an 8 MiB filter, then lower the runtime payload cap. + -- EXPLAIN EXECUTE forces the generic plan before the cap changes; + -- the second EXPLAIN proves the cached producer was not resized away. + EXECUTE 'PREPARE rpt_bwf_oversized AS ' || pg_temp.rpt_bwf_query(); + EXECUTE 'EXPLAIN (FORMAT JSON) EXECUTE rpt_bwf_oversized' INTO plan; + IF plan::text NOT LIKE '%8388608%' THEN + RAISE EXCEPTION 'oversized producer fixture was not planned: %', plan; + END IF; + PERFORM set_config('anser.max_info_size', '2097152', true); + EXECUTE 'EXPLAIN (FORMAT JSON) EXECUTE rpt_bwf_oversized' INTO plan; + IF plan::text NOT LIKE '%8388608%' THEN + RAISE EXCEPTION 'oversized producer fixture was replanned: %', plan; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'skip', true); + deliveries_before := anser_test_deliveries_completed(); + EXECUTE 'EXECUTE rpt_bwf_oversized' INTO answer; + -- The fault proves delivery was attempted; only the counter can + -- distinguish a cancellation from a successfully delivered filter. + IF anser_test_deliveries_completed() <> deliveries_before THEN + RAISE EXCEPTION 'oversized producer delivered a filter instead of cancelling'; + END IF; + IF pg_temp.rpt_bwf_hits('anser_before_deliver', true) = 0 THEN + RAISE EXCEPTION 'oversized producer never delivered its cancellation'; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'reset', true); + EXECUTE 'DEALLOCATE rpt_bwf_oversized'; + PERFORM set_config('anser.max_info_size', '68157440', true); + PERFORM set_config('anser.debug_min_filter_bytes', '0', true); + PERFORM set_config('anser.debug_delivery_chunk_bytes', '0', true); + IF answer IS DISTINCT FROM baseline OR pg_temp.rpt_bwf_run(true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward producer cancellation changed results'; + END IF; + END LOOP; +END +$$; +CREATE FUNCTION pg_temp.rpt_bwf_cancel() RETURNS void LANGUAGE plpgsql AS $$ +DECLARE + cancelled boolean := false; + answer text; +BEGIN + BEGIN + EXECUTE 'EXECUTE rpt_bwf_cancel_plan' INTO answer; + EXCEPTION WHEN query_canceled THEN + cancelled := true; + END; + IF NOT cancelled THEN + RAISE EXCEPTION 'backward wait was not cancelled (optimizer=%, delivery hits=%)', + current_setting('optimizer'), pg_temp.rpt_bwf_hits('anser_before_deliver', true); + END IF; +END +$$; +-- Prepare and verify each optimizer's plan before starting the short timer. +-- The cancellation check then measures executor/transport cleanup, not planning. +CREATE FUNCTION pg_temp.rpt_bwf_prepare_cancel() RETURNS void LANGUAGE plpgsql AS $$ +DECLARE + plan jsonb; +BEGIN + IF rpt_transfer_plan(pg_temp.rpt_bwf_query()) NOT LIKE '%backward class %decision=inject%' THEN + RAISE EXCEPTION 'cancel fixture has no backward injection (optimizer=%)', current_setting('optimizer'); + END IF; + EXECUTE 'PREPARE rpt_bwf_cancel_plan AS ' || pg_temp.rpt_bwf_query(); + EXECUTE 'EXPLAIN (VERBOSE, FORMAT JSON) EXECUTE rpt_bwf_cancel_plan' INTO plan; + IF plan::text NOT LIKE '%Anser Bloom Producer%' OR plan::text NOT LIKE '%Anser Bloom Consumer%' THEN + RAISE EXCEPTION 'cancel fixture lost its runtime nodes: %', plan; + END IF; +END +$$; +-- Separate top-level commands arm statement_timeout for each optimizer. +SET optimizer = off; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_prepare_cancel(); END $$; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'sleep', true); END $$; +SET anser.timeout_ms = 600000; +SET statement_timeout = '300ms'; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_cancel(); END $$; +SET statement_timeout = '120s'; +DO $$ +BEGIN + IF pg_temp.rpt_bwf_hits('anser_before_deliver', true) = 0 THEN + RAISE EXCEPTION 'cancel test never reached backward delivery'; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'reset', true); + EXECUTE 'DEALLOCATE rpt_bwf_cancel_plan'; + IF pg_temp.rpt_bwf_run(true) IS DISTINCT FROM pg_temp.rpt_bwf_run(false) THEN + RAISE EXCEPTION 'session failed after backward cancellation'; + END IF; +END +$$; +SET optimizer = on; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_prepare_cancel(); END $$; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'sleep', true); END $$; +SET statement_timeout = '300ms'; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_cancel(); END $$; +SET statement_timeout = '120s'; +DO $$ +BEGIN + IF pg_temp.rpt_bwf_hits('anser_before_deliver', true) = 0 THEN + RAISE EXCEPTION 'ORCA cancel test never reached backward delivery'; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'reset', true); + EXECUTE 'DEALLOCATE rpt_bwf_cancel_plan'; + IF pg_temp.rpt_bwf_run(true) IS DISTINCT FROM pg_temp.rpt_bwf_run(false) THEN + RAISE EXCEPTION 'ORCA session failed after backward cancellation'; + END IF; +END +$$; +DROP TABLE rpt_bwf_root, rpt_bwf_member; +DROP EXTENSION robust_predicate_transfer; +RESET rpt.enable; +RESET rpt.forward; +RESET rpt.backward; +RESET rpt.forward_max_exchange_ms; +RESET anser.runtime_filter; +RESET anser.timeout_ms; +RESET optimizer; +RESET statement_timeout; +RESET enable_nestloop; +RESET enable_mergejoin; +RESET gp_enable_hashjoin_size_heuristic; +RESET work_mem; diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql new file mode 100644 index 00000000000..f57fb56709a --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql @@ -0,0 +1,349 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Forward reducer, end to end: the InitPlan is built, its filters +-- reach the main plan across the dispatch boundary, intermediate hops work +-- both through Anser and in-process, and results never change. Both +-- optimizers; every check raises with the plan or dump that failed it. +-- Faults live in rpt_forward_faults. +CREATE EXTENSION robust_predicate_transfer; +SET anser.runtime_filter = off; +SET rpt.backward = off; +SET statement_timeout = '120s'; +-- Star: two dimensions on two different fact keys (two hops into the root). +CREATE TABLE rpt_fwd_fact (id int, d1 int, d2 int, v int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwd_d1 (k int, a int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwd_d2 (k int, b int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwd_empty (k int, a int) DISTRIBUTED RANDOMLY; +-- Snowflake: fact -> dimension -> subdimension, a chain of two hops whose +-- first hop is consumed inside the reducer. rpt_fwd_dl and rpt_fwd_sub are +-- distributed on their join columns, so that hop is local (in-process); +-- rpt_fwd_dr is the same dimension distributed randomly, so the hop is global. +CREATE TABLE rpt_fwd_dl (k int, s int) DISTRIBUTED BY (s); +CREATE TABLE rpt_fwd_dr (k int, s int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwd_sub (k int, c int) DISTRIBUTED BY (k); +INSERT INTO rpt_fwd_fact + SELECT i, CASE WHEN i % 97 = 0 THEN NULL ELSE i % 1000 END, i % 500, i + FROM generate_series(1, 200000) i; +INSERT INTO rpt_fwd_d1 SELECT i, i FROM generate_series(0, 999) i; +INSERT INTO rpt_fwd_d2 SELECT i, i FROM generate_series(0, 499) i; +INSERT INTO rpt_fwd_dl SELECT i, i % 100 FROM generate_series(0, 999) i; +INSERT INTO rpt_fwd_dr SELECT i, i % 100 FROM generate_series(0, 999) i; +INSERT INTO rpt_fwd_sub SELECT i, i FROM generate_series(0, 99) i; +ANALYZE rpt_fwd_fact; +ANALYZE rpt_fwd_d1; +ANALYZE rpt_fwd_d2; +ANALYZE rpt_fwd_empty; +ANALYZE rpt_fwd_dl; +ANALYZE rpt_fwd_dr; +ANALYZE rpt_fwd_sub; +-- Legal LEAKPROOF function: the error reveals nothing about its argument. +-- It is still unsafe for an eager reducer when the main scan never executes. +CREATE FUNCTION pg_temp.rpt_fwd_constant_error(integer) RETURNS boolean +LANGUAGE plpgsql IMMUTABLE LEAKPROOF AS $$ +BEGIN + RAISE EXCEPTION 'constant failure'; +END +$$; +CREATE FUNCTION pg_temp.rpt_fwd_plan(q text, do_analyze boolean) RETURNS jsonb +LANGUAGE plpgsql AS $$ +DECLARE + plan jsonb; +BEGIN + EXECUTE format('EXPLAIN (%s VERBOSE, FORMAT JSON) %s', + CASE WHEN do_analyze THEN 'ANALYZE,' ELSE '' END, q) INTO plan; + RETURN plan; +END +$$; +-- Result of q as one comparable text value, with RPT on or off. +CREATE FUNCTION pg_temp.rpt_fwd_result(q text, rpt boolean) RETURNS text +LANGUAGE plpgsql AS $$ +DECLARE + r text; +BEGIN + PERFORM set_config('rpt.enable', CASE WHEN rpt THEN 'on' ELSE 'off' END, true); + EXECUTE format('SELECT string_agg(t::text, %L ORDER BY t::text) FROM (%s) t', ';', q) INTO r; + PERFORM set_config('rpt.enable', 'on', true); + RETURN coalesce(r, ''); +END +$$; +-- Nodes of a provider inside InitPlans (reducer) or outside them (main plan); +-- a NULL provider matches every node. +CREATE FUNCTION pg_temp.rpt_fwd_nodes(plan jsonb, provider text, in_reducer boolean) +RETURNS SETOF jsonb LANGUAGE sql AS $$ + WITH RECURSIVE walk(node, in_init) AS ( + SELECT plan->0->'Plan', false + UNION ALL + SELECT child, in_init OR coalesce(child->>'Parent Relationship', '') = 'InitPlan' + FROM walk CROSS JOIN LATERAL jsonb_array_elements(coalesce(node->'Plans', '[]')) child + ) + SELECT node FROM walk + WHERE in_init = in_reducer AND (provider IS NULL OR node->>'Custom Plan Provider' = provider) +$$; +CREATE FUNCTION pg_temp.rpt_fwd_count(plan jsonb, provider text, in_reducer boolean) +RETURNS bigint LANGUAGE sql AS $$ + SELECT count(*) FROM pg_temp.rpt_fwd_nodes(plan, provider, in_reducer) +$$; +-- Rows removed by forward filters in the main plan: by a consumer itself, or +-- by the Seq Scan it pushed the filter into. +CREATE FUNCTION pg_temp.rpt_fwd_removed(plan jsonb) RETURNS float8 +LANGUAGE sql AS $$ + SELECT coalesce(sum(coalesce((node->>'Rows Removed by Bloom Filter')::float8, 0) + + coalesce((node->>'Rows Removed by Pushdown Runtime Filter')::float8, 0)), 0) + FROM pg_temp.rpt_fwd_nodes(plan, NULL, false) node +$$; +DO $$ +DECLARE + opt text; + q text; + selectivity_limit text; + dump text; + plan jsonb; + prep_n bigint; + prep_s bigint; + star constant text := + 'SELECT count(*) AS n, sum(f.v) AS s FROM rpt_fwd_fact f + JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k JOIN rpt_fwd_d2 d2 ON f.d2 = d2.k + WHERE d1.a < 50 AND d2.b < 100'; + flake_local constant text := + 'SELECT count(*) AS n, sum(f.v) AS s FROM rpt_fwd_fact f + JOIN rpt_fwd_dl d ON f.d1 = d.k JOIN rpt_fwd_sub u ON d.s = u.k + WHERE u.c < 5'; + flake_global constant text := + 'SELECT count(*) AS n, sum(f.v) AS s FROM rpt_fwd_fact f + JOIN rpt_fwd_dr d ON f.d1 = d.k JOIN rpt_fwd_sub u ON d.s = u.k + WHERE u.c < 5'; +BEGIN + PERFORM set_config('rpt.enable', 'on', true); + PERFORM set_config('rpt.forward_min_root_rows', '0', true); + PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + + -- Shape: a reducer InitPlan with producers, a consumer in the main plan. + dump := rpt_transfer_plan(star); + IF dump NOT LIKE '%decision=inject%' THEN + RAISE EXCEPTION 'star not selected (optimizer=%): %', opt, dump; + END IF; + plan := pg_temp.rpt_fwd_plan(star, false); + IF pg_temp.rpt_fwd_count(plan, 'Anser Bloom Producer', true) = 0 OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', false) = 0 THEN + RAISE EXCEPTION 'reducer shape missing (optimizer=%): %', opt, plan; + END IF; + + -- Two root consumers must really be stacked. VERBOSE deparses their + -- INDEX_VAR outputs; copying the child's identity tlist here used to + -- make EXPLAIN loop forever in resolve_special_varno. + IF NOT EXISTS (SELECT FROM pg_temp.rpt_fwd_nodes(plan, 'Anser Bloom Consumer', false) n + WHERE n->'Plans'->0->>'Custom Plan Provider' = 'Anser Bloom Consumer') THEN + RAISE EXCEPTION 'star did not exercise stacked consumers (optimizer=%): %', opt, plan; + END IF; + + -- The property the design rests on: filters produced in the InitPlan's + -- dispatch are applied by the main plan's dispatch. + plan := pg_temp.rpt_fwd_plan(star, true); + IF pg_temp.rpt_fwd_removed(plan) <= 0 THEN + RAISE EXCEPTION 'no rows removed by a forward filter in the main plan (optimizer=%): %', opt, plan; + END IF; + + -- Multi-hop, global: the intermediate hop is an Anser consumer + -- inside the reducer, fed by a producer of the same dispatch. + dump := rpt_transfer_plan(flake_global); + IF dump NOT LIKE '%decision=inject%' OR dump NOT LIKE '%channels=2 %' OR + dump LIKE '%in-process%' THEN + RAISE EXCEPTION 'global chain not selected as two Anser hops (optimizer=%): %', opt, dump; + END IF; + plan := pg_temp.rpt_fwd_plan(flake_global, true); + IF pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', true) = 0 OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', false) <> 1 OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Producer', true) < 2 OR + pg_temp.rpt_fwd_removed(plan) <= 0 THEN + RAISE EXCEPTION 'global chain shape or effect missing (optimizer=%): %', opt, plan; + END IF; + + -- Multi-hop, local: the intermediate hop never leaves the QE. + dump := rpt_transfer_plan(flake_local); + IF dump NOT LIKE '%decision=inject%' OR dump NOT LIKE '%local in-process%' THEN + RAISE EXCEPTION 'local hop not in-process (optimizer=%): %', opt, dump; + END IF; + plan := pg_temp.rpt_fwd_plan(flake_local, true); + IF pg_temp.rpt_fwd_count(plan, 'RPT Local Bloom Producer', true) <> 1 OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', false) <> 1 OR + pg_temp.rpt_fwd_count(plan, 'RPT Local Bloom Consumer', true) <> 1 OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', true) <> 0 OR + (SELECT sum((n->>'Rows Removed by Local Bloom Filter')::float8) + FROM pg_temp.rpt_fwd_nodes(plan, 'RPT Local Bloom Consumer', true) n) <= 0 OR + pg_temp.rpt_fwd_removed(plan) <= 0 THEN + RAISE EXCEPTION 'local chain shape or effect missing (optimizer=%): %', opt, plan; + END IF; + + -- Results never change: star, chains, empty + -- filters, NULL keys, a volatile qual (dropped whole in the reducer), + -- an empty dimension, a preserved side. + FOREACH q IN ARRAY ARRAY[ + star, + flake_local, + flake_global, + 'SELECT f.id, f.v FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 3', + -- Existing scalar InitPlans and correlated expression SubPlans + -- must be walked with the statement's subplan lookup context. + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 50 AND f.v <= (SELECT max(v) FROM rpt_fwd_fact)', + 'SELECT f.id, (SELECT max(b) FROM rpt_fwd_d2 d2 WHERE d2.k = f.d2) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 50 AND f.id < 10', + + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 0', + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a IN (0,1,2,3,4,5,6,7,8,9)', + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a NOT IN (0,1,2,3,4,5,6,7,8,9)', + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 50 AND random() >= 0', + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_empty e ON f.d1 = e.k WHERE e.a < 50', + 'SELECT count(*) FROM rpt_fwd_fact f LEFT JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k AND d1.a < 50', + 'SELECT d.k, count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_dl d ON f.d1 = d.k + JOIN rpt_fwd_sub u ON d.s = u.k WHERE u.c IN (1, 7) GROUP BY d.k' + ] LOOP + IF pg_temp.rpt_fwd_result(q, true) IS DISTINCT FROM pg_temp.rpt_fwd_result(q, false) THEN + RAISE EXCEPTION 'results differ with RPT (optimizer=%): %', opt, q; + END IF; + END LOOP; + + -- A prepared statement keeps its reducer and its answer. + EXECUTE 'PREPARE rpt_fwd_prep AS ' || star; + FOR i IN 1..2 LOOP + EXECUTE 'EXECUTE rpt_fwd_prep' INTO prep_n, prep_s; + IF row(prep_n, prep_s)::text IS DISTINCT FROM pg_temp.rpt_fwd_result(star, false) THEN + RAISE EXCEPTION 'prepared statement result differs (optimizer=%, run %)', opt, i; + END IF; + END LOOP; + EXECUTE 'DEALLOCATE rpt_fwd_prep'; + + -- Gates: below the root-row threshold, nothing is injected. + PERFORM set_config('rpt.forward_min_root_rows', '1e12', true); + IF rpt_transfer_plan(star) NOT LIKE '%decision=root rows below rpt.forward_min_root_rows%' OR + pg_temp.rpt_fwd_count(pg_temp.rpt_fwd_plan(star, false), NULL, true) <> 0 THEN + RAISE EXCEPTION 'root-row gate did not refuse (optimizer=%)', opt; + END IF; + PERFORM set_config('rpt.forward_min_root_rows', '0', true); + + -- Gates: an exchange budget below the estimate refuses the chain. + PERFORM set_config('rpt.forward_max_exchange_ms', '0.001', true); + IF rpt_transfer_plan(star) NOT LIKE '%decision=estimated exchange cost above%' THEN + RAISE EXCEPTION 'cost gate did not refuse (optimizer=%)', opt; + END IF; + PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); + + -- Gates: a dimension keeping 90% of its keys is not worth a reducer. + q := 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 900'; + IF rpt_transfer_plan(q) NOT LIKE '%decision=estimated root selectivity not below%' OR + pg_temp.rpt_fwd_count(pg_temp.rpt_fwd_plan(q, false), NULL, true) <> 0 THEN + RAISE EXCEPTION 'selectivity gate did not refuse (optimizer=%)', opt; + END IF; + + -- A qual that can fail is never run by the reducer. The + -- reducer runs before the main plan and over rows the main plan may + -- never read; with LIMIT 0 the main plan reads none, so a reducer + -- evaluating 100 / a would raise division by zero (a = 0 exists), and + -- so would an I/O cast it cannot prove safe. With lambda the + -- qual-less chain is refused; with lambda off it is built with no + -- quals. Either way: no error, the same answer. + q := 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE 100 / d1.a > 1'; + dump := rpt_transfer_plan(q); + IF dump NOT LIKE '%dropped in the reducer (not provably error-free)%' OR + dump NOT LIKE '%decision=estimated root selectivity not below%' THEN + RAISE EXCEPTION 'unsafe qual not dropped, or chain not refused (optimizer=%): %', opt, dump; + END IF; + q := 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE pg_temp.rpt_fwd_constant_error(d1.a)'; + IF rpt_transfer_plan(q) NOT LIKE '%dropped in the reducer (not provably error-free)%' THEN + RAISE EXCEPTION 'leakproof user function accepted by reducer (optimizer=%)', opt; + END IF; + FOREACH selectivity_limit IN ARRAY ARRAY['0.35', '1'] LOOP + PERFORM set_config('rpt.forward_max_selectivity', selectivity_limit, true); + -- The planner keeps the LIMIT 0 join, so with lambda off the + -- reducer really runs (GPORCA may fold the query away instead). + q := 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE 100 / d1.a > 1 LIMIT 0'; + IF selectivity_limit = '1' AND opt = 'off' AND rpt_transfer_plan(q) NOT LIKE '%decision=inject%' THEN + RAISE EXCEPTION 'LIMIT 0 reducer not built with lambda off: %', rpt_transfer_plan(q); + END IF; + FOREACH q IN ARRAY ARRAY[ + 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE 100 / d1.a > 1 LIMIT 0', + 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 50 AND 100 / d1.a > 1 LIMIT 0', + 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE (d1.a::text)::int < 50 LIMIT 0', + 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE pg_temp.rpt_fwd_constant_error(d1.a) LIMIT 0' + ] LOOP + IF pg_temp.rpt_fwd_result(q, true) <> '' THEN + RAISE EXCEPTION 'LIMIT 0 returned rows (optimizer=%): %', opt, q; + END IF; + END LOOP; + END LOOP; + PERFORM set_config('rpt.forward_max_selectivity', '0.35', true); + -- ... while provably safe quals are kept. + IF rpt_transfer_plan(star) LIKE '%dropped in the reducer%' THEN + RAISE EXCEPTION 'safe quals dropped (optimizer=%)', opt; + END IF; + + -- Exclusivity: with Anser's own runtime filter on, RPT+ stands down. + PERFORM set_config('anser.runtime_filter', 'on', true); + IF rpt_transfer_plan(star) NOT LIKE '%forward: needs anser.enable=on and anser.runtime_filter=off%' OR + pg_temp.rpt_fwd_count(pg_temp.rpt_fwd_plan(star, false), NULL, true) <> 0 THEN + RAISE EXCEPTION 'RPT+ did not stand down for anser.runtime_filter (optimizer=%)', opt; + END IF; + PERFORM set_config('anser.runtime_filter', 'off', true); + END LOOP; +END +$$; +-- PL/pgSQL (SPI) runs the reducer like any other caller. Its static +-- statements keep the plan of their first call, so the function runs once, +-- with RPT on, and the baseline comes from uncached dynamic SQL. +CREATE FUNCTION pg_temp.rpt_fwd_spi() RETURNS bigint LANGUAGE plpgsql AS $$ +DECLARE + total bigint := 0; + r record; +BEGIN + FOR r IN SELECT f.v FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 50 LOOP + total := total + r.v; + END LOOP; + FOR r IN SELECT f.v FROM rpt_fwd_fact f JOIN rpt_fwd_dl d ON f.d1 = d.k + JOIN rpt_fwd_sub u ON d.s = u.k WHERE u.c < 5 LOOP + total := total + r.v; + END LOOP; + RETURN total; +END +$$; +DO $$ +DECLARE + with_rpt bigint; + without bigint; +BEGIN + PERFORM set_config('rpt.forward_min_root_rows', '0', true); + PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); + PERFORM set_config('rpt.enable', 'on', true); + with_rpt := pg_temp.rpt_fwd_spi(); + PERFORM set_config('rpt.enable', 'off', true); + EXECUTE 'SELECT (SELECT sum(f.v) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k + WHERE d1.a < 50) + + (SELECT sum(f.v) FROM rpt_fwd_fact f JOIN rpt_fwd_dl d ON f.d1 = d.k + JOIN rpt_fwd_sub u ON d.s = u.k WHERE u.c < 5)' INTO without; + IF with_rpt IS DISTINCT FROM without THEN + RAISE EXCEPTION 'SPI result differs: % vs %', with_rpt, without; + END IF; +END +$$; +DROP TABLE rpt_fwd_fact, rpt_fwd_d1, rpt_fwd_d2, rpt_fwd_empty, + rpt_fwd_dl, rpt_fwd_dr, rpt_fwd_sub; +DROP EXTENSION robust_predicate_transfer; +RESET anser.runtime_filter; +RESET rpt.backward; +RESET statement_timeout; diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_forward_faults.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_forward_faults.sql new file mode 100644 index 00000000000..a972090b481 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_forward_faults.sql @@ -0,0 +1,176 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Forward reducer under forced transport failures. Kept apart from +-- rpt_forward because gp_inject_fault exists only with the debug extensions, +-- and the regression driver skips any file that calls it. +-- +-- The chain is fact <- dimension <- subdimension with a random dimension, so +-- both hops are Anser channels and the first one has its consumer inside the +-- reducer InitPlan. Every case must return the RPT-off answer, and the next +-- command on the same gangs must answer: a reducer's dispatcher releases its +-- gangs before ExecutorEnd. If one of those connections is destroyed, libpq's +-- PGEVT_CONNDESTROY callback must remove Anser's references before it is freed. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS gp_inject_fault; +-- end_ignore +CREATE EXTENSION robust_predicate_transfer; +SET optimizer = off; +SET anser.runtime_filter = off; +SET rpt.backward = off; +SET anser.debug = on; +SET rpt.enable = on; +SET rpt.forward_min_root_rows = 0; +SET rpt.forward_max_exchange_ms = -1; +SET statement_timeout = '120s'; +CREATE TABLE rpt_fwf_fact (id int, d int, v int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwf_dim (k int, s int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwf_sub (k int, c int) DISTRIBUTED RANDOMLY; +INSERT INTO rpt_fwf_fact SELECT i, i % 1000, i FROM generate_series(1, 200000) i; +INSERT INTO rpt_fwf_dim SELECT i, i % 100 FROM generate_series(0, 999) i; +INSERT INTO rpt_fwf_sub SELECT i, i FROM generate_series(0, 99) i; +ANALYZE rpt_fwf_fact; +ANALYZE rpt_fwf_dim; +ANALYZE rpt_fwf_sub; +-- Dynamic SQL: a static statement would keep the plan of its first call, +-- with or without the reducer, whatever rpt.enable says later. +CREATE FUNCTION pg_temp.rpt_fwf_run(rpt boolean) RETURNS text LANGUAGE plpgsql AS $$ +DECLARE + r text; +BEGIN + PERFORM set_config('rpt.enable', CASE WHEN rpt THEN 'on' ELSE 'off' END, true); + EXECUTE 'SELECT count(*)::text || ''/'' || sum(f.v)::text + FROM rpt_fwf_fact f JOIN rpt_fwf_dim d ON f.d = d.k + JOIN rpt_fwf_sub u ON d.s = u.k WHERE u.c < 5' INTO r; + PERFORM set_config('rpt.enable', 'on', true); + RETURN r; +END +$$; +-- How often a fault fired, so a case cannot pass without exercising it. +CREATE FUNCTION pg_temp.rpt_fwf_hits(fault text, coordinator boolean) +RETURNS int LANGUAGE sql AS $$ + SELECT coalesce(sum(substring(gp_inject_fault(fault, 'status', dbid) + FROM 'num times hit:''([0-9]+)''')::int), 0)::int + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator +$$; +-- First occurrence only; on the coordinator a 'sleep' fault sleeps 1 s. +CREATE FUNCTION pg_temp.rpt_fwf_fault(fault text, action text, coordinator boolean) +RETURNS void LANGUAGE plpgsql AS $$ +BEGIN + IF action = 'reset' THEN + PERFORM gp_inject_fault(fault, 'reset', dbid) + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator; + ELSE + PERFORM gp_inject_fault(fault, action, '', '', '', 1, 1, + CASE WHEN coordinator THEN 1 ELSE 0 END, dbid) + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator; + END IF; +END +$$; +DO $$ +DECLARE + expected text := pg_temp.rpt_fwf_run(false); + got text; +BEGIN + IF rpt_transfer_plan('SELECT count(*) FROM rpt_fwf_fact f JOIN rpt_fwf_dim d ON f.d = d.k + JOIN rpt_fwf_sub u ON d.s = u.k WHERE u.c < 5') + NOT LIKE '%channels=2 %decision=inject%' THEN + RAISE EXCEPTION 'fixture chain not selected'; + END IF; + + -- 1. The coordinator holds the first delivery for a second; every + -- consumer waiting on it, the reducer's included, gives up at 100 ms and + -- runs unfiltered. + PERFORM set_config('anser.timeout_ms', '100', true); + PERFORM pg_temp.rpt_fwf_fault('anser_before_deliver', 'sleep', true); + got := pg_temp.rpt_fwf_run(true); + IF pg_temp.rpt_fwf_hits('anser_before_deliver', true) = 0 THEN + RAISE EXCEPTION 'held delivery: fault never fired'; + END IF; + PERFORM pg_temp.rpt_fwf_fault('anser_before_deliver', 'reset', true); + IF got IS DISTINCT FROM expected THEN + RAISE EXCEPTION 'held delivery: % <> %', got, expected; + END IF; + IF pg_temp.rpt_fwf_run(true) IS DISTINCT FROM expected THEN + RAISE EXCEPTION 'next command after held delivery'; + END IF; + + -- 2. The first consumer on each segment -- the reducer's -- walks away + -- after the first chunk of a 64 MB filter, leaving its delivery + -- unfinished on a connection of the InitPlan's gang, which is released + -- before the main plan runs. + PERFORM set_config('anser.timeout_ms', '500', true); + PERFORM set_config('anser.debug_delivery_chunk_bytes', '8192', true); + PERFORM set_config('anser.debug_min_filter_bytes', '67108864', true); + PERFORM pg_temp.rpt_fwf_fault('anser_after_chunk', 'skip', false); + got := pg_temp.rpt_fwf_run(true); + IF pg_temp.rpt_fwf_hits('anser_after_chunk', false) = 0 THEN + RAISE EXCEPTION 'walk-away: fault never fired'; + END IF; + PERFORM pg_temp.rpt_fwf_fault('anser_after_chunk', 'reset', false); + IF got IS DISTINCT FROM expected THEN + RAISE EXCEPTION 'walk-away: % <> %', got, expected; + END IF; + FOR i IN 1..2 LOOP + IF pg_temp.rpt_fwf_run(true) IS DISTINCT FROM expected THEN + RAISE EXCEPTION 'next command % after walk-away', i; + END IF; + END LOOP; + PERFORM set_config('anser.debug_delivery_chunk_bytes', '0', true); + PERFORM set_config('anser.debug_min_filter_bytes', '0', true); +END +$$; +-- 3. The statement is cancelled while the reducer waits for a delivery, so +-- the reducer's dispatcher is torn down on the abort path. Whether the +-- cancel lands before the query finishes is timing, so its output is not +-- compared; the session and its gangs must survive with the right answers. +DO $$ +BEGIN + PERFORM pg_temp.rpt_fwf_fault('anser_before_deliver', 'sleep', true); +END +$$; +SET anser.timeout_ms = 5000; +SET statement_timeout = '300ms'; +-- start_ignore +SELECT pg_temp.rpt_fwf_run(true) IS NOT NULL AS finished; +-- end_ignore +SET statement_timeout = '120s'; +RESET anser.timeout_ms; +DO $$ +BEGIN + PERFORM pg_temp.rpt_fwf_fault('anser_before_deliver', 'reset', true); + IF pg_temp.rpt_fwf_run(true) IS DISTINCT FROM pg_temp.rpt_fwf_run(false) THEN + RAISE EXCEPTION 'next command after cancel'; + END IF; +END +$$; +SELECT count(*) AS dim_rows FROM rpt_fwf_dim; +DROP TABLE rpt_fwf_fact, rpt_fwf_dim, rpt_fwf_sub; +DROP EXTENSION robust_predicate_transfer; +RESET rpt.enable; +RESET rpt.forward_min_root_rows; +RESET rpt.forward_max_exchange_ms; +RESET anser.debug; +RESET anser.runtime_filter; +RESET rpt.backward; +RESET optimizer; +RESET statement_timeout; diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_graph.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_graph.sql new file mode 100644 index 00000000000..3e2fe5c29a9 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_graph.sql @@ -0,0 +1,132 @@ +-- Join graph and transfer plan: equivalence classes, root choice, forward +-- chain shape, and consumer eligibility. Assertions are over structural +-- properties of the dump (LIKE/strpos), so the test does not depend on +-- estimate printouts or tie-breaking cosmetics. +CREATE EXTENSION robust_predicate_transfer; +CREATE TABLE rpt_a (a int, b int) DISTRIBUTED BY (a); +CREATE TABLE rpt_b (a int) DISTRIBUTED BY (a); +CREATE TABLE rpt_c (a int) DISTRIBUTED BY (a); +CREATE TABLE rpt_d (x int, y int) DISTRIBUTED BY (x); +CREATE TABLE rpt_r (a int) DISTRIBUTED REPLICATED; +INSERT INTO rpt_a SELECT i, i FROM generate_series(1, 100) i; +INSERT INTO rpt_b SELECT i FROM generate_series(1, 10) i; +INSERT INTO rpt_c SELECT i FROM generate_series(1, 50) i; +INSERT INTO rpt_d SELECT i, i FROM generate_series(1, 20) i; +ANALYZE rpt_a; +ANALYZE rpt_b; +ANALYZE rpt_c; +ANALYZE rpt_d; +INSERT INTO rpt_r SELECT i FROM generate_series(1, 10) i; +ANALYZE rpt_r; +DO $$ +DECLARE + opt text; + dump text; + q text; + distributed_rtindex text; + replicated_rtindex text; + preserved_name text; + preserved_rtindex text; + expected_members text; + member_count int; +BEGIN + IF EXISTS (SELECT FROM pg_proc p, LATERAL aclexplode(p.proacl) acl + WHERE p.oid = 'rpt_transfer_plan(text)'::regprocedure + AND acl.grantee = 0 AND acl.privilege_type = 'EXECUTE') THEN + RAISE EXCEPTION 'diagnostic function is executable by PUBLIC'; + END IF; + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + FOREACH q IN ARRAY ARRAY[ + 'SELECT * FROM rpt_a JOIN rpt_b ON rpt_a.a = rpt_b.a JOIN rpt_c ON rpt_b.a = rpt_c.a WHERE rpt_a.b > 50', + 'SELECT * FROM rpt_a JOIN rpt_b ON rpt_a.a = rpt_b.a JOIN rpt_c ON rpt_a.a = rpt_c.a WHERE rpt_a.b > 50', + 'SELECT * FROM rpt_a JOIN rpt_b ON rpt_a.a = rpt_b.a JOIN rpt_c ON rpt_b.a = rpt_c.a AND rpt_a.a = rpt_c.a WHERE rpt_a.b > 50' + ] LOOP + dump := rpt_transfer_plan(q); + distributed_rtindex := substring(dump FROM 'vertex ([0-9]+) rpt_a rows='); + SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) + INTO member_count, expected_members + FROM regexp_matches(dump, 'vertex ([0-9]+) (rpt_a|rpt_b|rpt_c) rows=', 'g') AS vertices(parts); + IF distributed_rtindex IS NULL OR member_count <> 3 OR + dump NOT LIKE ('%members=' || expected_members || ' leader=' || distributed_rtindex || chr(10) || '%') THEN + RAISE EXCEPTION 'class/root mismatch (optimizer=%): %', opt, dump; + END IF; + IF dump NOT LIKE ('%reducer order:' || chr(10) || '%') THEN + RAISE EXCEPTION 'unfiltered tables entered reducer: %', dump; + END IF; + END LOOP; + -- A co-distributed star must stay local after Algorithm 1 creates + -- implied dimension-to-dimension hops, in both transfer stages. + dump := rpt_transfer_plan('SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON a.a = c.a JOIN rpt_d d ON a.a = d.x WHERE a.b > 0 AND b.a > 0 AND c.a > 0 AND d.y > 0'); + IF dump NOT LIKE '%forward hop%' OR dump NOT LIKE '%backward hop%' OR + dump LIKE '%merge=global%' OR + EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE 'consumer_ok % yes') THEN + RAISE EXCEPTION 'local star uses global/blocking filters (optimizer=%): %', opt, dump; + END IF; + -- Replicated -> hash is local; the reverse broadcast stays global. + dump := rpt_transfer_plan('SELECT * FROM rpt_a JOIN rpt_r ON rpt_a.a = rpt_r.a WHERE rpt_a.b > 0 AND rpt_r.a > 0'); + -- Resolve table names from vertex records: ORCA can reorder rtindexes. + distributed_rtindex := substring(dump FROM 'vertex ([0-9]+) rpt_a rows='); + replicated_rtindex := substring(dump FROM 'vertex ([0-9]+) rpt_r rows='); + IF distributed_rtindex IS NULL OR replicated_rtindex IS NULL OR + dump NOT LIKE ('%forward hop ' || replicated_rtindex || '->' || distributed_rtindex || ' merge=local%') OR + dump NOT LIKE ('%backward hop ' || distributed_rtindex || '->' || replicated_rtindex || ' merge=global%') THEN + RAISE EXCEPTION 'replicated direction mismatch (optimizer=%): %', opt, dump; + END IF; + dump := rpt_transfer_plan('SELECT * FROM rpt_a JOIN rpt_d ON rpt_a.a = rpt_d.x AND rpt_a.b = rpt_d.y WHERE rpt_a.b > 50'); + SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) + INTO member_count, expected_members + FROM regexp_matches(dump, 'vertex ([0-9]+) (rpt_a|rpt_d) rows=', 'g') AS vertices(parts); + IF member_count <> 2 OR + (SELECT count(*) FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('class %: members=' || expected_members || ' leader=%')) <> 2 THEN + RAISE EXCEPTION 'expected two compound-key classes: %', dump; + END IF; + FOR q, preserved_name IN + SELECT * FROM (VALUES + ('SELECT * FROM rpt_a LEFT JOIN rpt_b ON rpt_a.a = rpt_b.a WHERE rpt_a.b > 50', 'rpt_a'), + ('SELECT * FROM rpt_b RIGHT JOIN rpt_a ON rpt_a.a = rpt_b.a WHERE rpt_a.b > 50', 'rpt_a'), + ('SELECT * FROM rpt_c LEFT JOIN (rpt_b JOIN rpt_a ON rpt_a.a = rpt_b.a AND rpt_b.a > 0) ON rpt_c.a = rpt_a.a WHERE rpt_c.a > 1', 'rpt_c') + ) AS cases(query, preserved) + LOOP + dump := rpt_transfer_plan(q); + preserved_rtindex := substring(dump FROM ('vertex ([0-9]+) ' || preserved_name || ' rows=')); + IF preserved_rtindex IS NULL THEN + RAISE EXCEPTION 'missing preserved table % (optimizer=%): %', preserved_name, opt, dump; + END IF; + IF EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('vertex ' || preserved_rtindex || ' %') + AND line LIKE '%apply from%') THEN + RAISE EXCEPTION 'preserved side reduced: %', dump; + END IF; + -- The first LEFT JOIN must filter the nullable side, so an + -- entirely empty transfer plan cannot satisfy this safety test. + IF q LIKE 'SELECT * FROM rpt_a LEFT JOIN%' AND + NOT EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE 'vertex %' AND line LIKE '%apply from%') THEN + RAISE EXCEPTION 'LEFT JOIN produced no filter applications (optimizer=%): %', opt, dump; + END IF; + END LOOP; + FOREACH q IN ARRAY ARRAY[ + 'SELECT * FROM rpt_a a FULL JOIN rpt_b b ON a.a = b.a', + 'SELECT a.a IN (SELECT b.a FROM rpt_b b) FROM rpt_a a', + 'SELECT * FROM rpt_a a WHERE NOT EXISTS (SELECT FROM rpt_b b WHERE a.a = b.a)', + 'SELECT * FROM rpt_a a JOIN (SELECT a::bigint aa FROM rpt_b) s ON a.a = s.aa' + ] LOOP + IF rpt_transfer_plan(q) <> ('no transfer regions' || chr(10)) THEN + RAISE EXCEPTION 'unsupported join/key accepted (optimizer=%): %', opt, q; + END IF; + END LOOP; + dump := rpt_transfer_plan('SELECT * FROM rpt_a WHERE rpt_a.b > 50 AND rpt_a.a IN (SELECT a FROM rpt_b)'); + SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) + INTO member_count, expected_members + FROM regexp_matches(dump, 'vertex ([0-9]+) (rpt_a|rpt_b) rows=', 'g') AS vertices(parts); + IF member_count <> 2 OR dump NOT LIKE ('%members=' || expected_members || ' leader=%') THEN + RAISE EXCEPTION 'missing SEMI class: %', dump; + END IF; + END LOOP; +END +$$; +DROP TABLE rpt_a, rpt_b, rpt_c, rpt_d, rpt_r; +DROP EXTENSION robust_predicate_transfer; diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_key_types.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_key_types.sql new file mode 100644 index 00000000000..b3859499d0e --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_key_types.sql @@ -0,0 +1,92 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Equality modulo 10 equates different raw Datum representations. RPT must +-- reject it even though the type is by-value and has default operator classes. +BEGIN; +SET LOCAL client_min_messages = warning; +CREATE EXTENSION robust_predicate_transfer; +CREATE SCHEMA rpt_eq_repro; +SET LOCAL search_path=rpt_eq_repro,pg_catalog; +SET LOCAL optimizer=off; +SET LOCAL statement_timeout='30s'; +SET LOCAL anser.runtime_filter=off; +SET LOCAL rpt.enable=off; +-- Legal by-value type whose equality identifies congruent integers mod 10. +CREATE TYPE modint; +CREATE FUNCTION modint_in(cstring) RETURNS modint AS 'int4in' LANGUAGE internal IMMUTABLE STRICT; +CREATE FUNCTION modint_out(modint) RETURNS cstring AS 'int4out' LANGUAGE internal IMMUTABLE STRICT; +CREATE TYPE modint(INPUT=modint_in,OUTPUT=modint_out,INTERNALLENGTH=4,PASSEDBYVALUE,ALIGNMENT=int4); +CREATE CAST (modint AS integer) WITHOUT FUNCTION AS IMPLICIT; +CREATE CAST (integer AS modint) WITHOUT FUNCTION AS IMPLICIT; +CREATE FUNCTION modint_eq(modint,modint) RETURNS boolean LANGUAGE plpgsql IMMUTABLE STRICT + AS $$ BEGIN RETURN ($1::integer % 10)=($2::integer % 10); END $$; +CREATE FUNCTION modint_hash(modint) RETURNS integer LANGUAGE sql IMMUTABLE STRICT + AS 'SELECT hashint4($1::integer % 10)'; +CREATE OPERATOR = (LEFTARG=modint,RIGHTARG=modint,FUNCTION=modint_eq,COMMUTATOR='=',HASHES); +CREATE OPERATOR CLASS modint_hash_ops DEFAULT FOR TYPE modint USING hash AS + OPERATOR 1 = (modint,modint), FUNCTION 1 modint_hash(modint); +-- Supply the type's own btree family as well: otherwise PostgreSQL can +-- choose integer's default family via the explicitly binary-compatible cast. +CREATE FUNCTION modint_cmp(modint,modint) RETURNS integer LANGUAGE sql IMMUTABLE STRICT + AS 'SELECT CASE WHEN $1::integer % 10 < $2::integer % 10 THEN -1 WHEN $1::integer % 10 > $2::integer % 10 THEN 1 ELSE 0 END'; +CREATE FUNCTION modint_lt(modint,modint) RETURNS boolean LANGUAGE sql IMMUTABLE STRICT AS 'SELECT modint_cmp($1,$2)<0'; +CREATE FUNCTION modint_le(modint,modint) RETURNS boolean LANGUAGE sql IMMUTABLE STRICT AS 'SELECT modint_cmp($1,$2)<=0'; +CREATE FUNCTION modint_gt(modint,modint) RETURNS boolean LANGUAGE sql IMMUTABLE STRICT AS 'SELECT modint_cmp($1,$2)>0'; +CREATE FUNCTION modint_ge(modint,modint) RETURNS boolean LANGUAGE sql IMMUTABLE STRICT AS 'SELECT modint_cmp($1,$2)>=0'; +CREATE OPERATOR < (LEFTARG=modint,RIGHTARG=modint,FUNCTION=modint_lt); +CREATE OPERATOR <= (LEFTARG=modint,RIGHTARG=modint,FUNCTION=modint_le); +CREATE OPERATOR > (LEFTARG=modint,RIGHTARG=modint,FUNCTION=modint_gt); +CREATE OPERATOR >= (LEFTARG=modint,RIGHTARG=modint,FUNCTION=modint_ge); +CREATE OPERATOR CLASS modint_btree_ops DEFAULT FOR TYPE modint USING btree AS + OPERATOR 1 <, OPERATOR 2 <=, OPERATOR 3 =, OPERATOR 4 >=, OPERATOR 5 >, FUNCTION 1 modint_cmp(modint,modint); +CREATE TABLE fact(k modint) DISTRIBUTED RANDOMLY; +CREATE TABLE dim(k modint,a int) DISTRIBUTED RANDOMLY; +INSERT INTO fact SELECT i::modint FROM generate_series(11,1010)i; +INSERT INTO dim SELECT i::modint,i FROM generate_series(1,10)i; +ANALYZE fact; +ANALYZE dim; +SET LOCAL rpt.forward_min_root_rows=0; +SET LOCAL rpt.forward_max_exchange_ms=-1; +SET LOCAL rpt.forward_max_selectivity=1; +DO $$ +DECLARE + opt text; + baseline bigint; + filtered bigint; + dump text; + q constant text := 'SELECT count(*) FROM fact f JOIN dim d ON f.k = d.k WHERE d.a < 11'; +BEGIN + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + PERFORM set_config('rpt.enable', 'off', true); + EXECUTE q INTO baseline; + PERFORM set_config('rpt.enable', 'on', true); + dump := public.rpt_transfer_plan(q); + EXECUTE q INTO filtered; + IF baseline <> 1000 OR filtered IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'custom equality lost rows (optimizer=%): % vs %', opt, filtered, baseline; + END IF; + IF dump NOT LIKE 'no transfer regions%' THEN + RAISE EXCEPTION 'custom equality was accepted (optimizer=%): %', opt, dump; + END IF; + END LOOP; +END +$$; +ROLLBACK; diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_regions.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_regions.sql new file mode 100644 index 00000000000..57912fd02df --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_regions.sql @@ -0,0 +1,78 @@ +-- Region discovery: transfer regions are connected components of the join +-- graph over base scans, and every operator the key resolver cannot descend +-- (aggregate, set op, window, limit, SRF/UNNEST, CteScan, partitioned +-- table) is a boundary -- queries whose only join key comes through one have +-- no region at all. +CREATE EXTENSION robust_predicate_transfer; +CREATE TABLE rpt_a (a int, b int) DISTRIBUTED BY (a); +CREATE TABLE rpt_b (a int) DISTRIBUTED BY (a); +CREATE TABLE rpt_c (a int) DISTRIBUTED BY (a); +INSERT INTO rpt_a SELECT i, i FROM generate_series(1, 100) i; +INSERT INTO rpt_b SELECT i FROM generate_series(1, 10) i; +INSERT INTO rpt_c SELECT i FROM generate_series(1, 50) i; +ANALYZE rpt_a; +ANALYZE rpt_b; +ANALYZE rpt_c; +-- A partitioned table joins through an Append, which resolution cannot cross. +CREATE TABLE rpt_p (a int) DISTRIBUTED BY (a) + PARTITION BY RANGE (a) (START (1) END (101) EVERY (50)); +INSERT INTO rpt_p SELECT i FROM generate_series(1, 100) i; +ANALYZE rpt_p; +DO $$ +DECLARE + opt text; + q text; + dump text; + expected_members text; + member_count int; +BEGIN + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + FOREACH q IN ARRAY ARRAY[ + 'SELECT 1', + 'SELECT * FROM rpt_a a JOIN (SELECT a, count(*) c FROM rpt_b GROUP BY a) s ON a.a = s.a WHERE s.c > 1', + 'SELECT * FROM rpt_a a JOIN (SELECT a FROM rpt_b UNION SELECT a FROM rpt_c) u ON a.a = u.a', + 'SELECT * FROM rpt_a a JOIN (SELECT a, row_number() OVER () r FROM rpt_b) w ON a.a = w.a WHERE w.r > 1', + 'SELECT * FROM rpt_a a JOIN (SELECT a FROM rpt_b ORDER BY a LIMIT 5) l ON a.a = l.a', + 'SELECT * FROM rpt_a a JOIN (SELECT u FROM rpt_b, unnest(ARRAY[a]) u) s ON a.a = s.u', + 'SELECT * FROM rpt_a a JOIN rpt_p p ON a.a = p.a' + ] LOOP + dump := rpt_transfer_plan(q); + IF dump <> ('no transfer regions' || chr(10)) THEN + RAISE EXCEPTION 'region boundary crossed (optimizer=%): %: %', opt, q, dump; + END IF; + END LOOP; + -- PostgreSQL retains a CteScan boundary here. ORCA exposes the + -- underlying base scans, so its unfiltered class is expected. + q := 'WITH w AS MATERIALIZED (SELECT a FROM rpt_b) SELECT * FROM rpt_a a JOIN w ON a.a = w.a'; + dump := rpt_transfer_plan(q); + IF opt = 'off' THEN + IF dump <> ('no transfer regions' || chr(10)) THEN + RAISE EXCEPTION 'CteScan boundary crossed: %', dump; + END IF; + ELSE + SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) + INTO member_count, expected_members + FROM regexp_matches(dump, 'vertex ([0-9]+) (a|rpt_b) rows=', 'g') AS vertices(parts); + IF member_count <> 2 OR + dump NOT LIKE ('%members=' || expected_members || ' leader=%') OR + dump NOT LIKE ('%reducer order:' || chr(10) || '%') OR + dump LIKE '%build for%' THEN + RAISE EXCEPTION 'unexpected ORCA materialized CTE plan: %', dump; + END IF; + END IF; + dump := rpt_transfer_plan('SELECT * FROM rpt_b JOIN rpt_a ON rpt_b.a = rpt_a.a'); + SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) + INTO member_count, expected_members + FROM regexp_matches(dump, 'vertex ([0-9]+) (rpt_a|rpt_b) rows=', 'g') AS vertices(parts); + IF member_count <> 2 OR dump NOT LIKE ('%members=' || expected_members || ' leader=%') OR + dump NOT LIKE ('%reducer order:' || chr(10) || '%') OR + dump LIKE '%build for%' THEN + RAISE EXCEPTION 'unfiltered pair produced transfers: %', dump; + END IF; + END LOOP; +END +$$; +DROP TABLE rpt_p; +DROP TABLE rpt_a, rpt_b, rpt_c; +DROP EXTENSION robust_predicate_transfer; diff --git a/gpcontrib/robust_predicate_transfer/src/rptbackward.cpp b/gpcontrib/robust_predicate_transfer/src/rptbackward.cpp new file mode 100644 index 00000000000..cb47b243a32 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rptbackward.cpp @@ -0,0 +1,142 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptbackward.cpp + * Backward placement on the scan stacks built by the forward pass. + * + *------------------------------------------------------------------------- + */ +#include "rptinject.h" + +extern "C" +{ +#include "anserplan.h" +} + +/* The proof names the original parent, not a wrapper added by forward. */ +static bool +rpt_backward_endpoint(const RptTransferMgr &schedule, + const RptInjectionState &state, int rtindex, + const RptKey &key, Plan *parent, AttrNumber attno) +{ + const auto &vertex = schedule.vertices.at(schedule.vertex_index_by_rtindex.at(rtindex)); + return vertex.scan != NULL && IsA(vertex.scan, SeqScan) && + RptStackMatches(state, vertex.scan, parent) && + attno > 0 && RptScanKeyPosition(vertex.scan, key) == attno; +} + +void +RptInjectBackward(PlannedStmt *stmt, const RptTransferMgr &schedule, + const std::vector &forward, + const RptForwardParams ¶ms, RptFilterSizeFn size_fn, + bool materialize, RptInjectionState *state, + std::string *trace, bool *injected) +{ + size_t root_classes = 0, strict = 0, eligible = 0; + for (const auto &group : schedule.groups) + root_classes += schedule.root_rtindexes.count(group.second.leader_rtindex); + for (const auto &proof : schedule.backward_consumers) + { + strict += proof.strict_eligible; + eligible += proof.eligible; + } + *trace += "runtime survey classes=" + std::to_string(schedule.groups.size()) + + " root_classes=" + std::to_string(root_classes) + + " strict=" + std::to_string(strict) + " eligible=" + std::to_string(eligible) + "\n"; + std::map widths; + for (const auto &decision : schedule.backward_consumers) + if (decision.eligible) + { + for (int slice_index : {decision.producer_slice_index, decision.consumer_slice_index}) + if (!widths.count(slice_index)) + widths[slice_index] = RptPgCall([&]() { + return AnserSliceProducers(stmt, slice_index); + }); + } + std::vector classes; + RptSelectBackward(schedule, forward, params, widths, size_fn, &classes); + for (auto &cls : classes) + { + if (!cls.accepted) + continue; + /* Class-wide preflight before allocating any executable node. */ + for (const auto &target : cls.consumers) + { + if (!target.accepted) + continue; + const auto &proof = target.proof; + if (!(proof.producer_key == cls.producer_key) || + widths[proof.producer_slice_index] != cls.producer_processes || + !rpt_backward_endpoint(schedule, *state, cls.producer_rtindex, + cls.producer_key, proof.producer_parent, proof.producer_attno) || + !rpt_backward_endpoint(schedule, *state, proof.consumer_rtindex, + proof.consumer_key, proof.consumer_parent, proof.consumer_attno)) + { + cls.accepted = false; + cls.reason = "backward placement changed or key is unavailable"; + break; + } + } + if (!cls.accepted) + { + for (auto &target : cls.consumers) + if (target.accepted) + { + target.accepted = false; + target.reason = cls.reason; + } + continue; + } + cls.condition_id = state->next_condition_id++; + cls.condition_key = "rpt_b_" + std::to_string(cls.producer_rtindex) + "_" + + std::to_string(cls.class_key.rtindex) + "_" + std::to_string(cls.class_key.attno); + if (!materialize) + continue; + Plan *scan = schedule.vertices.at( + schedule.vertex_index_by_rtindex.at(cls.producer_rtindex)).scan; + Plan *producer = RptPgCall([&]() { + CustomScan *node = AnserBuildBloomProducerScan(state->tops.at(scan), + RptScanKeyPosition(scan, cls.producer_key), cls.condition_id, + cls.condition_key.c_str(), cls.total_elems, (Size) cls.max_payload, + cls.planned_bytes, cls.producer_processes); + node->scan.plan.plan_node_id = state->next_plan_node_id++; + return (Plan *) node; + }); + RptRelinkStack(state, scan, producer); + for (const auto &target : cls.consumers) + { + if (!target.accepted) + continue; + const auto &proof = target.proof; + scan = schedule.vertices.at( + schedule.vertex_index_by_rtindex.at(proof.consumer_rtindex)).scan; + Plan *consumer = RptPgCall([&]() { + CustomScan *node = AnserBuildBloomConsumerScan(state->tops.at(scan), + proof.consumer_attno, cls.condition_id, cls.condition_key.c_str(), + cls.total_elems, (Size) cls.max_payload, cls.planned_bytes, + cls.producer_processes, proof.defer_first); + node->scan.plan.plan_node_id = state->next_plan_node_id++; + return (Plan *) node; + }); + RptRelinkStack(state, scan, consumer); + } + *injected = true; + } + RptDumpBackward(classes, trace); +} diff --git a/gpcontrib/robust_predicate_transfer/src/rptbackwardplan.cpp b/gpcontrib/robust_predicate_transfer/src/rptbackwardplan.cpp new file mode 100644 index 00000000000..4985f234779 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rptbackwardplan.cpp @@ -0,0 +1,159 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptbackwardplan.cpp + * Pure backward selection over the shared schedule and eligibility proof. + * + *------------------------------------------------------------------------- + */ +#include "rpt.h" +#include + +static int +rpt_slice_processes(const std::map &widths, int slice_index) +{ + auto found = widths.find(slice_index); + return found == widths.end() ? 0 : found->second; +} + +void +RptSelectBackward(const RptTransferMgr &schedule, + const std::vector &forward, + const RptForwardParams ¶ms, + const std::map &slice_processes, + RptFilterSizeFn size_fn, std::vector *out) +{ + out->clear(); + std::map > decisions; + for (const auto &decision : schedule.backward_consumers) + decisions[decision.class_key].push_back(decision); + for (const auto &entry : decisions) + { + RptBackwardClass cls; + cls.class_key = entry.first; + auto chosen = std::find_if(entry.second.begin(), entry.second.end(), + [](const RptBackwardConsumer &d) { return d.eligible; }); + const auto &first = chosen == entry.second.end() ? entry.second.front() : *chosen; + cls.producer_rtindex = first.producer_rtindex; + cls.producer_key = first.producer_key; + cls.producer_processes = rpt_slice_processes(slice_processes, + first.producer_slice_index); + const auto &producer = schedule.vertices.at( + schedule.vertex_index_by_rtindex.at(cls.producer_rtindex)); + double root_keep = 1; + for (const auto &comp : forward) + if (comp.accepted && comp.root_rtindex == cls.producer_rtindex) + root_keep = comp.est_selectivity; + cls.estimated_producer_rows = producer.cardinality * root_keep; + bool any = false; + for (const auto &decision : entry.second) + { + RptBackwardTarget target; + target.proof = decision; + target.reason = decision.reason; + target.processes = rpt_slice_processes(slice_processes, + decision.consumer_slice_index); + double rows = schedule.vertices.at( + schedule.vertex_index_by_rtindex.at(decision.consumer_rtindex)).cardinality; + double kept = 1; + if (decision.estimated_rows_removed >= 0 && rows > 0) + { + kept = std::max(0.0, std::min(1.0, + 1 - decision.estimated_rows_removed / rows)) * root_keep; + target.estimated_rows_removed = rows * (1 - kept); + } + if (decision.eligible) + { + if (target.processes <= 0) + target.reason = "unknown consumer process count"; + else if (params.max_selectivity < 1 && kept >= params.max_selectivity) + target.reason = "estimated member selectivity not below rpt.forward_max_selectivity"; + else + { + target.accepted = true; + target.reason = "inject"; + any = true; + } + } + cls.consumers.push_back(target); + } + if (!schedule.root_rtindexes.count(cls.producer_rtindex)) + cls.reason = "bridge-led class is deferred"; + else if (!any) + cls.reason = "no eligible consumers"; + else if (cls.producer_processes <= 0) + cls.reason = "unknown producer process count"; + else if (!size_fn(cls.estimated_producer_rows, &cls.total_elems, + &cls.max_payload, &cls.planned_bytes)) + cls.reason = "backward filter sizing declined"; + else + { + int64 consumer_processes = 0; + for (const auto &target : cls.consumers) + if (target.accepted) + consumer_processes += target.processes; + double mib = cls.planned_bytes / (1024.0 * 1024.0); + cls.est_cost_ms = mib * + (params.ms_per_mib_part * cls.producer_processes + + params.delivery_ms_per_mib * consumer_processes); + if (params.max_exchange_ms >= 0 && cls.est_cost_ms > params.max_exchange_ms) + cls.reason = "estimated exchange cost above rpt.forward_max_exchange_ms"; + else + { + cls.accepted = true; + cls.reason = "inject"; + } + } + if (!cls.accepted) + for (auto &target : cls.consumers) + if (target.accepted) + { + target.accepted = false; + target.reason = cls.reason; + } + out->push_back(cls); + } +} + +void +RptDumpBackward(const std::vector &classes, std::string *out) +{ + std::ostringstream text; + text << std::fixed << std::setprecision(3); + for (const auto &cls : classes) + { + size_t accepted = 0; + for (const auto &target : cls.consumers) + accepted += target.accepted; + text << "backward class root=" << cls.producer_rtindex + << " rep=" << cls.class_key.rtindex << '.' << cls.class_key.attno + << " consumers=" << accepted << " bytes=" << cls.planned_bytes + << " est_cost_ms=" << cls.est_cost_ms + << " decision=" << cls.reason << '\n'; + for (const auto &target : cls.consumers) + text << "backward consumer " << target.proof.consumer_rtindex + << " root=" << cls.producer_rtindex + << " rep=" << cls.class_key.rtindex << '.' << cls.class_key.attno + << " key=" << (cls.condition_key.empty() ? "-" : cls.condition_key) + << " defer_first=" << (target.proof.defer_first ? "yes" : "no") + << " rows_removed=" << target.estimated_rows_removed + << " decision=" << target.reason << '\n'; + } + *out += text.str(); +} diff --git a/gpcontrib/robust_predicate_transfer/src/rpteligible.cpp b/gpcontrib/robust_predicate_transfer/src/rpteligible.cpp new file mode 100644 index 00000000000..653394192f1 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rpteligible.cpp @@ -0,0 +1,426 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rpteligible.cpp + * Backward eligibility proof shared by runtime injection and the survey. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/src/rpteligible.cpp + * + *------------------------------------------------------------------------- + */ +#include "rpt.h" + +extern "C" +{ +#include "postgres.h" +#include "optimizer/walkers.h" +#include "anserplan.h" +} + + +struct RptPlacement +{ + std::vector path; + int slice_index = -1; + bool ambiguous = false; + bool in_subplan = false; +}; + +struct RptPlacementContext +{ + plan_tree_base_prefix base; + std::set scans; + std::map placements; + std::vector path; + int slice_index = 0; + bool in_subplan = false; +}; + +/* Expressions are visited too, but a scan's ancestor path contains only + * plans and plan lists. Omit lists so the previous entry is its relink parent. */ +static bool +rpt_placement_walker(Node *node, void *context) +{ + auto *ctx = static_cast(context); + if (node == NULL) + return false; + bool list = IsA(node, List) || IsA(node, IntList) || IsA(node, OidList); + int saved_slice_index = ctx->slice_index; + if (IsA(node, Motion)) + ctx->slice_index = ((Motion *) node)->motionID; + if (!list) + ctx->path.push_back((Plan *) node); + if (ctx->scans.count((Plan *) node)) + { + auto &placement = ctx->placements[(Plan *) node]; + placement.in_subplan = placement.in_subplan || ctx->in_subplan; + if (!placement.path.empty()) + placement.ambiguous = true; + else + { + placement.path = ctx->path; + placement.slice_index = ctx->slice_index; + } + } + bool result = RptPgCall([&]() { + return plan_tree_walker(node, rpt_placement_walker, context, false); + }); + if (!list) + ctx->path.pop_back(); + ctx->slice_index = saved_slice_index; + return result; +} + +/* Map a base column upward through identity/projection Vars. Do not guess + * that a relation's physical attno is also its output-tuple position. */ +static AttrNumber +rpt_path_key_slot(const RptPlacement &placement, size_t top, const RptKey &key) +{ + Plan *scan = placement.path.back(); + AttrNumber slot = 0; + ListCell *lc; + + if (!IsA(scan, SeqScan)) + return 0; /* Anser's builders/resolver support SeqScan endpoints. */ + foreach(lc, scan->targetlist) + { + auto *tle = (TargetEntry *) lfirst(lc); + if (IsA(tle->expr, Var)) + { + auto *var = (Var *) tle->expr; + if (var->varno == key.rtindex && var->varattno == key.attno) + { + slot = tle->resno; + break; + } + } + } + for (size_t i = placement.path.size() - 1; slot > 0 && i > top; --i) + { + Plan *parent = placement.path[i - 1]; + Plan *child = placement.path[i]; + int varno = 0; + if (outerPlan(parent) == child) + varno = OUTER_VAR; + else if (innerPlan(parent) == child) + varno = INNER_VAR; + else if (AnserIsRuntimeFilterScan(parent)) + varno = INDEX_VAR; + AttrNumber mapped = 0; + foreach(lc, parent->targetlist) + { + auto *tle = (TargetEntry *) lfirst(lc); + if (IsA(tle->expr, Var)) + { + auto *var = (Var *) tle->expr; + if (varno != 0 && var->varno == varno && var->varattno == slot) + { + mapped = tle->resno; + break; + } + } + } + slot = mapped; + } + return slot; +} + +static int +rpt_path_slice_index(const RptPlacement &placement, size_t top) +{ + int slice_index = 0; + for (size_t i = 0; i <= top; ++i) + if (IsA(placement.path[i], Motion)) + slice_index = ((Motion *) placement.path[i])->motionID; + return slice_index; +} + +static bool +rpt_resolve_placement(const RptPlacement &placement, size_t top, + const RptKey &key, bool consumer, + Plan **parent, AttrNumber *attno, int *slice_index) +{ + AttrNumber slot = rpt_path_key_slot(placement, top, key); + if (slot <= 0 || placement.path.size() < 2) + return false; + Plan *scan = placement.path.back(); + Plan *resolved = NULL; + if (top == placement.path.size() - 1) + { + *parent = placement.path[top - 1]; + *attno = slot; + *slice_index = placement.slice_index; + return outerPlan(*parent) == scan; + } + bool ok = RptPgCall([&]() { + return AnserResolveKeyScan(placement.path[top], slot, + rpt_path_slice_index(placement, top), consumer, parent, &resolved, attno, slice_index); + }); + return ok && resolved == scan && *slice_index == placement.slice_index; +} + +static const char * +rpt_consumer_path_reason(const RptPlacement &placement, size_t join, + bool builds_first) +{ + for (size_t i = join + 1; i + 1 < placement.path.size(); ++i) + { + Plan *node = placement.path[i]; + Plan *child = placement.path[i + 1]; + if (IsA(node, HashJoin) || IsA(node, MergeJoin) || IsA(node, NestLoop)) + { + JoinType type = ((Join *) node)->jointype; + bool inner = innerPlan(node) == child; + if ((type == JOIN_LEFT && inner) || (type == JOIN_RIGHT && !inner) || type == JOIN_FULL) + return "nullable side of outer join"; + if (type != JOIN_INNER && type != JOIN_LEFT && type != JOIN_RIGHT) + return "unsupported join on consumer path"; + } + } + if (builds_first) + return NULL; + for (size_t i = join + 1; i + 1 < placement.path.size(); ++i) + { + switch (nodeTag(placement.path[i])) + { + case T_Sort: return "sort between consumer and join"; + case T_Agg: return "aggregate between consumer and join"; + case T_Material: return "material between consumer and join"; + case T_Unique: return "unique between consumer and join"; + case T_WindowAgg: return "window between consumer and join"; + case T_Hash: return "hash between consumer and join"; + case T_SetOp: return "set operation between consumer and join"; + case T_HashJoin: + case T_MergeJoin: + case T_NestLoop: return "join between consumer and join"; + default: break; + } + } + return NULL; +} + +/* Anser retains its publication across rescans. Any external parameter, + * including SubPlan correlation and InitPlan parameters, is conservatively + * refused until the runtime has a generation/rebuild protocol. */ +static bool +rpt_parameterized_producer(Plan *build) +{ + return !RptPgCall([&]() { return bms_is_empty(build->extParam); }); +} + +static void +rpt_prove_consumer(RptBackwardConsumer *decision, const RptKey &producer_key, + const RptKey &consumer_key, const RptPlacement &producer, + const RptPlacement &consumer) +{ + decision->reason = "producer is not on a qualifying hash build side"; + if (producer.in_subplan || consumer.in_subplan) + { + decision->reason = "scan inside a SubPlan"; + return; + } + if (producer.ambiguous || consumer.ambiguous) + { + decision->reason = "scan has multiple plan placements"; + return; + } + if (producer.slice_index < 0 || consumer.slice_index < 0 || producer.slice_index == consumer.slice_index) + { + decision->reason = "same or unknown slice"; + return; + } + for (size_t i = 0; i + 1 < consumer.path.size(); ++i) + { + Plan *node = consumer.path[i]; + if (!IsA(node, HashJoin) || consumer.path[i + 1] != outerPlan(node)) + continue; + auto *join = (HashJoin *) node; + auto build = std::find(producer.path.begin(), producer.path.end(), innerPlan(node)); + if (build == producer.path.end()) + continue; + decision->rule9 = true; + if (join->join.jointype != JOIN_INNER && join->join.jointype != JOIN_RIGHT) + { + decision->reason = "unsupported qualifying hash join"; + continue; + } + bool builds_first = join->join.prefetch_inner || join->join.jointype == JOIN_RIGHT; + const char *reason = rpt_consumer_path_reason(consumer, i, builds_first); + if (reason != NULL) + { + decision->reason = reason; + continue; + } + if (rpt_parameterized_producer(innerPlan(node))) + { + decision->reason = "parameterized producer can be rescanned"; + continue; + } + if (!rpt_resolve_placement(producer, build - producer.path.begin(), producer_key, + false, &decision->producer_parent, + &decision->producer_attno, &decision->producer_slice_index)) + { + decision->reason = "unsupported producer key path"; + continue; + } + if (!rpt_resolve_placement(consumer, i + 1, consumer_key, true, + &decision->consumer_parent, &decision->consumer_attno, + &decision->consumer_slice_index)) + { + decision->reason = "unsupported consumer key path"; + continue; + } + decision->defer_first = !builds_first; + decision->strict_eligible = decision->eligible = true; + decision->reason = "eligible"; + return; + } +} + +static void +rpt_cap_backward_consumers(RptTransferMgr *mgr) +{ + std::map > classes; + for (size_t i = 0; i < mgr->backward_consumers.size(); ++i) + if (mgr->backward_consumers[i].strict_eligible) + classes[mgr->backward_consumers[i].class_key].push_back(i); + for (auto &group : classes) + { + auto &indices = group.second; + std::stable_sort(indices.begin(), indices.end(), [&](size_t a, size_t b) { + const auto &left = mgr->backward_consumers[a]; + const auto &right = mgr->backward_consumers[b]; + double l = left.estimated_rows_removed >= 0 ? left.estimated_rows_removed : + mgr->vertices[mgr->vertex_index_by_rtindex.at(left.consumer_rtindex)].cardinality; + double r = right.estimated_rows_removed >= 0 ? right.estimated_rows_removed : + mgr->vertices[mgr->vertex_index_by_rtindex.at(right.consumer_rtindex)].cardinality; + return l > r || (l == r && left.consumer_rtindex < right.consumer_rtindex); + }); + for (size_t i = rpt_max_backward_consumers; i < indices.size(); ++i) + { + auto &decision = mgr->backward_consumers[indices[i]]; + decision.eligible = false; + decision.reason = "backward consumer cap exceeded"; + } + } +} + +static void +rpt_consumer_verdicts(RptTransferMgr *mgr) +{ + for (const auto &vertex : mgr->vertices) + mgr->consumer_ok_by_rtindex[vertex.rtindex] = false; + std::map all_global; + for (const auto &decision : mgr->backward_consumers) + { + if (decision.local) + continue; + auto found = all_global.find(decision.consumer_rtindex); + if (found == all_global.end()) + all_global[decision.consumer_rtindex] = decision.eligible; + else + found->second = found->second && decision.eligible; + } + for (const auto &entry : all_global) + mgr->consumer_ok_by_rtindex[entry.first] = entry.second; +} + +void +RptCheckBackwardConsumers(PlannedStmt *stmt, RptTransferMgr *mgr) +{ + RptPlacementContext context; + for (const auto &vertex : mgr->vertices) + { + mgr->consumer_ok_by_rtindex[vertex.rtindex] = false; + context.scans.insert(vertex.scan); + } + mgr->backward_consumers.clear(); + if (stmt == NULL || stmt->planTree == NULL) + return; + RptPgCall([&]() { exec_init_plan_tree_base(&context.base, stmt); }); + rpt_placement_walker((Node *) stmt->planTree, &context); + ListCell *lc; + foreach(lc, stmt->subplans) + { + context.slice_index = -1; + context.in_subplan = true; /* Motions must not clear the SubPlan refusal. */ + rpt_placement_walker((Node *) lfirst(lc), &context); + } + std::set > seen; + for (const auto &node : mgr->graph) + { + for (const auto &edge : node.second.backward_stage.out) + { + for (size_t key = 0; key < edge.left_keys.size(); ++key) + { + RptKey source = edge.right_keys[key]; + RptKey target = edge.left_keys[key]; + RptKey rep = RptFindKeyRoot(source, mgr->uf_parent); + if (!seen.insert(std::make_pair(edge.other_rtindex, rep)).second) + continue; + RptBackwardConsumer decision; + decision.class_key = rep; + decision.producer_key = source; + decision.consumer_key = target; + decision.producer_rtindex = node.first; + decision.consumer_rtindex = edge.other_rtindex; + decision.local = edge.local; + auto group = mgr->groups.find(rep); + auto source_placement = context.placements.find(mgr->vertices[mgr->vertex_index_by_rtindex.at(node.first)].scan); + auto target_placement = context.placements.find(mgr->vertices[mgr->vertex_index_by_rtindex.at(edge.other_rtindex)].scan); + if ((source_placement != context.placements.end() && source_placement->second.in_subplan) || + (target_placement != context.placements.end() && target_placement->second.in_subplan)) + decision.reason = "scan inside a SubPlan"; + else if (edge.local) + decision.reason = "local edge uses core runtime filtering"; + else if (group == mgr->groups.end() || group->second.leader_rtindex != node.first) + decision.reason = "edge has no unprotected equivalence class"; + else if (!mgr->root_rtindexes.count(node.first)) + decision.reason = "bridge-led class is deferred"; + else if (!context.placements.count(mgr->vertices[mgr->vertex_index_by_rtindex.at(node.first)].scan) || + !context.placements.count(mgr->vertices[mgr->vertex_index_by_rtindex.at(edge.other_rtindex)].scan)) + decision.reason = "scan placement not found"; + else + { + const auto &producer = mgr->vertices[mgr->vertex_index_by_rtindex.at(node.first)]; + const auto &consumer = mgr->vertices[mgr->vertex_index_by_rtindex.at(edge.other_rtindex)]; + rpt_prove_consumer(&decision, source, target, + context.placements.at(producer.scan), context.placements.at(consumer.scan)); + if (decision.strict_eligible) + { + double root_ndv = RptEstimateDistinct(producer, source.attno); + double member_ndv = RptEstimateDistinct(consumer, target.attno); + if (root_ndv > 0 && member_ndv > 0) + { + double root_rows = producer.cardinality * + (producer.replicated ? 1.0 : std::max(1, producer.numsegments)); + root_ndv = std::min(root_ndv, root_rows); + decision.estimated_rows_removed = consumer.cardinality * + (1.0 - std::min(1.0, root_ndv / member_ndv)); + } + } + } + mgr->backward_consumers.push_back(decision); + } + } + } + rpt_cap_backward_consumers(mgr); + rpt_consumer_verdicts(mgr); +} diff --git a/gpcontrib/robust_predicate_transfer/src/rptforward.cpp b/gpcontrib/robust_predicate_transfer/src/rptforward.cpp new file mode 100644 index 00000000000..3a01ad28df8 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rptforward.cpp @@ -0,0 +1,694 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptforward.cpp + * Build the forward reducer as an InitPlan of the statement + * and apply its filters to the root scans of the main plan, then add + * backward filters through the shared injection entry point. + * + * A transfer is an Anser channel, except a local hop consumed only inside + * the reducer, which is handed over in-process (rptlocal.c). The reducer + * InitPlan is dispatched by + * preprocess_initplans() before the main plan, under the same + * gp_command_count, and the coordinator keeps a command's channels until + * ExecutorEnd -- so every forward channel is complete before a main-plan consumer + * subscribes. Backward channels are produced in the main plan and use the + * survey proof to place consumers that can safely wait. + * + * Reducer scans are copies of the main plan's scans (same scanrelid, so the + * same RTE, locks and permission checks) with a key-only targetlist. Their + * row set must be the main scan's or a superset: the quals + * are copied whole, in order, or dropped whole unless every one of them is + * proven unable to fail. The reducer runs eagerly, over rows the main plan + * may never visit (LIMIT 0, an empty outer side), so a qual that can raise an + * error there -- 100 / a with a = 0 -- would turn a successful query into a + * failing one, and the planner's fail-open cannot catch an executor error. + * + * Everything runs on a private copy of the statement; the caller publishes + * it only when the whole pass succeeds. PostgreSQL calls are made through + * RptPgCall, because C++ containers are alive throughout. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/src/rptforward.cpp + * + *------------------------------------------------------------------------- + */ +#include "rpt.h" +#include "rptinject.h" + +extern "C" +{ +#include "postgres.h" + +#include "cdb/cdbllize.h" +#include "cdb/cdbutil.h" +#include "nodes/makefuncs.h" +#include "nodes/nodeFuncs.h" +#include "optimizer/clauses.h" +#include "optimizer/optimizer.h" +#include "optimizer/walkers.h" +#include "utils/lsyscache.h" + +#include "catalog/pg_class.h" +#include "utils/syscache.h" + +#include "anser.h" +#include "anserplan.h" +#include "rptlocal.h" +} + +#include "rptqual.h" + + +/* ---- finding where a main-plan scan hangs ---- */ + +typedef struct RptParentContext +{ + plan_tree_base_prefix base; /* must be first */ + const std::set *targets; + std::map *parents; + std::vector *stack; /* enclosing plan nodes */ + std::map *scan_slices; + int slice_index; +} RptParentContext; + +static bool +rpt_parent_walker(Node *node, void *context) +{ + RptParentContext *ctx = (RptParentContext *) context; + + if (node == NULL) + return false; + if (!is_plan_node(node)) + return plan_tree_walker(node, rpt_parent_walker, context, false); + + int saved_slice_index = ctx->slice_index; + if (IsA(node, Motion)) + ctx->slice_index = ((Motion *) node)->motionID; + if (ctx->targets->count((Plan *) node) && !ctx->stack->empty()) + { + auto inserted = ctx->parents->emplace((Plan *) node, ctx->stack->back()); + if (!inserted.second) + inserted.first->second = NULL; /* ambiguous placement */ + (*ctx->scan_slices)[(Plan *) node] = ctx->slice_index; + } + + ctx->stack->push_back((Plan *) node); + bool result = plan_tree_walker(node, rpt_parent_walker, context, false); + + ctx->stack->pop_back(); + ctx->slice_index = saved_slice_index; + return result; +} + +/* Preserve original parents and slice indexes in the main tree. SubPlan IDs + * are resolved while visiting expressions, but their plans are not entered. */ +static std::map +rpt_find_parents(PlannedStmt *stmt, const std::set &targets, + std::map *scan_slices) +{ + std::map parents; + std::vector stack; + RptParentContext ctx; + + ctx.targets = &targets; + ctx.parents = &parents; + ctx.stack = &stack; + ctx.scan_slices = scan_slices; + ctx.slice_index = 0; + RptPgCall([&]() { + exec_init_plan_tree_base(&ctx.base, stmt); + (void) rpt_parent_walker((Node *) stmt->planTree, &ctx); + }); + return parents; +} + +/* Replace `child` under `parent`. Only the links key resolution can pass + * through hold base scans here: the outer/inner trees and Anser's own nodes. */ +static bool +rpt_relink(Plan *parent, Plan *child, Plan *replacement) +{ + if (parent->lefttree == child) + { + parent->lefttree = replacement; + return true; + } + if (parent->righttree == child) + { + parent->righttree = replacement; + return true; + } + if (IsA(parent, CustomScan)) + { + ListCell *lc; + + foreach(lc, ((CustomScan *) parent)->custom_plans) + { + if (lfirst(lc) == child) + { + lfirst(lc) = replacement; + return true; + } + } + } + return false; +} + +/* Output position of a base column in a scan's targetlist, or 0. */ +AttrNumber +RptScanKeyPosition(Plan *scan, const RptKey &key) +{ + if (!IsA(scan, SeqScan) || ((Scan *) scan)->scanrelid != (Index) key.rtindex) + return 0; + ListCell *lc; + + foreach(lc, scan->targetlist) + { + TargetEntry *tle = (TargetEntry *) lfirst(lc); + + if (IsA(tle->expr, Var) && + ((Var *) tle->expr)->varno == (int) ((Scan *) scan)->scanrelid && + ((Var *) tle->expr)->varattno == key.attno) + return tle->resno; + } + return 0; +} + +/* Verify the original parent still holds the current top, exactly once. */ +bool +RptStackMatches(const RptInjectionState &state, Plan *scan, Plan *expected_parent) +{ + auto parent = state.parents.find(scan); + auto top = state.tops.find(scan); + if (parent == state.parents.end() || parent->second == NULL || + parent->second != expected_parent || top == state.tops.end()) + return false; + int matches = (parent->second->lefttree == top->second) + + (parent->second->righttree == top->second); + if (IsA(parent->second, CustomScan)) + { + ListCell *lc; + foreach(lc, ((CustomScan *) parent->second)->custom_plans) + matches += lfirst(lc) == top->second; + } + return matches == 1; +} + +void +RptRelinkStack(RptInjectionState *state, Plan *scan, Plan *top) +{ + Plan *parent = state->parents.at(scan); + if (!RptStackMatches(*state, scan, parent) || + !rpt_relink(parent, state->tops.at(scan), top)) + RptPgCall([]() { elog(ERROR, "RPT scan stack changed after preflight"); }); + state->tops.at(scan) = top; +} + +/* ---- quals a reducer scan may evaluate ---- */ + +/* Only explicitly trusted built-in comparisons and their hash support may + * run eagerly. LEAKPROOF prevents argument disclosure, not unconditional + * errors. Every other call, coercion, Param or SubPlan drops the whole qual. */ +static bool +rpt_unsafe_qual_walker(Node *node, void *context) +{ + if (node == NULL) + return false; + switch (nodeTag(node)) + { + case T_Var: + case T_Const: + case T_List: + case T_BoolExpr: + case T_NullTest: + case T_BooleanTest: + case T_RelabelType: + break; + case T_FuncExpr: + if (!RptQualFunctionSafe(((FuncExpr *) node)->funcid)) + return true; + break; + case T_OpExpr: + case T_DistinctExpr: + case T_NullIfExpr: + set_opfuncid((OpExpr *) node); + if (!RptQualFunctionSafe(((OpExpr *) node)->opfuncid)) + return true; + break; + case T_ScalarArrayOpExpr: + { + ScalarArrayOpExpr *op = (ScalarArrayOpExpr *) node; + + set_sa_opfuncid(op); + if (!RptQualFunctionSafe(op->opfuncid) || + (OidIsValid(op->negfuncid) && !RptQualFunctionSafe(op->negfuncid)) || + (OidIsValid(op->hashfuncid) && !RptQualHashFunctionSafe(op->hashfuncid))) + return true; + break; + } + default: + return true; /* Param, SubPlan, coercions, everything else */ + } + return expression_tree_walker(node, rpt_unsafe_qual_walker, context); +} + +/* Whether a reducer may evaluate these quals (all of them, or none). */ +static bool +rpt_quals_safe(List *quals) +{ + return quals == NIL || RptPgCall([&]() { + return !contain_volatile_functions((Node *) quals) && + !rpt_unsafe_qual_walker((Node *) quals, NULL); + }); +} + +/* All of the main scan's quals, or none of them. */ +static List * +rpt_reducer_quals(List *quals) +{ + if (quals == NIL || !rpt_quals_safe(quals)) + return NIL; + return (List *) copyObjectImpl(quals); +} + +/* Rows per segment before quals, from pg_class.reltuples; < 0 when unknown. + * Partitioned tables spread them over numsegments, a replicated table keeps + * them all on each segment (as costsize.c's adjust_reloptinfo). */ +static double +rpt_base_rows(const RptVertexInfo &v) +{ + if (!OidIsValid(v.relid)) + return -1; + return RptPgCall([&]() { + HeapTuple tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(v.relid)); + double rows; + + if (!HeapTupleIsValid(tuple)) + return -1.0; + rows = ((Form_pg_class) GETSTRUCT(tuple))->reltuples; + ReleaseSysCache(tuple); + if (rows < 0) + return -1.0; + if (!v.replicated && v.numsegments > 0) + rows /= v.numsegments; + return rows; + }); +} + +/* What each vertex's reducer scan is expected to keep. */ +static std::map +rpt_vertex_estimates(const RptTransferMgr &forward) +{ + std::map est; + + for (const RptVertexInfo &v : forward.vertices) + { + RptForwardVertexEst e; + double base = rpt_base_rows(v); + + e.quals_dropped = !rpt_quals_safe(v.scan->qual); + e.rows = base > 0 ? base : (double) v.cardinality; + if (v.scan->qual != NIL && !e.quals_dropped && base > 0) + e.selectivity = std::min(1.0, std::max(0.0, v.scan->plan_rows / base)); + est[v.rtindex] = e; + } + return est; +} + +/* ---- node construction ---- */ + +static void +rpt_reset_copied_plan(Plan *plan, int *next_plan_node_id) +{ + plan->plan_node_id = (*next_plan_node_id)++; + plan->lefttree = NULL; + plan->righttree = NULL; + plan->initPlan = NIL; + plan->extParam = NULL; + plan->allParam = NULL; + plan->parallel_aware = false; + plan->async_capable = false; + plan->parallel = 0; + plan->flow = NULL; +} + +static Plan * +rpt_reducer_scan(Plan *main_scan, Oid relid, const std::vector &attnos, + bool *quals_dropped, int *next_plan_node_id) +{ + Plan *scan = (Plan *) copyObjectImpl(main_scan); + List *tlist = NIL; + Index varno = ((Scan *) main_scan)->scanrelid; + AttrNumber resno = 0; + + for (AttrNumber attno : attnos) + { + Oid typid; + int32 typmod; + Oid collid; + + get_atttypetypmodcoll(relid, attno, &typid, &typmod, &collid); + tlist = lappend(tlist, + makeTargetEntry((Expr *) makeVar(varno, attno, typid, typmod, collid, 0), + ++resno, NULL, false)); + } + scan->targetlist = tlist; + scan->qual = rpt_reducer_quals(main_scan->qual); + *quals_dropped = (main_scan->qual != NIL && scan->qual == NIL); + rpt_reset_copied_plan(scan, next_plan_node_id); + return scan; +} + +static Plan * +rpt_wrap_consumer(Plan *child, AttrNumber position, const RptForwardChannel &ch, + int width, int *next_plan_node_id) +{ + CustomScan *cs = AnserBuildBloomConsumerScan(child, position, ch.condition_id, + ch.condition_key.c_str(), + ch.total_elems, (Size) ch.max_payload, + ch.planned_bytes, width, + false /* channels are complete */ ); + + cs->scan.plan.plan_node_id = (*next_plan_node_id)++; + return (Plan *) cs; +} + +static Plan * +rpt_wrap_producer(Plan *child, AttrNumber position, const RptForwardChannel &ch, + int width, int *next_plan_node_id) +{ + CustomScan *cs = AnserBuildBloomProducerScan(child, position, ch.condition_id, + ch.condition_key.c_str(), + ch.total_elems, (Size) ch.max_payload, + ch.planned_bytes, width); + + cs->scan.plan.plan_node_id = (*next_plan_node_id)++; + return (Plan *) cs; +} + +static bool +rpt_anser_size(double est_rows, int64 *total_elems, int64 *max_payload, + int64 *planned_bytes) +{ + return RptPgCall([&]() { + return AnserRuntimeFilterSize(est_rows, total_elems, max_payload, + planned_bytes); + }); +} + +static bool +rpt_plan_has_anser_nodes_walker(Node *node, void *context) +{ + if (node == NULL) + return false; + if (is_plan_node(node) && + (AnserIsRuntimeFilterScan((Plan *) node) || RptIsLocalScan((Plan *) node))) + return true; + return plan_tree_walker(node, rpt_plan_has_anser_nodes_walker, context, true); +} + +/* The backend walker resolves SubPlan IDs even while walking expressions. + * Always provide the statement-backed prefix, including for diagnostic runs. */ +static bool +rpt_has_runtime_filters(PlannedStmt *stmt) +{ + plan_tree_base_prefix context; + + return RptPgCall([&]() { + exec_init_plan_tree_base(&context, stmt); + return rpt_plan_has_anser_nodes_walker((Node *) stmt->planTree, &context); + }); +} + +/* Main-plan scans of `vertex`, wrapped in consumers of every channel into it. + * False when the scan cannot be found or keyed; nothing is changed then. */ +static bool +rpt_apply_in_main(const RptVertexInfo &vertex, + const std::vector &channels, + RptInjectionState *state, int width, + bool dry_run) +{ + auto parent = state->parents.find(vertex.scan); + + if (!IsA(vertex.scan, SeqScan) || parent == state->parents.end() || + !RptStackMatches(*state, vertex.scan, parent->second)) + return false; + for (const RptForwardChannel &ch : channels) + if (ch.target_rtindex == vertex.rtindex && !ch.in_process && + RptScanKeyPosition(vertex.scan, ch.target_key) == 0) + return false; + if (dry_run) + return true; + + Plan *node = state->tops.at(vertex.scan); + + for (const RptForwardChannel &ch : channels) + if (ch.target_rtindex == vertex.rtindex && !ch.in_process) + node = RptPgCall([&]() { + return rpt_wrap_consumer(node, RptScanKeyPosition(vertex.scan, ch.target_key), + ch, width, &state->next_plan_node_id); + }); + if (node == state->tops.at(vertex.scan)) + return false; + RptRelinkStack(state, vertex.scan, node); + return true; +} + +/* Channel identities are assigned only after placement accepts a component. */ +static void +rpt_assign_forward_channels(std::vector &comps, + RptInjectionState &state) +{ + for (auto &comp : comps) + if (comp.accepted) + for (auto &ch : comp.channels) + { + ch.condition_id = state.next_condition_id++; + ch.condition_key = "rpt_f_" + std::to_string(ch.source_rtindex) + "_" + + std::to_string(ch.target_rtindex); + } +} + +/* Build the forward part using the statement's shared IDs and scan stacks. + * The selected components also supply root selectivity to backward sizing. */ +static List * +RptInjectForward(const RptTransferMgr &forward, + const RptForwardParams ¶ms, + const std::map &est, + bool materialize, RptInjectionState &state, + std::vector &comps, + std::string *trace, bool *injected) +{ + RptSelectForward(forward, params, est, rpt_anser_size, RptEstimateDistinct, &comps); + + for (RptForwardComponent &comp : comps) + { + if (!comp.accepted) + continue; + for (int rtindex : comp.chain_rtindexes) + { + const RptVertexInfo &v = forward.vertices[forward.vertex_index_by_rtindex.at(rtindex)]; + + if (!IsA(v.scan, SeqScan) || !OidIsValid(v.relid)) + { + comp.accepted = false; + comp.reason = "chain table " + std::to_string(rtindex) + " is not a plain SeqScan"; + break; + } + } + if (comp.accepted && + !rpt_apply_in_main(forward.vertices[forward.vertex_index_by_rtindex.at(comp.root_rtindex)], + comp.channels, &state, params.width, true)) + { + comp.accepted = false; + comp.reason = "root scan not found, not a SeqScan, or key not in its output"; + } + } + + rpt_assign_forward_channels(comps, state); + RptDumpForward(comps, trace); + for (const RptForwardComponent &comp : comps) + for (int rtindex : comp.chain_rtindexes) + if (est.at(rtindex).quals_dropped) + *trace += "forward: quals of " + std::to_string(rtindex) + + " dropped in the reducer (not provably error-free)\n"; + + for (const RptForwardComponent &comp : comps) + *injected = *injected || comp.accepted; + + List *children = NIL; + + for (const RptForwardComponent &comp : comps) + { + if (!materialize || !comp.accepted) + continue; + + for (int rtindex : comp.chain_rtindexes) + { + const RptVertexInfo &v = forward.vertices[forward.vertex_index_by_rtindex.at(rtindex)]; + std::vector attnos; + + for (const RptForwardChannel &ch : comp.channels) + { + AttrNumber a = 0; + + if (ch.target_rtindex == rtindex) + a = ch.target_key.attno; + else if (ch.source_rtindex == rtindex) + a = ch.source_key.attno; + if (a != 0 && std::find(attnos.begin(), attnos.end(), a) == attnos.end()) + attnos.push_back(a); + } + + bool dropped = false; /* reported with the dump */ + Plan *node = RptPgCall([&]() { + return rpt_reducer_scan(v.scan, v.relid, attnos, &dropped, &state.next_plan_node_id); + }); + + /* Consumers below producers: a filter is built from rows the + * inbound filters already reduced. */ + for (const RptForwardChannel &ch : comp.channels) + { + if (ch.target_rtindex != rtindex) + continue; + AttrNumber pos = (AttrNumber) (std::find(attnos.begin(), attnos.end(), + ch.target_key.attno) - attnos.begin()) + 1; + + node = RptPgCall([&]() { + if (!ch.in_process) + return rpt_wrap_consumer(node, pos, ch, params.width, &state.next_plan_node_id); + CustomScan *cs = RptBuildLocalConsumerScan(node, pos, ch.condition_id, + ch.total_elems); + + cs->scan.plan.plan_node_id = state.next_plan_node_id++; + return (Plan *) cs; + }); + } + for (const RptForwardChannel &ch : comp.channels) + { + if (ch.source_rtindex != rtindex) + continue; + AttrNumber pos = (AttrNumber) (std::find(attnos.begin(), attnos.end(), + ch.source_key.attno) - attnos.begin()) + 1; + + node = RptPgCall([&]() { + if (!ch.in_process) + return rpt_wrap_producer(node, pos, ch, params.width, &state.next_plan_node_id); + CustomScan *cs = RptBuildLocalProducerScan(node, pos, ch.condition_id, + ch.total_elems); + + cs->scan.plan.plan_node_id = state.next_plan_node_id++; + return (Plan *) cs; + }); + } + children = RptPgCall([&]() { return lappend(children, node); }); + } + + if (!rpt_apply_in_main(forward.vertices[forward.vertex_index_by_rtindex.at(comp.root_rtindex)], + comp.channels, &state, params.width, false)) + RptPgCall([]() { elog(ERROR, "RPT forward: root relink failed after preflight"); }); + } + + return children; +} + +PlannedStmt * +RptInject(PlannedStmt *stmt, bool materialize, std::string *trace) +{ + if (!gp_anser_enable || gp_anser_runtime_filter) + { + *trace += "forward: needs anser.enable=on and anser.runtime_filter=off\n"; + return stmt; + } + if (rpt_has_runtime_filters(stmt)) + { + *trace += "forward: plan already carries Anser nodes\n"; + return stmt; + } + + PlannedStmt *work = materialize + ? RptPgCall([&]() { return (PlannedStmt *) copyObjectImpl(stmt); }) + : stmt; + + RptTransferMgr analysis; + + RptCollectGraph(work, &analysis); + if (analysis.vertices.empty()) + { + *trace += "forward: no transfer regions\n"; + return stmt; + } + RptBuildClasses(&analysis); + + RptTransferMgr forward; + + RptBuildForwardPlan(&analysis, &forward); + RptCheckBackwardConsumers(work, &forward); + + /* Preflight against the plan: everything that could refuse a component + * is checked before any node is built, so a refusal changes nothing. */ + std::set targets; + + for (const RptVertexInfo &v : forward.vertices) + targets.insert(v.scan); + + RptInjectionState state; + std::map scan_slices; + state.parents = rpt_find_parents(work, targets, &scan_slices); + for (Plan *scan : targets) + state.tops[scan] = scan; + state.next_plan_node_id = materialize + ? RptPgCall([&]() { return AnserMaxPlanNodeId(work); }) + 1 : 0; + + RptForwardParams params; + + params.width = RptPgCall([]() { return getgpsegmentCount(); }); + params.ms_per_mib_part = rpt_forward_ms_per_mib_part; + params.delivery_ms_per_mib = rpt_delivery_ms_per_mib; + params.max_exchange_ms = rpt_forward_max_exchange_ms; + params.min_root_rows = rpt_forward_min_root_rows; + params.max_selectivity = rpt_forward_max_selectivity; + for (const auto &vertex : forward.vertices) + { + auto found = scan_slices.find(vertex.scan); + if (found != scan_slices.end()) + params.main_consumer_processes[vertex.rtindex] = RptPgCall([&]() { + return AnserSliceProducers(work, found->second); + }); + } + + std::map est = rpt_vertex_estimates(forward); + std::vector comps; + bool any = false; + List *children = NIL; + if (rpt_forward) + children = RptInjectForward(forward, params, est, materialize, + state, comps, trace, &any); + else + *trace += "forward: disabled\n"; + + if (rpt_backward) + RptInjectBackward(work, forward, comps, params, rpt_anser_size, + materialize, &state, trace, &any); + else + *trace += "backward: disabled\n"; + if (materialize && children != NIL) + RptAttachReducerInitPlan(work, children, params.width); + return materialize && any ? work : stmt; +} diff --git a/gpcontrib/robust_predicate_transfer/src/rptforwardplan.cpp b/gpcontrib/robust_predicate_transfer/src/rptforwardplan.cpp new file mode 100644 index 00000000000..c9718454924 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rptforwardplan.cpp @@ -0,0 +1,227 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptforwardplan.cpp + * Forward reducer selection: which forward chains to build, their channels, + * and the cost gate. No PostgreSQL calls, so it is unit-tested directly. + * + * The cost model was measured on one host with three primaries: every + * global hop pays about c milliseconds per MiB of bitset per producer, because + * the coordinator receives and folds the parts one at a time. Hops of one + * chain run one after another inside the reducer, so a component's cost is + * the sum over its channels. A local hop consumed only inside the reducer is + * handed over in-process (rptlocal.c) and costs no exchange. + * + * The benefit side is the paper's selectivity test: inject only when the + * chain is expected to keep less than lambda of the root's rows. A vertex + * keeps the fraction its own reducer quals keep, times what its inbound + * filters keep; a filter from u keeps s(u) * min(1, ndv(u.key) / ndv(v.key)) + * of v, assuming the smaller key domain is contained in the larger one. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/src/rptforwardplan.cpp + * + *------------------------------------------------------------------------- + */ +#include "rpt.h" + +extern "C" +{ +#include "postgres.h" +} + + +#include + +/* Every vertex whose filters can reach `root` through forward edges. */ +static std::set +rpt_forward_member_rtindexes(const RptTransferMgr &forward, int root_rtindex) +{ + std::set member_rtindexes; + std::vector pending_rtindexes(1, root_rtindex); + + member_rtindexes.insert(root_rtindex); + while (!pending_rtindexes.empty()) + { + int rtindex = pending_rtindexes.back(); + auto it = forward.graph.find(rtindex); + + pending_rtindexes.pop_back(); + if (it == forward.graph.end()) + continue; + /* An in-edge's other_rtindex names the producing side. */ + for (const RptGraphEdge &in : it->second.forward_stage.in) + if (member_rtindexes.insert(in.other_rtindex).second) + pending_rtindexes.push_back(in.other_rtindex); + } + return member_rtindexes; +} + +/* The fraction of `to` rows a filter from `from` keeps. */ +static double +rpt_transfer_keep(const RptTransferMgr &forward, const RptForwardChannel &ch, + double from_keep, RptNdvFn ndv_fn) +{ + double d = ndv_fn ? ndv_fn(forward.vertices[forward.vertex_index_by_rtindex.at(ch.source_rtindex)], + ch.source_key.attno) : -1; + double n = ndv_fn ? ndv_fn(forward.vertices[forward.vertex_index_by_rtindex.at(ch.target_rtindex)], + ch.target_key.attno) : -1; + + if (d > 0 && n > 0 && d < n) + return from_keep * d / n; + return from_keep; +} + +void +RptSelectForward(const RptTransferMgr &forward, const RptForwardParams ¶ms, + const std::map &est, + RptFilterSizeFn size_fn, RptNdvFn ndv_fn, + std::vector *out) +{ + + out->clear(); + for (int root_rtindex : forward.root_rtindexes) + { + RptForwardComponent comp; + std::set member_rtindexes = rpt_forward_member_rtindexes(forward, root_rtindex); + std::map keep; /* chain vertex -> fraction it keeps */ + bool sized = true; + + comp.root_rtindex = root_rtindex; + + /* reducer_order is already topological: producers before consumers. */ + for (int rtindex : forward.reducer_order_rtindexes) + if (rtindex != root_rtindex && member_rtindexes.count(rtindex)) + comp.chain_rtindexes.push_back(rtindex); + + for (int rtindex : comp.chain_rtindexes) + { + const RptGraphNode &node = forward.graph.at(rtindex); + const RptVertexInfo &source = forward.vertices[forward.vertex_index_by_rtindex.at(rtindex)]; + auto e = est.find(rtindex); + double rows = (e != est.end() && e->second.rows >= 0) + ? e->second.rows : (double) source.cardinality; + double own = e != est.end() ? e->second.selectivity : 1.0; + double k = own; + + /* In-channels of this rtindex were made by earlier chain vertices. */ + for (const RptForwardChannel &in : comp.channels) + if (in.target_rtindex == rtindex) + k *= rpt_transfer_keep(forward, in, keep[in.source_rtindex], ndv_fn); + keep[rtindex] = k; + + for (const RptGraphEdge &edge : node.forward_stage.out) + { + RptForwardChannel ch; + + if (edge.left_keys.empty() || edge.right_keys.empty()) + continue; + ch.source_rtindex = rtindex; + ch.target_rtindex = edge.other_rtindex; + ch.source_key = edge.left_keys[0]; /* out-edge: left = this side */ + ch.target_key = edge.right_keys[0]; + ch.local = edge.local; + ch.in_process = ch.local && ch.target_rtindex != root_rtindex; + + /* Sized for what the reducer scan inserts: its rows after its + * own quals -- all of them when the quals were dropped. */ + if (!size_fn(rows * own, &ch.total_elems, + &ch.max_payload, &ch.planned_bytes)) + { + comp.reason = "filter sizing declined for " + + std::to_string(ch.source_rtindex) + "->" + std::to_string(ch.target_rtindex); + sized = false; + break; + } + if (!ch.in_process) + { + /* Each hop delivers either to a reducer scan or to the + * root scan in the main plan. */ + int deliveries = params.width; + if (ch.target_rtindex == root_rtindex) + { + auto processes = params.main_consumer_processes.find(root_rtindex); + if (processes != params.main_consumer_processes.end()) + deliveries = processes->second; + } + ch.est_cost_ms = + (params.ms_per_mib_part * params.width + + params.delivery_ms_per_mib * deliveries) * + ((double) ch.planned_bytes / (1024.0 * 1024.0)); + } + comp.est_cost_ms += ch.est_cost_ms; + comp.channels.push_back(ch); + } + if (!sized) + break; + } + + for (const RptForwardChannel &ch : comp.channels) + if (ch.target_rtindex == root_rtindex) + comp.est_selectivity *= rpt_transfer_keep(forward, ch, keep[ch.source_rtindex], ndv_fn); + + const RptVertexInfo &rootv = forward.vertices[forward.vertex_index_by_rtindex.at(root_rtindex)]; + + if (!sized) + ; /* reason already set */ + else if (comp.channels.empty()) + comp.reason = "no forward edges"; + else if ((double) rootv.cardinality < params.min_root_rows) + comp.reason = "root rows below rpt.forward_min_root_rows"; + else if (params.max_selectivity < 1 && + comp.est_selectivity >= params.max_selectivity) + comp.reason = "estimated root selectivity not below rpt.forward_max_selectivity"; + else if (params.max_exchange_ms >= 0 && + comp.est_cost_ms > params.max_exchange_ms) + comp.reason = "estimated exchange cost above rpt.forward_max_exchange_ms"; + else + { + comp.accepted = true; + comp.reason = "inject"; + } + out->push_back(comp); + } +} + +void +RptDumpForward(const std::vector &components, std::string *out) +{ + std::ostringstream text; + + text << std::fixed << std::setprecision(3); + for (const RptForwardComponent &comp : components) + { + text << "forward component root=" << comp.root_rtindex << " chain=["; + for (size_t i = 0; i < comp.chain_rtindexes.size(); ++i) + text << (i ? " " : "") << comp.chain_rtindexes[i]; + text << "] channels=" << comp.channels.size() + << " est_cost_ms=" << comp.est_cost_ms + << " est_sel=" << comp.est_selectivity + << " decision=" << comp.reason << '\n'; + for (const RptForwardChannel &ch : comp.channels) + text << "forward channel " << ch.source_key.rtindex << '.' << ch.source_key.attno + << "->" << ch.target_key.rtindex << '.' << ch.target_key.attno + << " key=" << (ch.condition_key.empty() ? "-" : ch.condition_key) + << " bytes=" << ch.planned_bytes + << " est_ms=" << ch.est_cost_ms + << (ch.local ? " local" : "") + << (ch.in_process ? " in-process" : "") << '\n'; + } + *out += text.str(); +} diff --git a/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp b/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp new file mode 100644 index 00000000000..359e4365015 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp @@ -0,0 +1,640 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptgraph.cpp + * Union-find equivalence classes over join keys, vertex + * classification, and the SkipUnfilteredTable rewiring of the + * join graph. + * + * Ported from the RPT+ artifact (embryo-labs/dynamic-predicate-transfer), + * src/optimizer/predicate_transfer/transfer_graph_manager.cpp and dag.cpp -- + * MIT license, Copyright 2018-2025 Stichting DuckDB Foundation (see + * licenses/LICENSE-mit-duckdb-rpt.txt): + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/src/rptgraph.cpp + * + *------------------------------------------------------------------------- + */ +#include "rpt.h" + +extern "C" +{ +#include "postgres.h" +} + + + +/* + * One (edge, clause) pair. The artifact's EdgeInfo carries a single join + * condition; ours merges every equality clause of a vertex pair into one + * edge, so the SkipUnfilteredTable helpers track the clause alongside the + * edge wherever the original tracked the edge alone. + */ +struct RptEdgeClause +{ + RptEdgeInfo *edge; + size_t clause; +}; + +/* + * Find the representative key of a key's equivalence class. Ports + * FindBindingRoot (transfer_graph_manager.cpp:12-23), with path + * compression. + */ +RptKey +RptFindKeyRoot(const RptKey &key, std::map &uf_parent) +{ + std::map::iterator it = uf_parent.find(key); + RptKey representative_key; + + if (it == uf_parent.end()) + return key; + + representative_key = it->second; + if (!(representative_key == key)) + { + representative_key = RptFindKeyRoot(representative_key, uf_parent); + uf_parent[key] = representative_key; /* path compression */ + } + return representative_key; +} + +/* + * Merge the equivalence classes of two clause keys. Ports UnionBindings + * (transfer_graph_manager.cpp:25-48), union by attaching b to a. The + * artifact keeps a group pointer per stale root and resolves the root again + * at lookup time; mgr->groups is keyed by the representative key, so the + * loser's entry is merged into the winner's and erased here instead. + */ +static void +rpt_union_keys(const RptKey &a, const RptKey &b, Oid key_type, + RptTransferMgr *mgr) +{ + RptKey root_a = RptFindKeyRoot(a, mgr->uf_parent); + RptKey root_b = RptFindKeyRoot(b, mgr->uf_parent); + std::map::iterator ga; + std::map::iterator gb; + + if (root_a == root_b) + return; + + /* Union by attaching b to a */ + mgr->uf_parent[root_b] = root_a; + + ga = mgr->groups.find(root_a); + if (ga == mgr->groups.end()) + { + RptKeyGroup group; + + group.leader_rtindex = -1; + group.key_type = key_type; + group.member_rtindexes.insert(a.rtindex); + ga = mgr->groups.insert(std::make_pair(root_a, group)).first; + } + + gb = mgr->groups.find(root_b); + if (gb == mgr->groups.end()) + { + RptKeyGroup group; + + group.leader_rtindex = -1; + group.key_type = key_type; + group.member_rtindexes.insert(b.rtindex); + gb = mgr->groups.insert(std::make_pair(root_b, group)).first; + } + + ga->second.member_rtindexes.insert(gb->second.member_rtindexes.begin(), + gb->second.member_rtindexes.end()); + mgr->groups.erase(root_b); +} + +/* + * ClassifyTables (transfer_graph_manager.cpp:265-309): classify all tables + * into three categories: intermediate table, unfiltered table, and filtered + * table. + * + * The artifact tests LOGICAL_FILTER / LogicalGet.table_filters; our region + * pass has already set RptVertexInfo.filtered from the scan's restriction + * quals, which is the same test seen one plan layer down. The artifact + * counts the group of each incident edge's left binding; we count the group + * of each of the vertex's own join keys. The two differ only on protected + * edges, whose keys were never unioned and so count as groups of their own + * here (the artifact conflates all of them into one null group). + */ +static void +rpt_classify_tables(RptTransferMgr *mgr) +{ + size_t i; + + for (i = 0; i < mgr->vertices.size(); i++) + { + RptVertexInfo *v = &mgr->vertices[i]; + std::set belong_groups; + std::set::const_iterator kit; + + v->intermediate = false; + v->unfiltered = false; + + /* 1. Filtered table: the scan carries restriction quals */ + if (v->filtered) + continue; + + /* 2. Find belong groups */ + for (kit = v->join_keys.begin(); kit != v->join_keys.end(); ++kit) + { + if (mgr->uf_parent.find(*kit) != mgr->uf_parent.end() || + mgr->groups.find(*kit) != mgr->groups.end()) + belong_groups.insert(RptFindKeyRoot(*kit, mgr->uf_parent)); + else + belong_groups.insert(*kit); /* never unioned: a group of its own */ + } + + /* 3. Intermediate table: touches more than one key group */ + if (belong_groups.size() > 1) + { + v->intermediate = true; + continue; + } + + /* 4. Last, it is an unfiltered table */ + v->unfiltered = true; + } +} + +/* Helper: check whether data can flow from one table to another */ +static bool +rpt_can_flow(const RptEdgeInfo *e, int source_rtindex, int target_rtindex) +{ + if (e->left_rtindex == source_rtindex && e->right_rtindex == target_rtindex) + return !e->protect_right; /* Left → Right allowed if not protected */ + if (e->right_rtindex == source_rtindex && e->left_rtindex == target_rtindex) + return !e->protect_left; /* Right → Left allowed if not protected */ + return false; +} + +/* Helper: get the key on vertex t's side of one clause of edge e */ +static const RptKey & +rpt_key_at(const RptEdgeInfo *e, size_t clause, int rtindex) +{ + return (e->left_rtindex == rtindex) ? e->clauses[clause].first : e->clauses[clause].second; +} + +/* Helper: get the key on the side opposite vertex t in one clause of edge e */ +static const RptKey & +rpt_key_opposite(const RptEdgeInfo *e, size_t clause, int rtindex) +{ + return (e->left_rtindex == rtindex) ? e->clauses[clause].second : e->clauses[clause].first; +} + +/* + * Helper: append the clauses of `other` that `old` does not have yet, in + * old's orientation (the artifact's edges are single-clause, so it has no + * need for this). + */ +static void +rpt_merge_clauses(RptEdgeInfo *old, const RptEdgeInfo &other, bool reversed) +{ + size_t i; + + for (i = 0; i < other.clauses.size(); i++) + { + std::pair clause = other.clauses[i]; + bool have = false; + size_t j; + + if (reversed) + std::swap(clause.first, clause.second); + for (j = 0; j < old->clauses.size(); j++) + { + if (old->clauses[j].first == clause.first && + old->clauses[j].second == clause.second) + { + have = true; + break; + } + } + if (!have) + old->clauses.push_back(clause); + } +} + +/* + * Helper: insert new edge into neighbor_matrix, merging if already exists. + * The matrix holds each edge twice ([a][b] and [b][a]) by value where the + * artifact shares one EdgeInfo between both directions, so every write here + * is mirrored explicitly. + */ +static bool +rpt_upsert_edge(RptTransferMgr *mgr, const RptEdgeInfo &ne) +{ + int i = ne.left_rtindex; + int j = ne.right_rtindex; + std::map::iterator ij; + bool exists = false; + bool changed = false; + + ij = mgr->neighbor_matrix[i].find(j); + if (ij != mgr->neighbor_matrix[i].end()) + { + /* + * The artifact compares the single bindings and clobbers a parallel + * edge on different columns; our edges already hold every clause of + * the vertex pair, so the same vertex pair in either orientation is + * the same edge and the clauses merge instead. + */ + RptEdgeInfo before = ij->second; + + bool same = (ij->second.left_rtindex == ne.left_rtindex && + ij->second.right_rtindex == ne.right_rtindex); + bool rev = (ij->second.left_rtindex == ne.right_rtindex && + ij->second.right_rtindex == ne.left_rtindex); + + if (same) + { + ij->second.protect_left |= ne.protect_left; + ij->second.protect_right |= ne.protect_right; + rpt_merge_clauses(&ij->second, ne, false); + exists = true; + } + else if (rev) + { + ij->second.protect_left |= ne.protect_right; + ij->second.protect_right |= ne.protect_left; + rpt_merge_clauses(&ij->second, ne, true); + exists = true; + } + if (exists) + { + changed = before.protect_left != ij->second.protect_left || + before.protect_right != ij->second.protect_right || + before.clauses.size() != ij->second.clauses.size(); + mgr->neighbor_matrix[j][i] = ij->second; + } + } + if (!exists) + { + mgr->neighbor_matrix[i][j] = ne; + mgr->neighbor_matrix[j][i] = ne; + changed = true; + } + return changed; +} + +/* + * SkipUnfilteredTable (transfer_graph_manager.cpp:311-511). + * + * Unfiltered tables only receive Bloom filters, they do not generate them: + * splice a direct bypass edge between the carriers and the destinations on + * the same join key, block the unfiltered table's outgoing edges, and drop + * fully blocked edges. Then, for each intermediate table with no incoming + * edge left, block its outgoing edges as well. + */ +static void +rpt_skip_unfiltered(RptTransferMgr *mgr) +{ + std::vector unfiltered_rtindexes; + std::vector intermediate_rtindexes; + size_t i; + bool changed; + + /* Sorted range-table indexes keep iteration and dumps deterministic. */ + for (i = 0; i < mgr->vertices.size(); i++) + { + if (mgr->vertices[i].unfiltered) + unfiltered_rtindexes.push_back(mgr->vertices[i].rtindex); + else if (mgr->vertices[i].intermediate) + intermediate_rtindexes.push_back(mgr->vertices[i].rtindex); + } + std::sort(unfiltered_rtindexes.begin(), unfiltered_rtindexes.end()); + std::sort(intermediate_rtindexes.begin(), intermediate_rtindexes.end()); + + /* 1) Iteratively skip unfiltered tables until no new edges are added. */ + do + { + changed = false; + + for (i = 0; i < unfiltered_rtindexes.size(); i++) + { + int rtindex = unfiltered_rtindexes[i]; + std::map &adj = mgr->neighbor_matrix[rtindex]; + std::map > incoming_by_col; + std::vector outgoing; + std::map::iterator pit; + std::map >::iterator git; + size_t j; + + /* ---- Step 1: collect all incoming edges from carrier tables → t ---- */ + for (pit = adj.begin(); pit != adj.end(); ++pit) + { + RptEdgeInfo *e = &pit->second; + size_t c; + + if (mgr->vertices[mgr->vertex_index_by_rtindex[pit->first]].unfiltered || + !rpt_can_flow(e, pit->first, rtindex)) + continue; + for (c = 0; c < e->clauses.size(); c++) + { + RptEdgeClause ref; + + ref.edge = e; + ref.clause = c; + incoming_by_col[rpt_key_at(e, c, rtindex)].push_back(ref); + } + } + + /* ---- Step 2: collect all outgoing edges t → neighbor ---- */ + for (pit = adj.begin(); pit != adj.end(); ++pit) + { + if (rpt_can_flow(&pit->second, rtindex, pit->first)) + outgoing.push_back(&pit->second); + } + + /* + * ---- Step 3: bypass t ---- + * For every (carrier → t) and (t → dst) pair with the same join + * column, create a direct edge (carrier → dst). + */ + for (git = incoming_by_col.begin(); git != incoming_by_col.end(); ++git) + { + for (j = 0; j < outgoing.size(); j++) + { + RptEdgeInfo *out_e = outgoing[j]; + size_t oc; + + for (oc = 0; oc < out_e->clauses.size(); oc++) + { + RptEdgeClause rep = git->second[0]; /* representative incoming edge */ + RptEdgeInfo *in_e = rep.edge; + RptEdgeInfo ne; + int src_v; + int dst_v; + + if (!(rpt_key_at(out_e, oc, rtindex) == git->first)) + continue; /* must be same join key */ + + /* src is the carrier: in_e's side opposite t */ + src_v = (in_e->left_rtindex == rtindex) ? in_e->right_rtindex : in_e->left_rtindex; + + ne.left_rtindex = src_v; + ne.key_type = out_e->key_type; + ne.protect_left = false; + ne.protect_right = false; + + if (out_e->left_rtindex == rtindex) + { + /* t is left side of out_e */ + dst_v = out_e->right_rtindex; + ne.right_rtindex = dst_v; + ne.clauses.push_back(std::make_pair(rpt_key_opposite(in_e, rep.clause, rtindex), + out_e->clauses[oc].second)); + ne.protect_left = true; /* disable reverse flow (R → L) */ + } + else + { + /* t is right side of out_e */ + dst_v = out_e->left_rtindex; + ne.right_rtindex = dst_v; + ne.clauses.push_back(std::make_pair(rpt_key_opposite(in_e, rep.clause, rtindex), + out_e->clauses[oc].first)); + + ne.protect_left = true; /* disable dst -> src */ + } + + if (src_v == dst_v) + continue; /* a self-edge no later pass can observe */ + + changed = rpt_upsert_edge(mgr, ne) || changed; + } + } + } + + /* ---- Step 4: disable all outgoing edges from this unfiltered table ---- */ + for (j = 0; j < outgoing.size(); j++) + { + RptEdgeInfo *e = outgoing[j]; + + if (e->left_rtindex == rtindex) + { + if (!e->protect_right) + { + e->protect_right = true; /* block Left→Right */ + changed = true; + } + } + else + { + if (!e->protect_left) + { + e->protect_left = true; /* block Right→Left */ + changed = true; + } + } + } + + /* ---- Step 5: remove fully blocked edges (no direction left) ---- */ + for (pit = adj.begin(); pit != adj.end();) + { + RptEdgeInfo *e = &pit->second; + int nbr = pit->first; + + if (e->protect_left && e->protect_right) + { + ++pit; + mgr->neighbor_matrix[rtindex].erase(nbr); + mgr->neighbor_matrix[nbr].erase(rtindex); + changed = true; + } + else + { + /* keep the reverse matrix entry in sync */ + mgr->neighbor_matrix[nbr][rtindex] = *e; + ++pit; + } + } + } + } while (changed); + + /* + * 2) For each intermediate table, if it has no filter and any in-edge, it + * should not have out-edge. (The artifact's comment says "any in-edge" + * but its code -- and ours -- does this when no in-edge is left.) + */ + for (i = 0; i < intermediate_rtindexes.size(); i++) + { + int rtindex = intermediate_rtindexes[i]; + std::map &adj = mgr->neighbor_matrix[rtindex]; + std::map > incoming_by_col; + std::map::iterator pit; + size_t c; + + /* ---- collect all incoming edges from carrier tables → t ---- */ + for (pit = adj.begin(); pit != adj.end(); ++pit) + { + RptEdgeInfo *e = &pit->second; + + if (!rpt_can_flow(e, pit->first, rtindex)) + continue; + for (c = 0; c < e->clauses.size(); c++) + { + RptEdgeClause ref; + + ref.edge = e; + ref.clause = c; + incoming_by_col[rpt_key_at(e, c, rtindex)].push_back(ref); + } + } + + if (incoming_by_col.empty()) + { + for (pit = adj.begin(); pit != adj.end(); ++pit) + { + RptEdgeInfo *e = &pit->second; + + if (!rpt_can_flow(e, rtindex, pit->first)) + continue; + if (e->left_rtindex == rtindex) + e->protect_right = true; /* block Left→Right */ + else + e->protect_left = true; /* block Right→Left */ + } + + for (pit = adj.begin(); pit != adj.end();) + { + RptEdgeInfo *e = &pit->second; + int nbr = pit->first; + + if (e->protect_left && e->protect_right) + { + ++pit; + mgr->neighbor_matrix[rtindex].erase(nbr); + mgr->neighbor_matrix[nbr].erase(rtindex); + } + else + { + /* keep the reverse matrix entry in sync */ + mgr->neighbor_matrix[nbr][rtindex] = *e; + ++pit; + } + } + } + } +} + +/* + * Build the join-key equivalence classes and classify the vertices, then + * rewire the graph around tables that carry no information of their own. + * This is the rptgraph half of the artifact's Build() + * (transfer_graph_manager.cpp:50-70): the union pass of ExtractEdgesInfo + * over edges the region pass already extracted, ClassifyTables, and + * SkipUnfilteredTable. + */ +void +RptBuildClasses(RptTransferMgr *mgr) +{ + std::map >::iterator row; + + /* + * Union the clause keys of every edge that is unprotected both ways + * (transfer_graph_manager.cpp:249-251): the preserved side of an outer + * join keeps its rows whether or not they match, so a protected edge + * never merges classes. The matrix holds each edge in both directions; + * unioning twice is idempotent. + */ + for (row = mgr->neighbor_matrix.begin(); row != mgr->neighbor_matrix.end(); ++row) + { + std::map::iterator eit; + + for (eit = row->second.begin(); eit != row->second.end(); ++eit) + { + RptEdgeInfo *edge = &eit->second; + size_t c; + + if (edge->protect_left || edge->protect_right) + continue; + for (c = 0; c < edge->clauses.size(); c++) + rpt_union_keys(edge->clauses[c].first, edge->clauses[c].second, + edge->key_type, mgr); + } + } + + /* Equality is transitive within an unprotected class. Make its implicit + * edges explicit so Algorithm 1 can choose deepest/shallowest parents + * even when the SQL spelled a star rather than a clique. Never merge + * through a protected outer-join edge. */ + for (const auto &group : mgr->groups) + { + std::vector keys; + for (const auto &vertex : mgr->vertices) + for (const auto &key : vertex.join_keys) + if (RptFindKeyRoot(key, mgr->uf_parent) == group.first) + keys.push_back(key); + for (size_t i = 0; i < keys.size(); ++i) + for (size_t j = i + 1; j < keys.size(); ++j) + { + if (keys[i].rtindex == keys[j].rtindex) + continue; + RptEdgeInfo edge; + edge.left_rtindex = keys[i].rtindex; + edge.right_rtindex = keys[j].rtindex; + edge.key_type = group.second.key_type; + edge.protect_left = edge.protect_right = false; + edge.clauses.push_back(std::make_pair(keys[i], keys[j])); + const auto &existing_row = mgr->neighbor_matrix.find(edge.left_rtindex); + if (existing_row != mgr->neighbor_matrix.end()) + { + const auto &existing = existing_row->second.find(edge.right_rtindex); + if (existing != existing_row->second.end()) + { + bool reversed = existing->second.left_rtindex != edge.left_rtindex; + auto clause = reversed ? std::make_pair(keys[j], keys[i]) : + std::make_pair(keys[i], keys[j]); + if (std::find(existing->second.clauses.begin(), + existing->second.clauses.end(), clause) != + existing->second.clauses.end()) + continue; + } + } + rpt_upsert_edge(mgr, edge); + } + } + + rpt_classify_tables(mgr); + mgr->logical_neighbor_matrix = mgr->neighbor_matrix; + rpt_skip_unfiltered(mgr); +} diff --git a/gpcontrib/robust_predicate_transfer/src/rptinit.cpp b/gpcontrib/robust_predicate_transfer/src/rptinit.cpp new file mode 100644 index 00000000000..f17e91477d8 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rptinit.cpp @@ -0,0 +1,513 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptinit.cpp + * Module entry point: GUCs, the planner hook, and the SQL-visible + * transfer-plan dump function. + * + * The extension rides on Anser (gpcontrib/anser), which must be preloaded + * first: the AnserXxx entry points we call are resolved at load time from + * the global symbol table (dfmgr loads modules RTLD_GLOBAL). + * + * The planner hook injects forward and backward filters and logs decisions at + * DEBUG1 when rpt.debug is on. rpt_transfer_plan() exposes analysis without + * injecting filters. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/src/rptinit.cpp + * + *------------------------------------------------------------------------- + */ +#include "rpt.h" + +extern "C" +{ +#include "postgres.h" + +#include "cdb/cdbvars.h" +#include "fmgr.h" +#include "miscadmin.h" +#include "nodes/pg_list.h" +#include "optimizer/planner.h" +#include "parser/analyze.h" +#include "tcop/tcopprot.h" +#include "utils/builtins.h" +#include "utils/guc.h" +#include "utils/memutils.h" + +#include "rptlocal.h" + +PG_MODULE_MAGIC; + +PG_FUNCTION_INFO_V1(rpt_transfer_plan); + +void _PG_init(void); +} + + +#include + +bool rpt_enable = false; +bool rpt_debug = false; +bool rpt_backward = true; +int rpt_max_backward_consumers = 4; +int rpt_max_chain_depth = 4; + +bool rpt_forward = true; +/* Measured 2026-11-10 on one host with three primaries. */ +double rpt_forward_ms_per_mib_part = 3.5; +double rpt_delivery_ms_per_mib = 0.8; +double rpt_forward_max_exchange_ms = 100.0; +double rpt_forward_min_root_rows = 10000.0; +/* The RPT+ paper's lambda (its Algorithm 2 threshold). */ +double rpt_forward_max_selectivity = 0.35; + +static planner_hook_type prev_planner_hook = NULL; + +/* Set while rpt_transfer_plan() plans its query: the dump must describe the + * uninjected plan, so the hook leaves it alone. */ +static bool rpt_suppress_injection = false; + +static void rpt_define_gucs(void); +static PlannedStmt *rpt_planner(Query *parse, const char *query_string, + int cursorOptions, ParamListInfo boundParams, + OptimizerOptions *optimizer_options); + +/* Run analysis with C++ unwinding before reporting an error to C callers. */ +static void +rpt_analyze(PlannedStmt *stmt, StringInfo out) +{ + bool pg_error = false; + bool out_of_memory = false; + char message[256] = "unknown C++ exception"; + + try + { + RptTransferMgr mgr; + + RptCollectGraph(stmt, &mgr); + if (mgr.vertices.empty()) + RptPgCall([&]() { appendStringInfoString(out, "no transfer regions\n"); }); + else + { + RptBuildClasses(&mgr); + RptBuildTransferPlan(stmt, &mgr); + RptDumpTransferPlan(stmt, &mgr, out); + + /* Dry-run the configured runtime passes without changing the plan. */ + std::string injection_trace; + + (void) RptInject(stmt, false, &injection_trace); + RptPgCall([&]() { appendStringInfoString(out, injection_trace.c_str()); }); + } + return; + } + catch (const RptPgError &) + { + pg_error = true; + } + catch (const std::bad_alloc &) + { + out_of_memory = true; + } + catch (const std::exception &e) + { + strlcpy(message, e.what(), sizeof(message)); + } + catch (...) + { + } + + /* No live C++ objects (including exception objects) at these longjmps. */ + if (pg_error) + PG_RE_THROW(); + if (out_of_memory) + ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("out of memory during RPT analysis"))); + ereport(ERROR, (errmsg("RPT analysis failed: %s", message))); +} + +void +_PG_init(void) +{ + rpt_define_gucs(); + + /* + * The local reducer nodes travel to the segments inside dispatched plans, + * so every backend that loads us must be able to resolve them by name. + */ + RptRegisterLocalMethods(); + + /* + * Only a preloaded library can be relied on to have installed its hooks + * in every backend. Loaded any other way the extension stays inert: the + * GUCs exist (so a stray setting is not an error) and the dump function + * works, but nothing is wired into planning. + */ + if (!process_shared_preload_libraries_in_progress) + return; + + prev_planner_hook = planner_hook; + planner_hook = rpt_planner; + + MarkGUCPrefixReserved("rpt"); +} + +/* + * The extension's GUCs are "rpt.*"-qualified because they belong to a + * loadable module; "anser" is already reserved by the extension we ride on. + */ +static void +rpt_define_gucs(void) +{ + DefineCustomBoolVariable("rpt.enable", + "Enables RPT+ runtime filters over Anser.", + "When disabled the planner hook is a pass-through.", + &rpt_enable, + false, + PGC_USERSET, + GUC_GPDB_NEED_SYNC, + NULL, NULL, NULL); + + DefineCustomBoolVariable("rpt.debug", + "Logs the RPT+ transfer plan of every analyzed query.", + "Dumps regions, equivalence classes and the forward/backward transfer plan at DEBUG1.", + &rpt_debug, + false, + PGC_USERSET, + GUC_GPDB_NEED_SYNC, + NULL, NULL, NULL); + + DefineCustomBoolVariable("rpt.backward", + "Developer control for backward runtime-filter injection.", + "QD-only; only consumers proven safe to wait are injected.", + &rpt_backward, true, PGC_USERSET, GUC_NOT_IN_SAMPLE, + NULL, NULL, NULL); + + DefineCustomIntVariable("rpt.max_backward_consumers", + "Caps backward consumers per equivalence class.", + "QD-only; prefer consumers with larger estimated benefit.", + &rpt_max_backward_consumers, 4, 1, INT_MAX, + PGC_USERSET, 0, NULL, NULL, NULL); + + DefineCustomBoolVariable("rpt.forward", + "Developer control for forward reducer injection.", + "QD-only. Needs anser.enable=on and anser.runtime_filter=off.", + &rpt_forward, true, PGC_USERSET, GUC_NOT_IN_SAMPLE, + NULL, NULL, NULL); + + DefineCustomRealVariable("rpt.forward_ms_per_mib_part", + "Estimated collection cost per MiB of bitset per producer.", + "Shared by forward and backward exchanges; QD-only.", + &rpt_forward_ms_per_mib_part, 3.5, 0.0, 1.0e6, + PGC_USERSET, 0, NULL, NULL, NULL); + + DefineCustomRealVariable("rpt.delivery_ms_per_mib", + "Estimated delivery cost per consumer process per MiB.", + "Shared by forward and backward exchanges; QD-only.", + &rpt_delivery_ms_per_mib, 0.8, 0.0, 1.0e6, + PGC_USERSET, 0, NULL, NULL, NULL); + + DefineCustomRealVariable("rpt.forward_max_exchange_ms", + "Skips a forward chain or backward class above this exchange cost.", + "-1 disables the limit; QD-only.", + &rpt_forward_max_exchange_ms, 100.0, -1.0, 1.0e9, + PGC_USERSET, 0, NULL, NULL, NULL); + + DefineCustomRealVariable("rpt.forward_min_root_rows", + "Skips a forward chain whose root scan is estimated smaller.", + "Compared with the root scan's plan_rows (per segment); QD-only.", + &rpt_forward_min_root_rows, 10000.0, 0.0, 1.0e15, + PGC_USERSET, 0, NULL, NULL, NULL); + + DefineCustomRealVariable("rpt.forward_max_selectivity", + "Skips transfers expected to keep at least this fraction of target rows.", + "Shared selectivity threshold for forward roots and backward members; 1 disables the check; QD-only.", + &rpt_forward_max_selectivity, 0.35, 0.0, 1.0, + PGC_USERSET, 0, NULL, NULL, NULL); + + DefineCustomIntVariable("rpt.max_chain_depth", + "Sets the maximum forward-chain length RPT+ may plan.", + "Global hops require publish/merge/deliver barriers, so chain depth is capped; the default is a placeholder until hop latency is measured.", + &rpt_max_chain_depth, + 4, 1, INT_MAX, + PGC_USERSET, + GUC_GPDB_NEED_SYNC, + NULL, NULL, NULL); +} + +/* The diagnostic hook must not turn an optional analysis failure into a + * failed user query. No C++ owners live across this PostgreSQL catch: the + * analysis boundary has already unwound them before rethrowing the error. */ +static void +rpt_log_analysis(PlannedStmt *stmt) +{ + MemoryContext saved_context = CurrentMemoryContext; + MemoryContext volatile analysis_context = NULL; + + PG_TRY(); + { + analysis_context = AllocSetContextCreate(saved_context, + "RPT analysis", + ALLOCSET_DEFAULT_SIZES); + MemoryContextSwitchTo(analysis_context); + StringInfoData buf; + + initStringInfo(&buf); + rpt_analyze(stmt, &buf); + elog(DEBUG1, "rpt transfer plan:\n%s", buf.data); + MemoryContextSwitchTo(saved_context); + } + PG_CATCH(); + { + MemoryContextSwitchTo(saved_context); + ErrorData *error = CopyErrorData(); + + FlushErrorState(); + if (analysis_context != NULL) + { + MemoryContextDelete(analysis_context); + analysis_context = NULL; + } + /* An optional pass must still honor the user's cancellation. */ + if (error->sqlerrcode == ERRCODE_QUERY_CANCELED) + ReThrowError(error); + ereport(WARNING, (errmsg("RPT analysis skipped: %s", error->message))); + FreeErrorData(error); + } + PG_END_TRY(); + + if (analysis_context != NULL) + MemoryContextDelete(analysis_context); +} + +/* Contain C++ exceptions before returning to the PostgreSQL error handler. */ +static PlannedStmt * +rpt_build_injection(PlannedStmt *stmt) +{ + bool pg_error = false; + bool out_of_memory = false; + char message[256] = "unknown C++ exception"; + + try + { + std::string trace; + PlannedStmt *result = RptInject(stmt, true, &trace); + + if (rpt_debug) + RptPgCall([&]() { elog(DEBUG1, "rpt injection:\n%s", trace.c_str()); }); + return result; + } + catch (const RptPgError &) + { + pg_error = true; + } + catch (const std::bad_alloc &) + { + out_of_memory = true; + } + catch (const std::exception &e) + { + strlcpy(message, e.what(), sizeof(message)); + } + catch (...) + { + } + if (pg_error) + PG_RE_THROW(); + if (out_of_memory) + ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("out of memory building the RPT runtime filters"))); + ereport(ERROR, (errmsg("RPT runtime filters failed: %s", message))); + return NULL; +} + +/* The original plan survives construction failure, reported as a WARNING; + * only cancellation propagates. */ +static PlannedStmt * +rpt_try_injection(PlannedStmt *stmt) +{ + MemoryContext saved_context = CurrentMemoryContext; + MemoryContext volatile build_context = NULL; + PlannedStmt *volatile result = stmt; + + PG_TRY(); + { + build_context = AllocSetContextCreate(saved_context, + "RPT runtime filters", + ALLOCSET_DEFAULT_SIZES); + MemoryContextSwitchTo(build_context); + result = rpt_build_injection(stmt); + MemoryContextSwitchTo(saved_context); + /* Nothing injected: the scratch context holds only garbage. */ + if (result == stmt) + { + MemoryContextDelete(build_context); + build_context = NULL; + } + } + PG_CATCH(); + { + MemoryContextSwitchTo(saved_context); + ErrorData *error = CopyErrorData(); + + FlushErrorState(); + if (build_context != NULL) + MemoryContextDelete(build_context); + if (error->sqlerrcode == ERRCODE_QUERY_CANCELED) + ReThrowError(error); + ereport(WARNING, (errmsg("RPT runtime filters skipped: %s", error->message))); + FreeErrorData(error); + result = stmt; + } + PG_END_TRY(); + + return result; +} + +/* + * Plan as usual, then run the analysis over the finished tree. Chaining + * prev first means that when Anser is preloaded before us its injection pass + * has already run on the tree we analyze, which is what we want: the region + * discovery sees through Anser's own filter nodes. + */ +static PlannedStmt * +rpt_planner(Query *parse, const char *query_string, int cursorOptions, + ParamListInfo boundParams, OptimizerOptions *optimizer_options) +{ + PlannedStmt *result; + + if (prev_planner_hook) + result = prev_planner_hook(parse, query_string, cursorOptions, + boundParams, optimizer_options); + else + result = standard_planner(parse, query_string, cursorOptions, + boundParams, optimizer_options); + + if (rpt_enable && rpt_debug && + Gp_role == GP_ROLE_DISPATCH && + result != NULL && result->commandType == CMD_SELECT && + result->planTree != NULL) + rpt_log_analysis(result); + + if (rpt_enable && (rpt_forward || rpt_backward) && !rpt_suppress_injection && + Gp_role == GP_ROLE_DISPATCH && + result != NULL && result->commandType == CMD_SELECT && + result->planTree != NULL && result->intoClause == NULL && + result->copyIntoClause == NULL && result->refreshClause == NULL) + result = rpt_try_injection(result); + + return result; +} + +/* + * rpt_transfer_plan(query text) -> text + * + * Plan the query and dump its RPT+ transfer plan. Analysis only: the plan + * is discarded afterwards. This is the regression-test driver. + */ +static Datum +rpt_transfer_plan_impl(FunctionCallInfo fcinfo) +{ + char *query_string = text_to_cstring(PG_GETARG_TEXT_PP(0)); + List *raw_parsetree_list; + List *query_list; + Query *query; + PlannedStmt *stmt; + StringInfoData buf; + + raw_parsetree_list = pg_parse_query(query_string); + if (list_length(raw_parsetree_list) != 1) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("rpt_transfer_plan expects a single statement"))); + + query_list = pg_analyze_and_rewrite_fixedparams( + (RawStmt *) linitial(raw_parsetree_list), + query_string, NULL, 0, NULL); + if (list_length(query_list) != 1) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("rpt_transfer_plan expects a single query"))); + + query = (Query *) linitial(query_list); + if (query->commandType != CMD_SELECT) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("rpt_transfer_plan supports only SELECT"))); + + /* + * pg_plan_query runs the hook chain, so with anser preloaded and its + * runtime_filter GUC on the tree we analyze already carries Anser's + * injected nodes -- exactly what production planning will hand us. + */ + /* The dump describes the uninjected plan; the forward decisions are + * appended by a dry run inside rpt_analyze(). */ + rpt_suppress_injection = true; + PG_TRY(); + { + stmt = pg_plan_query(query, query_string, 0, NULL); + } + PG_FINALLY(); + { + rpt_suppress_injection = false; + } + PG_END_TRY(); + + initStringInfo(&buf); + rpt_analyze(stmt, &buf); + + PG_RETURN_TEXT_P(cstring_to_text(buf.data)); +} + +/* The SQL entry point also contains exceptions raised by future UDF code. */ +Datum +rpt_transfer_plan(PG_FUNCTION_ARGS) +{ + bool pg_error = false; + bool out_of_memory = false; + char message[256] = "unknown C++ exception"; + + try + { + return rpt_transfer_plan_impl(fcinfo); + } + catch (const RptPgError &) + { + pg_error = true; + } + catch (const std::bad_alloc &) + { + out_of_memory = true; + } + catch (const std::exception &e) + { + strlcpy(message, e.what(), sizeof(message)); + } + catch (...) + { + } + if (pg_error) + PG_RE_THROW(); + if (out_of_memory) + ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("out of memory while planning RPT query"))); + ereport(ERROR, (errmsg("RPT query planning failed: %s", message))); + PG_RETURN_NULL(); +} diff --git a/gpcontrib/robust_predicate_transfer/src/rptinject.cpp b/gpcontrib/robust_predicate_transfer/src/rptinject.cpp new file mode 100644 index 00000000000..376ed95bdb8 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rptinject.cpp @@ -0,0 +1,119 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptinject.cpp + * Finished-plan construction of the distributed forward InitPlan. + * + *------------------------------------------------------------------------- + */ +#include "rpt.h" + +extern "C" +{ +#include "postgres.h" +#include "catalog/pg_type.h" +#include "nodes/makefuncs.h" +#include "anserplan.h" +} + + +/* The caller owns a private copy of the statement. This function may throw; + * publish that copy only after the entire injection pass has succeeded. + * Children must be non-parallel reducer producers that return NULL at EOF. + * The final empty Result makes EXISTS drain the Sequence completely. */ +void +RptAttachReducerInitPlan(PlannedStmt *stmt, List *children, int width) +{ + RptPgCall([&]() { + if (children == NIL || !stmt->planTree || width <= 0 || + stmt->numSlices <= 0 || !stmt->slices || + (stmt->subplans != NIL && !stmt->subplan_sliceIds)) + elog(ERROR, "invalid RPT reducer InitPlan input"); + + int max_plan_node_id = AnserMaxPlanNodeId(stmt); + ListCell *lc; + foreach(lc, children) + { + PlannedStmt child_stmt = {}; + child_stmt.planTree = (Plan *) lfirst(lc); + max_plan_node_id = Max(max_plan_node_id, AnserMaxPlanNodeId(&child_stmt)); + } + int next_plan_node_id = max_plan_node_id + 1; + int root_slice_index = stmt->numSlices; + int sender_slice_index = root_slice_index + 1; + int old_subplans = list_length(stmt->subplans); + int param_exec_id = list_length(stmt->paramExecTypes); + Flow *distributed = makeNode(Flow); + distributed->flotype = FLOW_PARTITIONED; + distributed->locustype = CdbLocusType_Strewn; + distributed->numsegments = width; + Result *empty = makeNode(Result); + empty->plan.plan_node_id = next_plan_node_id++; + empty->plan.flow = distributed; + /* ExecInitResult passes this field to ExecInitQual as implicit AND. */ + empty->resconstantqual = (Node *) list_make1(makeBoolConst(false, false)); + Sequence *sequence = makeNode(Sequence); + sequence->plan.plan_node_id = next_plan_node_id++; + sequence->plan.flow = distributed; + sequence->subplans = lappend(list_copy(children), empty); + Motion *gather = makeNode(Motion); + gather->plan.plan_node_id = next_plan_node_id++; + gather->plan.lefttree = (Plan *) sequence; + gather->motionType = MOTIONTYPE_GATHER; + gather->motionID = sender_slice_index; + Flow *singleton = makeNode(Flow); + singleton->flotype = FLOW_SINGLETON; + singleton->locustype = CdbLocusType_Entry; + singleton->segindex = -1; + singleton->numsegments = 1; + gather->plan.flow = singleton; + + PlanSlice *slices = (PlanSlice *) palloc0( + sizeof(PlanSlice) * (stmt->numSlices + 2)); + memcpy(slices, stmt->slices, sizeof(PlanSlice) * stmt->numSlices); + slices[root_slice_index].sliceIndex = root_slice_index; + slices[root_slice_index].parentIndex = -1; + slices[root_slice_index].gangType = GANGTYPE_UNALLOCATED; + slices[root_slice_index].segindex = -1; + slices[sender_slice_index].sliceIndex = sender_slice_index; + slices[sender_slice_index].parentIndex = root_slice_index; + slices[sender_slice_index].gangType = GANGTYPE_PRIMARY_READER; + slices[sender_slice_index].segindex = 0; + slices[sender_slice_index].numsegments = width; + int *subplan_slice_indexes = (int *) palloc(sizeof(int) * (old_subplans + 1)); + if (old_subplans) + memcpy(subplan_slice_indexes, stmt->subplan_sliceIds, sizeof(int) * old_subplans); + subplan_slice_indexes[old_subplans] = root_slice_index; + + SubPlan *subplan = makeNode(SubPlan); + subplan->subLinkType = EXISTS_SUBLINK; + subplan->plan_id = old_subplans + 1; + subplan->plan_name = psprintf("InitPlan %d", subplan->plan_id); + subplan->firstColType = VOIDOID; + subplan->firstColTypmod = -1; + subplan->is_initplan = true; + subplan->setParam = list_make1_int(param_exec_id); + stmt->subplans = lappend(stmt->subplans, gather); + stmt->paramExecTypes = lappend_oid(stmt->paramExecTypes, BOOLOID); + stmt->planTree->initPlan = lappend(stmt->planTree->initPlan, subplan); + stmt->subplan_sliceIds = subplan_slice_indexes; + stmt->slices = slices; + stmt->numSlices += 2; + }); +} diff --git a/gpcontrib/robust_predicate_transfer/src/rptlocal.c b/gpcontrib/robust_predicate_transfer/src/rptlocal.c new file mode 100644 index 00000000000..36a6c91dc76 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rptlocal.c @@ -0,0 +1,431 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptlocal.c + * In-process bloom handoff for local reducer hops. + * + * A local hop joins co-located tables: every row of the destination that can + * match a row of the source lives on the same segment as that row. So the + * source's filter on segment i is complete for the destination on segment i, + * and the hop needs no coordinator. Within the reducer both scans run in one + * QE process, as children of one Sequence: the producer's child is exhausted + * before the consumer's scan starts. The producer leaves its filter in a + * per-execution registry; the consumer picks it up there. + * + * Only hops consumed inside the reducer use this. A filter the main plan + * applies goes through Anser, because the main plan is a different dispatch. + * + * Failing open: a consumer that finds no completed filter passes every row. + * The registry entry lives in the executor's query context and unregisters + * itself when that context goes, so an aborted execution leaves nothing behind. + * + * Keys are hashed as raw Datums, exactly as Anser does: both sides have the + * same by-value type, whose equality is bitwise (rptregion.cpp). + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/src/rptlocal.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "ansertlist.h" + +#include "commands/explain.h" +#include "executor/executor.h" +#include "lib/bloomfilter.h" +#include "lib/ilist.h" +#include "miscadmin.h" +#include "nodes/extensible.h" +#include "nodes/makefuncs.h" +#include "nodes/nodeFuncs.h" +#include "utils/memutils.h" + +#include "rptlocal.h" + +/* custom_private layout */ +#define RPT_LOCAL_PRIV_CONDITION 0 +#define RPT_LOCAL_PRIV_KEY_ATTNO 1 +#define RPT_LOCAL_PRIV_TOTAL_ELEMS 2 + +/* One filter of one execution. */ +typedef struct RptLocalEntry +{ + dlist_node node; + EState *estate; + uint32 condition_id; + bloom_filter *filter; + bool complete; /* the producer's child was exhausted */ + MemoryContextCallback unregister; +} RptLocalEntry; + +static dlist_head rpt_local_entries = DLIST_STATIC_INIT(rpt_local_entries); + +typedef struct RptLocalScanState +{ + CustomScanState csstate; + uint32 condition_id; + AttrNumber key_attno; + int64 total_elems; + bool started; + RptLocalEntry *entry; /* producer: where it adds; consumer: what it probes */ +} RptLocalScanState; + +static Node *rpt_local_create_producer(CustomScan *cscan); +static Node *rpt_local_create_consumer(CustomScan *cscan); +static void rpt_local_begin(CustomScanState *node, EState *estate, int eflags); +static TupleTableSlot *rpt_local_produce_exec(CustomScanState *node); +static TupleTableSlot *rpt_local_consume_exec(CustomScanState *node); +static void rpt_local_end(CustomScanState *node); +static void rpt_local_rescan(CustomScanState *node); +static void rpt_local_explain(CustomScanState *node, List *ancestors, + ExplainState *es); + +static const CustomScanMethods rpt_local_producer_methods = { + .CustomName = "RPT Local Bloom Producer", + .CreateCustomScanState = rpt_local_create_producer, +}; + +static const CustomScanMethods rpt_local_consumer_methods = { + .CustomName = "RPT Local Bloom Consumer", + .CreateCustomScanState = rpt_local_create_consumer, +}; + +static const CustomExecMethods rpt_local_producer_exec = { + .CustomName = "RPT Local Bloom Producer", + .BeginCustomScan = rpt_local_begin, + .ExecCustomScan = rpt_local_produce_exec, + .EndCustomScan = rpt_local_end, + .ReScanCustomScan = rpt_local_rescan, + .ExplainCustomScan = rpt_local_explain, +}; + +static const CustomExecMethods rpt_local_consumer_exec = { + .CustomName = "RPT Local Bloom Consumer", + .BeginCustomScan = rpt_local_begin, + .ExecCustomScan = rpt_local_consume_exec, + .EndCustomScan = rpt_local_end, + .ReScanCustomScan = rpt_local_rescan, + .ExplainCustomScan = rpt_local_explain, +}; + +void +RptRegisterLocalMethods(void) +{ + RegisterCustomScanMethods(&rpt_local_producer_methods); + RegisterCustomScanMethods(&rpt_local_consumer_methods); +} + +bool +RptIsLocalScan(const Plan *plan) +{ + if (plan == NULL || !IsA(plan, CustomScan)) + return false; + return ((const CustomScan *) plan)->methods == &rpt_local_producer_methods || + ((const CustomScan *) plan)->methods == &rpt_local_consumer_methods; +} + +/* ---- plan construction ---- */ + +/* Pass-through targetlist over the child's output, as Anser's nodes use. */ +static List * +rpt_local_identity_tlist(List *child_tlist) +{ + List *tlist = NIL; + ListCell *lc; + AttrNumber attno = 0; + + foreach(lc, child_tlist) + { + TargetEntry *tle = (TargetEntry *) lfirst(lc); + + attno++; + tlist = lappend(tlist, + makeTargetEntry((Expr *) makeVar(INDEX_VAR, attno, + exprType((Node *) tle->expr), + exprTypmod((Node *) tle->expr), + exprCollation((Node *) tle->expr), + 0), + attno, + tle->resname ? pstrdup(tle->resname) : NULL, + tle->resjunk)); + } + return tlist; +} + +static CustomScan * +rpt_local_build(const CustomScanMethods *methods, Plan *child, + AttrNumber key_attno, uint32 condition_id, int64 total_elems) +{ + CustomScan *cs = makeNode(CustomScan); + + cs->custom_private = list_make3(makeInteger((int) condition_id), + makeInteger((int) key_attno), + makeInteger((int) Min(total_elems, PG_INT32_MAX))); + cs->scan.plan.targetlist = rpt_local_identity_tlist(child->targetlist); + cs->scan.plan.startup_cost = child->startup_cost; + cs->scan.plan.total_cost = child->total_cost; + cs->scan.plan.plan_rows = child->plan_rows; + cs->scan.plan.plan_width = child->plan_width; + cs->scan.plan.parallel_aware = false; + cs->scan.plan.parallel_safe = false; + cs->scan.plan.flow = (Flow *) copyObject(child->flow); + cs->scan.scanrelid = 0; + cs->custom_plans = list_make1(child); + cs->custom_scan_tlist = AnserFilterScanTlist(child); + cs->methods = methods; + return cs; +} + +CustomScan * +RptBuildLocalProducerScan(Plan *child, AttrNumber key_attno, + uint32 condition_id, int64 total_elems) +{ + return rpt_local_build(&rpt_local_producer_methods, child, key_attno, + condition_id, total_elems); +} + +CustomScan * +RptBuildLocalConsumerScan(Plan *child, AttrNumber key_attno, + uint32 condition_id, int64 total_elems) +{ + return rpt_local_build(&rpt_local_consumer_methods, child, key_attno, + condition_id, total_elems); +} + +/* ---- registry ---- */ + +static void +rpt_local_unregister(void *arg) +{ + dlist_delete(&((RptLocalEntry *) arg)->node); +} + +static RptLocalEntry * +rpt_local_find(EState *estate, uint32 condition_id) +{ + dlist_iter iter; + + dlist_foreach(iter, &rpt_local_entries) + { + RptLocalEntry *e = dlist_container(RptLocalEntry, node, iter.cur); + + if (e->estate == estate && e->condition_id == condition_id) + return e; + } + return NULL; +} + +/* ---- execution ---- */ + +static Node * +rpt_local_create_state(CustomScan *cscan, const CustomExecMethods *exec) +{ + RptLocalScanState *st = (RptLocalScanState *) + newNode(sizeof(RptLocalScanState), T_CustomScanState); + + st->csstate.methods = exec; + return (Node *) st; +} + +static Node * +rpt_local_create_producer(CustomScan *cscan) +{ + return rpt_local_create_state(cscan, &rpt_local_producer_exec); +} + +static Node * +rpt_local_create_consumer(CustomScan *cscan) +{ + return rpt_local_create_state(cscan, &rpt_local_consumer_exec); +} + +static void +rpt_local_begin(CustomScanState *node, EState *estate, int eflags) +{ + RptLocalScanState *st = (RptLocalScanState *) node; + CustomScan *cscan = (CustomScan *) node->ss.ps.plan; + + st->condition_id = (uint32) intVal(list_nth(cscan->custom_private, RPT_LOCAL_PRIV_CONDITION)); + st->key_attno = (AttrNumber) intVal(list_nth(cscan->custom_private, RPT_LOCAL_PRIV_KEY_ATTNO)); + st->total_elems = intVal(list_nth(cscan->custom_private, RPT_LOCAL_PRIV_TOTAL_ELEMS)); + st->started = false; + st->entry = NULL; + + /* Nothing is allocated until the node runs: the coordinator initializes + * every slice, and EXPLAIN initializes without running. */ + node->custom_ps = list_make1(ExecInitNode((Plan *) linitial(cscan->custom_plans), + estate, eflags)); +} + +static TupleTableSlot * +rpt_local_pass(CustomScanState *node, TupleTableSlot *childslot) +{ + ExecCopySlot(node->ss.ss_ScanTupleSlot, childslot); + return node->ss.ss_ScanTupleSlot; +} + +static bool +rpt_local_recheck(CustomScanState *node, TupleTableSlot *slot) +{ + return true; +} + +static TupleTableSlot * +rpt_local_produce_next(CustomScanState *node) +{ + RptLocalScanState *st = (RptLocalScanState *) node; + PlanState *child = (PlanState *) linitial(node->custom_ps); + TupleTableSlot *slot; + + if (!st->started) + { + EState *estate = node->ss.ps.state; + + st->started = true; + st->entry = rpt_local_find(estate, st->condition_id); + + /* + * A rescan keeps the entry and adds to the same filter: the result + * is a superset of the keys of every pass, which is all a consumer + * needs. + */ + if (st->entry == NULL) + { + MemoryContext oldcxt = MemoryContextSwitchTo(estate->es_query_cxt); + RptLocalEntry *e = (RptLocalEntry *) palloc0(sizeof(RptLocalEntry)); + + e->estate = estate; + e->condition_id = st->condition_id; + e->filter = bloom_create(Max(st->total_elems, 1), work_mem, 0); + e->unregister.func = rpt_local_unregister; + e->unregister.arg = e; + MemoryContextRegisterResetCallback(estate->es_query_cxt, &e->unregister); + dlist_push_head(&rpt_local_entries, &e->node); + MemoryContextSwitchTo(oldcxt); + st->entry = e; + } + } + + slot = ExecProcNode(child); + if (TupIsNull(slot)) + { + st->entry->complete = true; + return NULL; + } + + { + bool isnull; + Datum value = slot_getattr(slot, st->key_attno, &isnull); + + if (!isnull) + bloom_add_element(st->entry->filter, (unsigned char *) &value, + sizeof(Datum)); + } + return rpt_local_pass(node, slot); +} + +static TupleTableSlot * +rpt_local_produce_exec(CustomScanState *node) +{ + return ExecScan(&node->ss, (ExecScanAccessMtd) rpt_local_produce_next, + (ExecScanRecheckMtd) rpt_local_recheck); +} + +static TupleTableSlot * +rpt_local_consume_next(CustomScanState *node) +{ + RptLocalScanState *st = (RptLocalScanState *) node; + PlanState *child = (PlanState *) linitial(node->custom_ps); + + if (!st->started) + { + RptLocalEntry *e = rpt_local_find(node->ss.ps.state, st->condition_id); + + st->started = true; + st->entry = (e != NULL && e->complete) ? e : NULL; + } + + for (;;) + { + TupleTableSlot *slot = ExecProcNode(child); + bool isnull; + Datum value; + + if (TupIsNull(slot)) + return NULL; + if (st->entry == NULL) + return rpt_local_pass(node, slot); /* fail open */ + + /* NULLs never match an equijoin key; let the join handle them. */ + value = slot_getattr(slot, st->key_attno, &isnull); + if (!isnull) + { + InstrCountFiltered1(node, 1); + if (bloom_lacks_element(st->entry->filter, (unsigned char *) &value, + sizeof(Datum))) + { + InstrCountFiltered2(node, 1); + continue; + } + } + return rpt_local_pass(node, slot); + } +} + +static TupleTableSlot * +rpt_local_consume_exec(CustomScanState *node) +{ + return ExecScan(&node->ss, (ExecScanAccessMtd) rpt_local_consume_next, + (ExecScanRecheckMtd) rpt_local_recheck); +} + +static void +rpt_local_end(CustomScanState *node) +{ + /* The entry belongs to the query context, not to this node. */ + ((RptLocalScanState *) node)->entry = NULL; + if (node->custom_ps != NIL) + ExecEndNode((PlanState *) linitial(node->custom_ps)); +} + +static void +rpt_local_rescan(CustomScanState *node) +{ + RptLocalScanState *st = (RptLocalScanState *) node; + + st->started = false; + st->entry = NULL; + if (node->custom_ps != NIL) + ExecReScan((PlanState *) linitial(node->custom_ps)); +} + +static void +rpt_local_explain(CustomScanState *node, List *ancestors, ExplainState *es) +{ + RptLocalScanState *st = (RptLocalScanState *) node; + + ExplainPropertyInteger("Local Bloom Condition", NULL, st->condition_id, es); + if (es->analyze && node->ss.ps.instrument != NULL && + node->methods == &rpt_local_consumer_exec && + node->ss.ps.instrument->nloops > 0) + ExplainPropertyFloat("Rows Removed by Local Bloom Filter", NULL, + node->ss.ps.instrument->nfiltered2 / + node->ss.ps.instrument->nloops, 0, es); +} diff --git a/gpcontrib/robust_predicate_transfer/src/rptplan.cpp b/gpcontrib/robust_predicate_transfer/src/rptplan.cpp new file mode 100644 index 00000000000..2862f13a001 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rptplan.cpp @@ -0,0 +1,808 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptplan.cpp + * The asymmetric transfer plan: a Prim spanning forest + * (LargestRootUpdated), forward/backward stage assignment + * (CreateTransferPlanUpdated), backward-consumer eligibility, and + * the deterministic text dump that feeds the regression tests. + * + * Ported from the RPT+ artifact (embryo-labs/dynamic-predicate-transfer), + * src/optimizer/predicate_transfer/transfer_graph_manager.cpp and dag.cpp -- + * MIT license, Copyright 2018-2025 Stichting DuckDB Foundation (see + * licenses/LICENSE-mit-duckdb-rpt.txt): + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/src/rptplan.cpp + * + *------------------------------------------------------------------------- + */ +#include "rpt.h" + +extern "C" +{ +#include "postgres.h" + +#include "optimizer/walkers.h" +#include "parser/parsetree.h" +} + + +#include + +/* + * GraphNode::Add (dag.cpp:9-19): find-or-create the edge to `other` in the + * given stage and direction. + */ +static RptGraphEdge * +rpt_node_add(RptGraphNode *node, int other_rtindex, bool is_forward, bool is_in_edge) +{ + std::vector &edges = is_forward + ? (is_in_edge ? node->forward_stage.in : node->forward_stage.out) + : (is_in_edge ? node->backward_stage.in : node->backward_stage.out); + size_t i; + + for (i = 0; i < edges.size(); i++) + { + if (edges[i].other_rtindex == other_rtindex) + return &edges[i]; + } + edges.push_back(RptGraphEdge()); + edges.back().other_rtindex = other_rtindex; + return &edges.back(); +} + +/* GraphNode::Add with keys (dag.cpp:21-28). */ +static RptGraphEdge * +rpt_node_add_keys(RptGraphNode *node, int other_rtindex, + const std::vector &left_keys, + const std::vector &right_keys, + const std::vector &key_types, + bool is_forward, bool is_in_edge) +{ + RptGraphEdge *edge = rpt_node_add(node, other_rtindex, is_forward, is_in_edge); + + edge->left_keys = left_keys; + edge->right_keys = right_keys; + edge->key_types = key_types; + return edge; +} + +/* + * RegisterLeader (transfer_graph_manager.hpp:50-55): the first vertex of a + * class to enter the transfer order becomes the leader the backward + * broadcast builds its single filter on. + */ +static void +rpt_register_leader(RptTransferMgr *mgr, int rtindex) +{ + const RptVertexInfo &v = mgr->vertices[mgr->vertex_index_by_rtindex[rtindex]]; + std::set::const_iterator kit; + + for (kit = v.join_keys.begin(); kit != v.join_keys.end(); ++kit) + { + RptKey rep = RptFindKeyRoot(*kit, mgr->uf_parent); + std::map::iterator git = mgr->groups.find(rep); + + if (git != mgr->groups.end() && git->second.leader_rtindex == -1) + { + git->second.leader_rtindex = rtindex; + git->second.leader_key = *kit; + } + } +} + +/* + * Algorithm 1: choose the maximum-weight frontier vertex, break ties by + * cardinality, then select deepest forward / shallowest backward parents. + * Fully protected edges never participate. + */ +static bool +rpt_find_edge(const RptTransferMgr *mgr, const std::set &constructed, + const std::set &unconstructed, + const std::map &forward_depth, + const std::map &backward_depth, + int *forward_parent_rtindex, int *backward_parent_rtindex, int *target_rtindex) +{ + size_t best_weight = 0; + int64 best_rows = -1; + bool found = false; + auto edge_at = [&](int parent_rtindex, int candidate_rtindex, bool forward) + -> const RptEdgeInfo * { + const auto &matrix = forward || mgr->logical_neighbor_matrix.empty() + ? mgr->neighbor_matrix : mgr->logical_neighbor_matrix; + auto row = matrix.find(parent_rtindex); + if (row == matrix.end()) + return NULL; + auto edge = row->second.find(candidate_rtindex); + if (edge == row->second.end() || + (edge->second.protect_left && edge->second.protect_right)) + return NULL; + return &edge->second; + }; + + /* Grow one forest over both views. Forward pruning must not disconnect + * tables that can still receive a useful backward filter. */ + for (int candidate_rtindex : unconstructed) + for (int parent_rtindex : constructed) + for (bool forward : {true, false}) + { + const RptEdgeInfo *edge = edge_at(parent_rtindex, candidate_rtindex, forward); + if (edge == NULL) + continue; + size_t weight = edge->clauses.size(); + int64 rows = mgr->vertices[mgr->vertex_index_by_rtindex.at(candidate_rtindex)].cardinality; + if (!found || weight > best_weight || + (weight == best_weight && rows > best_rows)) + { + found = true; + best_weight = weight; + best_rows = rows; + *target_rtindex = candidate_rtindex; + } + } + if (!found) + return false; + + *forward_parent_rtindex = *backward_parent_rtindex = -1; + for (bool forward : {true, false}) + { + int *selected = forward ? forward_parent_rtindex : backward_parent_rtindex; + const auto &depth = forward ? forward_depth : backward_depth; + size_t best_parent_weight = 0; + for (int parent_rtindex : constructed) + { + const RptEdgeInfo *edge = edge_at(parent_rtindex, *target_rtindex, forward); + if (edge == NULL) + continue; + int source_rtindex = forward ? *target_rtindex : parent_rtindex; + bool open = edge->left_rtindex == source_rtindex + ? !edge->protect_right : !edge->protect_left; + size_t weight = edge->clauses.size(); + if (open && (*selected == -1 || weight > best_parent_weight || + (weight == best_parent_weight && + (forward ? depth.at(parent_rtindex) > depth.at(*selected) + : depth.at(parent_rtindex) < depth.at(*selected))))) + { + *selected = parent_rtindex; + best_parent_weight = weight; + } + } + } + return true; +} + +/* + * LargestRootUpdated (transfer_graph_manager.cpp:557-626): grow one + * component of the spanning forest by Prim's algorithm, rooted at the + * largest filtered-or-intermediate vertex, falling back to the largest + * vertex. sorted_rtindexes lists every vertex by ascending cardinality; only + * those still in `remaining` are candidates, and placed vertices are removed + * from it (the artifact instead erases from its operator map and can pick a + * stale root from its sorted list on the next component; keeping `remaining` + * explicit avoids that). + */ +static void +rpt_largest_root_updated(RptTransferMgr *mgr, std::set &remaining, + const std::vector &sorted_rtindexes, + std::vector > &forward_edges, + std::vector > &backward_edges, + int32 &next_order, bool largest_relation) +{ + std::map forward_depth; + std::map backward_depth; + std::set constructed_set; + std::set unconstructed_set; + std::vector::const_reverse_iterator rit; + std::set::iterator sit; + int root_rtindex = -1; + + /* Try to choose the largest filtered or intermediate table as the root */ + for (rit = sorted_rtindexes.rbegin(); rit != sorted_rtindexes.rend(); ++rit) + { + const RptVertexInfo &v = mgr->vertices[mgr->vertex_index_by_rtindex[*rit]]; + + if (remaining.count(*rit) && + (largest_relation || v.filtered || v.intermediate)) + { + root_rtindex = *rit; + break; + } + } + + /* If we cannot find it, all tables have no filter */ + if (root_rtindex == -1) + { + for (rit = sorted_rtindexes.rbegin(); rit != sorted_rtindexes.rend(); ++rit) + { + if (remaining.count(*rit)) + { + root_rtindex = *rit; + break; + } + } + } + Assert(root_rtindex != -1); + + /* Initialize nodes */ + for (sit = remaining.begin(); sit != remaining.end(); ++sit) + { + if (*sit == root_rtindex) + constructed_set.insert(*sit); + else + unconstructed_set.insert(*sit); + + mgr->graph[*sit].rtindex = *sit; + mgr->graph[*sit].cardinality_order = -1; + } + + /* Add root */ + mgr->root_rtindexes.insert(root_rtindex); + forward_depth[root_rtindex] = backward_depth[root_rtindex] = 0; + mgr->graph[root_rtindex].cardinality_order = next_order++; + mgr->transfer_order_rtindexes.push_back(root_rtindex); + rpt_register_leader(mgr, root_rtindex); + remaining.erase(root_rtindex); + + /* Build graph */ + while (!unconstructed_set.empty()) + { + int forward_parent_rtindex; + int backward_parent_rtindex; + int target_rtindex; + + if (!rpt_find_edge(mgr, constructed_set, unconstructed_set, + forward_depth, backward_depth, + &forward_parent_rtindex, &backward_parent_rtindex, &target_rtindex)) + break; + + if (forward_parent_rtindex != -1) + forward_edges.push_back(std::make_pair(forward_parent_rtindex, target_rtindex)); + if (backward_parent_rtindex != -1) + backward_edges.push_back(std::make_pair(backward_parent_rtindex, target_rtindex)); + forward_depth[target_rtindex] = forward_parent_rtindex == -1 ? 0 : forward_depth[forward_parent_rtindex] + 1; + backward_depth[target_rtindex] = backward_parent_rtindex == -1 ? 0 : backward_depth[backward_parent_rtindex] + 1; + mgr->graph[target_rtindex].cardinality_order = next_order++; + mgr->transfer_order_rtindexes.push_back(target_rtindex); + rpt_register_leader(mgr, target_rtindex); + + unconstructed_set.erase(target_rtindex); + constructed_set.insert(target_rtindex); + remaining.erase(target_rtindex); + } + + /* + * Whatever is still unconstructed is disconnected from this root; it + * stays in `remaining` and the caller wraps it in another component. + */ +} + +/* + * CreateTransferPlanUpdated (transfer_graph_manager.cpp:677-734): assign + * the selected edges to the forward and backward stages. The smaller table + * -- the one that entered the Prim order later -- is on the left; forward + * transfers flow from the smaller to the larger, backward transfers from + * the larger to the smaller, with backward edges redirected at the + * equivalence-class leader. + */ +static void +rpt_create_transfer_plan(RptTransferMgr *mgr, + const std::vector > &selected_edges, + bool forward) +{ + size_t i; + + for (i = 0; i < selected_edges.size(); i++) + { + std::map::iterator eit; + RptEdgeInfo *edge; + RptGraphNode *left_node; + RptGraphNode *right_node; + std::vector left_keys; + std::vector right_keys; + std::vector key_types; + bool protect_left; + bool protect_right; + size_t c; + + auto &matrix = forward || mgr->logical_neighbor_matrix.empty() + ? mgr->neighbor_matrix : mgr->logical_neighbor_matrix; + eit = matrix[selected_edges[i].first].find(selected_edges[i].second); + if (eit == matrix[selected_edges[i].first].end()) + continue; + edge = &eit->second; + + left_node = &mgr->graph[edge->left_rtindex]; + right_node = &mgr->graph[edge->right_rtindex]; + protect_left = edge->protect_left; + protect_right = edge->protect_right; + + for (c = 0; c < edge->clauses.size(); c++) + { + left_keys.push_back(edge->clauses[c].first); + right_keys.push_back(edge->clauses[c].second); + key_types.push_back(edge->key_type); + } + + /* smaller table is in the left */ + if (left_node->cardinality_order < right_node->cardinality_order) + { + std::swap(left_node, right_node); + std::swap(left_keys, right_keys); + std::swap(protect_left, protect_right); + } + + /* forward: from the smaller to the larger */ + if (forward && !protect_right && !mgr->root_rtindexes.count(left_node->rtindex) && + (mgr->vertices[mgr->vertex_index_by_rtindex[left_node->rtindex]].filtered || + mgr->bridge_rtindexes.count(left_node->rtindex))) + { + rpt_node_add_keys(left_node, right_node->rtindex, left_keys, right_keys, + key_types, true, false); + rpt_node_add_keys(right_node, left_node->rtindex, left_keys, right_keys, + key_types, true, true); + } + + /* backward: from the larger to the smaller */ + if (!forward && !protect_left) + { + /* + * Group-leader redirection (transfer_graph_manager.cpp:719-732): + * the backward edge is built on the class leader, which produces + * the single broadcast filter for the whole class. The + * artifact's edges are single-key; ours bucket the clauses by + * destination so a multi-key edge can fan out to one leader per + * clause class. + */ + std::map by_dest; + std::map::iterator dit; + + for (c = 0; c < left_keys.size(); c++) + { + RptKey rep = RptFindKeyRoot(right_keys[c], mgr->uf_parent); + std::map::iterator git = mgr->groups.find(rep); + int target_rtindex; + RptKey dest_key; + RptGraphEdge *bucket; + + if (git != mgr->groups.end() && git->second.leader_rtindex != -1) + { + target_rtindex = git->second.leader_rtindex; + dest_key = git->second.leader_key; + } + else + { + target_rtindex = right_node->rtindex; + dest_key = right_keys[c]; + } + + bucket = &by_dest[target_rtindex]; + bucket->other_rtindex = target_rtindex; + bucket->left_keys.push_back(left_keys[c]); + bucket->right_keys.push_back(dest_key); + bucket->key_types.push_back(key_types[c]); + } + + for (dit = by_dest.begin(); dit != by_dest.end(); ++dit) + { + const RptGraphEdge &bucket = dit->second; + + rpt_node_add_keys(left_node, bucket.other_rtindex, + bucket.left_keys, bucket.right_keys, + bucket.key_types, false, true); + rpt_node_add_keys(&mgr->graph[bucket.other_rtindex], left_node->rtindex, + bucket.left_keys, bucket.right_keys, + bucket.key_types, false, false); + } + } + } +} + +/* Locality is a property of the base scans' distribution policies, including + * for implied and bypass edges. A Motion elsewhere in the main plan does not + * change where the reducer reads a key. Parallel workers still need a + * segment-local fold (or a non-parallel reducer). */ +static bool +rpt_local_hop(const RptTransferMgr *mgr, int source_rtindex, const RptGraphEdge &hop) +{ + const auto &src = mgr->vertices[mgr->vertex_index_by_rtindex.at(source_rtindex)]; + const auto &dst = mgr->vertices[mgr->vertex_index_by_rtindex.at(hop.other_rtindex)]; + + /* A replicated destination needs the complete key set on every segment. */ + if (dst.replicated || src.numsegments <= 0 || dst.numsegments <= 0) + return false; + if (src.replicated) + return src.numsegments >= dst.numsegments; + if (src.numsegments != dst.numsegments || src.distkeys.empty() || + src.distkeys.size() != dst.distkeys.size() || + src.distopclasses.size() != src.distkeys.size() || + src.distopclasses != dst.distopclasses) + return false; + for (size_t col = 0; col < src.distkeys.size(); ++col) + { + bool matched = false; + for (size_t key = 0; key < hop.left_keys.size(); ++key) + { + const RptKey &a = hop.left_keys[key]; + const RptKey &b = hop.right_keys[key]; + if ((a == RptKey{source_rtindex, src.distkeys[col]} && + b == RptKey{hop.other_rtindex, dst.distkeys[col]}) || + (b == RptKey{source_rtindex, src.distkeys[col]} && + a == RptKey{hop.other_rtindex, dst.distkeys[col]})) + matched = true; + } + if (!matched) + return false; + } + return true; +} + +/* Persist the decision on both copies of every stage edge. In/out edges + * carry the same key pairs; other_rtindex is the producer on an in-edge. */ +static void +rpt_classify_hops(RptTransferMgr *mgr) +{ + for (auto &node : mgr->graph) + { + for (bool forward : {true, false}) + { + auto &outs = forward ? node.second.forward_stage.out : node.second.backward_stage.out; + for (auto &edge : outs) + { + edge.local = rpt_local_hop(mgr, node.first, edge); + auto &target_rtindex = mgr->graph.at(edge.other_rtindex); + auto &ins = forward ? target_rtindex.forward_stage.in : target_rtindex.backward_stage.in; + for (auto &in : ins) + if (in.other_rtindex == node.first) + in.local = edge.local; + } + } + } +} + +/* + * Build the transfer plan for the join graph: the Prim forest, the + * forward/backward stage assignment, and the consumer-eligibility check. + */ +static void +rpt_build_transfer_plan(PlannedStmt *stmt, RptTransferMgr *mgr, + bool largest_relation) +{ + std::vector > forward_edges; + std::vector > backward_edges; + std::vector sorted_rtindexes; + std::set remaining; + int32 next_order = 0; + size_t i; + + /* vertices by ascending cardinality, ties by rtindex, for a stable plan */ + for (i = 0; i < mgr->vertices.size(); i++) + { + sorted_rtindexes.push_back(mgr->vertices[i].rtindex); + remaining.insert(mgr->vertices[i].rtindex); + } + std::sort(sorted_rtindexes.begin(), sorted_rtindexes.end(), + [&mgr] (int a, int b) + { + const RptVertexInfo &va = mgr->vertices[mgr->vertex_index_by_rtindex[a]]; + const RptVertexInfo &vb = mgr->vertices[mgr->vertex_index_by_rtindex[b]]; + + if (va.cardinality != vb.cardinality) + return va.cardinality < vb.cardinality; + return a < b; + }); + + /* + * The while-loop forest wrapping (transfer_graph_manager.cpp:677-683): + * repeat the Prim pass until every vertex is placed, giving a spanning + * forest -- one tree per connected component of the join graph. + */ + while (!remaining.empty()) + rpt_largest_root_updated(mgr, remaining, sorted_rtindexes, forward_edges, backward_edges, + next_order, largest_relation); + + for (const auto &group : mgr->groups) + if (group.second.leader_rtindex != -1 && !mgr->root_rtindexes.count(group.second.leader_rtindex)) + mgr->bridge_rtindexes.insert(group.second.leader_rtindex); + + rpt_create_transfer_plan(mgr, forward_edges, true); + rpt_create_transfer_plan(mgr, backward_edges, false); + + /* Transfers run in reverse insertion order. Cap actual dependency hops, + * keeping every inbound edge mirrored in its producer's out list. */ + std::map depth; + for (auto it = mgr->transfer_order_rtindexes.rbegin(); it != mgr->transfer_order_rtindexes.rend(); ++it) + { + auto &outs = mgr->graph[*it].forward_stage.out; + for (auto edge = outs.begin(); edge != outs.end();) + { + int target_rtindex = edge->other_rtindex; + if (depth[*it] >= rpt_max_chain_depth) + { + auto &ins = mgr->graph[target_rtindex].forward_stage.in; + ins.erase(std::remove_if(ins.begin(), ins.end(), + [&](const RptGraphEdge &in) { return in.other_rtindex == *it; }), ins.end()); + edge = outs.erase(edge); + } + else + { + depth[target_rtindex] = std::max(depth[target_rtindex], depth[*it] + 1); + ++edge; + } + } + if (!outs.empty()) + mgr->reducer_order_rtindexes.push_back(*it); + } + + rpt_classify_hops(mgr); + if (!largest_relation) + RptCheckBackwardConsumers(stmt, mgr); +} + +/* Backward analysis retains the artifact's filtered-root heuristic. */ +void +RptBuildTransferPlan(PlannedStmt *stmt, RptTransferMgr *mgr) +{ + rpt_build_transfer_plan(stmt, mgr, false); +} + +/* Runtime transfers use one largest-relation forest for both passes. + * Keep the filtered-root survey schedule separate for diagnostic continuity. + * Eligibility is evaluated by the caller against this forest before surgery. */ +void +RptBuildForwardPlan(const RptTransferMgr *analysis, RptTransferMgr *forward) +{ + *forward = *analysis; + forward->graph.clear(); + forward->root_rtindexes.clear(); + forward->bridge_rtindexes.clear(); + forward->reducer_order_rtindexes.clear(); + forward->transfer_order_rtindexes.clear(); + forward->consumer_ok_by_rtindex.clear(); + forward->backward_consumers.clear(); + for (auto &group : forward->groups) + group.second.leader_rtindex = -1; + rpt_build_transfer_plan(NULL, forward, true); + +} + +/* + * Dump support. Everything below iterates std::map / sorted copies only, + * so the output is deterministic and can feed expected-output regression + * tests. + */ + +static bool +rpt_edge_less(const RptGraphEdge &a, const RptGraphEdge &b) +{ + if (a.other_rtindex != b.other_rtindex) + return a.other_rtindex < b.other_rtindex; + if (a.left_keys != b.left_keys) + return a.left_keys < b.left_keys; + return a.right_keys < b.right_keys; +} + +static void +rpt_dump_key_pairs(std::ostream &out, const RptGraphEdge &edge) +{ + for (size_t i = 0; i < edge.left_keys.size(); i++) + { + if (i > 0) + out << ','; + out << edge.left_keys[i].rtindex << '.' << edge.left_keys[i].attno + << "->" << edge.right_keys[i].rtindex << '.' << edge.right_keys[i].attno; + } +} + +/* A stage's sorted build/apply entries, preserving the diagnostic format. */ +static void +rpt_dump_stage(std::ostream &out, int rtindex, const char *stage, + const std::vector &in_edges, + const std::vector &out_edges) +{ + std::vector outs(out_edges); + std::vector ins(in_edges); + bool any = false; + + std::sort(outs.begin(), outs.end(), rpt_edge_less); + std::sort(ins.begin(), ins.end(), rpt_edge_less); + out << "vertex " << rtindex << ' ' << stage << ':'; + for (const auto &edge : outs) + { + out << (any ? "; " : " ") << "build for " << edge.other_rtindex << " on ("; + rpt_dump_key_pairs(out, edge); + out << ')'; + any = true; + } + for (const auto &edge : ins) + { + out << (any ? "; " : " ") << "apply from " << edge.other_rtindex << " on ("; + rpt_dump_key_pairs(out, edge); + out << ')'; + any = true; + } + if (!any) + out << " none"; + out << '\n'; +} + +/* Build in C++ storage, then cross the PostgreSQL allocation boundary once. + * RptPgCall ensures a failed append unwinds the stream and string owners. */ +void +RptDumpTransferPlan(const PlannedStmt *stmt, const RptTransferMgr *mgr, StringInfo output) +{ + std::ostringstream out; + std::vector rtindexes; + + out.exceptions(std::ios::badbit | std::ios::failbit); + out.imbue(std::locale::classic()); + for (const auto &vertex : mgr->vertices) + rtindexes.push_back(vertex.rtindex); + std::sort(rtindexes.begin(), rtindexes.end()); + + out << "transfer order:"; + for (int rtindex : mgr->transfer_order_rtindexes) + out << ' ' << rtindex; + out << "\nforward depth limit: " << rpt_max_chain_depth << "\nreducer order:"; + for (int rtindex : mgr->reducer_order_rtindexes) + out << ' ' << rtindex; + out << "\nbridge tables:"; + for (int rtindex : mgr->bridge_rtindexes) + out << ' ' << rtindex; + out << '\n'; + + for (int rtindex : rtindexes) + { + const auto &v = mgr->vertices[mgr->vertex_index_by_rtindex.at(rtindex)]; + const char *alias = ""; + + /* Use the query alias, so self-join references remain distinct. No + * name is copied or looked up until a diagnostic dump is requested. */ + if (stmt != NULL && rtindex > 0 && rtindex <= list_length(stmt->rtable)) + { + RangeTblEntry *rte = rt_fetch(rtindex, stmt->rtable); + + if (rte->eref != NULL && rte->eref->aliasname != NULL) + alias = rte->eref->aliasname; + } + out << "vertex " << v.rtindex << ' ' << alias << " rows=" << v.cardinality + << ' ' << (v.filtered ? "filtered" : v.intermediate ? "intermediate" : "unfiltered") + << '\n'; + } + for (const auto &group : mgr->groups) + { + bool first = true; + + out << "class " << group.first.rtindex << '.' << group.first.attno << ": members="; + for (int member_rtindex : group.second.member_rtindexes) + { + if (!first) + out << ','; + out << member_rtindex; + first = false; + } + out << " leader=" << group.second.leader_rtindex << '\n'; + } + for (int rtindex : rtindexes) + { + const auto &node = mgr->graph.find(rtindex); + + if (node == mgr->graph.end()) + { + out << "vertex " << rtindex << " forward: none\n" + << "vertex " << rtindex << " backward: none\n"; + continue; + } + rpt_dump_stage(out, rtindex, "forward", node->second.forward_stage.in, + node->second.forward_stage.out); + rpt_dump_stage(out, rtindex, "backward", node->second.backward_stage.in, + node->second.backward_stage.out); + } + for (const auto &node : mgr->graph) + { + for (const auto &edge : node.second.forward_stage.out) + out << "forward hop " << node.first << "->" << edge.other_rtindex + << " merge=" << (edge.local ? "local" : "global") << '\n'; + for (const auto &edge : node.second.backward_stage.out) + out << "backward hop " << node.first << "->" << edge.other_rtindex + << " merge=" << (edge.local ? "local" : "global") << '\n'; + } + int global_edges = 0, rule9 = 0, strict = 0, eligible = 0, root_classes = 0; + int unknown_estimates = 0; + double rows_removed = 0; + for (const auto &group : mgr->groups) + if (mgr->root_rtindexes.count(group.second.leader_rtindex)) + ++root_classes; + for (const auto &decision : mgr->backward_consumers) + { + global_edges += !decision.local; + rule9 += decision.rule9; + strict += decision.strict_eligible; + eligible += decision.eligible; + if (decision.strict_eligible) + { + if (decision.estimated_rows_removed < 0) + ++unknown_estimates; + else + rows_removed += decision.estimated_rows_removed; + } + out << "backward consumer " << decision.consumer_rtindex << " producer " << decision.producer_rtindex + << " class " << decision.class_key.rtindex << '.' << decision.class_key.attno + << " merge=" << (decision.local ? "local" : "global") + << " rule9=" << (decision.rule9 ? "yes" : "no") + << " strict=" << (decision.strict_eligible ? "yes" : "no") + << " eligible=" << (decision.eligible ? "yes" : "no") + << " defer_first=" << (decision.defer_first ? "yes" : "no") + << " producer_slice=" << decision.producer_slice_index + << " consumer_slice=" << decision.consumer_slice_index + << " producer_attno=" << decision.producer_attno + << " consumer_attno=" << decision.consumer_attno + << " rows_removed="; + if (decision.estimated_rows_removed < 0) + out << "unknown"; + else + out << decision.estimated_rows_removed; + out << " reason=" << decision.reason << '\n'; + } + out << "survey classes=" << mgr->groups.size() << " root_classes=" << root_classes + << " global_edges=" << global_edges << " rule9=" << rule9 << " strict=" << strict + << " eligible=" << eligible << " known_rows_removed=" << rows_removed + << " unknown_estimates=" << unknown_estimates << '\n'; + for (int rtindex : rtindexes) + { + const auto &consumer = mgr->consumer_ok_by_rtindex.find(rtindex); + bool ok = consumer != mgr->consumer_ok_by_rtindex.end() && consumer->second; + out << "consumer_ok " << rtindex << ' ' << (ok ? "yes" : "no"); + if (!ok) + { + std::string reason = "no backward edge"; + for (const auto &decision : mgr->backward_consumers) + if (decision.consumer_rtindex == rtindex && !decision.eligible) + { + reason = decision.reason; + if (!decision.local) + break; + } + out << ": " << reason; + } + out << '\n'; + } + + std::string text = out.str(); + RptPgCall([&]() { appendStringInfoString(output, text.c_str()); }); +} diff --git a/gpcontrib/robust_predicate_transfer/src/rptregion.cpp b/gpcontrib/robust_predicate_transfer/src/rptregion.cpp new file mode 100644 index 00000000000..114c1280278 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rptregion.cpp @@ -0,0 +1,652 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptregion.cpp + * Extract the RPT+ join graph -- base-scan vertices and equijoin + * edges -- from a finished PlannedStmt. + * + * This pass replaces the RPT+ artifact's TableOperatorManager + * (embryo-labs/dynamic-predicate-transfer): where DuckDB builds its transfer + * graph from the logical operator tree before join ordering, we read the + * finished plan after set_plan_references, so the join clauses are already + * in OUTER_VAR/INNER_VAR form and the key columns must be traced back to + * their base scans. The traversal mirrors anser_inject_walk and the key + * descent and equality validation mirror AnserResolveKeyScan and + * anser_hashjoin_keys (all in gpcontrib/anser/src/anserplan.c), widened as + * noted at each site. + * + * Graph extraction does not modify the plan tree. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/src/rptregion.cpp + * + *------------------------------------------------------------------------- + */ +#include "rpt.h" + +extern "C" +{ +#include "postgres.h" + +#include "catalog/gp_distribution_policy.h" +#include "catalog/pg_class.h" +#include "catalog/pg_statistic.h" +#include "utils/syscache.h" +#include "nodes/parsenodes.h" +#include "nodes/pg_list.h" +#include "parser/parsetree.h" +#include "utils/fmgroids.h" +#include "utils/lsyscache.h" +#include "utils/typcache.h" + +#include "anserplan.h" +} + + +static bool rpt_is_vertex_scan(const Plan *plan); +static bool rpt_scan_is_filtered(const Plan *plan); +static RptVertexInfo *rpt_vertex_for_scan(PlannedStmt *stmt, + RptTransferMgr *mgr, Plan *scan); +static bool rpt_resolve_key_scan(Plan *top, AttrNumber key_attno, + Plan **scan_out, AttrNumber *attno_out); +static bool rpt_equality_clause(Node *clause, Var **outer_var, + Var **inner_var, Oid *key_type); +static void rpt_add_edge(PlannedStmt *stmt, RptTransferMgr *mgr, + JoinType jointype, + Plan *outer_plan, AttrNumber outer_attno, + Plan *inner_plan, AttrNumber inner_attno, + Oid key_type); +static void rpt_collect_clauses(PlannedStmt *stmt, RptTransferMgr *mgr, + JoinType jointype, Plan *outer, Plan *inner, + List *clauses); +static void rpt_walk(PlannedStmt *stmt, RptTransferMgr *mgr, Plan *plan); + +/* + * True for the scan nodes a reducer pass could replay, the only accepted + * vertices of the join graph. + */ +static bool +rpt_is_vertex_scan(const Plan *plan) +{ + return IsA(plan, SeqScan) || + IsA(plan, IndexScan) || + IsA(plan, IndexOnlyScan) || + IsA(plan, BitmapHeapScan); +} + +/* + * Does the scan carry restriction quals? Index and bitmap scans restrict + * through their index quals as well as through plan.qual. + */ +static bool +rpt_scan_is_filtered(const Plan *plan) +{ + if (plan->qual != NIL) + return true; + + switch (nodeTag(plan)) + { + case T_IndexScan: + return ((IndexScan *) plan)->indexqual != NIL; + case T_IndexOnlyScan: + return ((IndexOnlyScan *) plan)->indexqual != NIL; + case T_BitmapHeapScan: + return ((BitmapHeapScan *) plan)->bitmapqualorig != NIL; + default: + return false; + } +} + +/* + * Find or create the vertex for a base scan. One vertex per rangetable + * index, deduplicated through mgr->by_rti; vertices are created lazily, only + * when a resolved equijoin edge reaches the scan, so a scan no key resolves + * to never becomes a vertex. The classification fields (intermediate, + * unfiltered) are rptgraph.cpp's; join_keys accumulates here as edges are + * recorded. + */ +static RptVertexInfo * +rpt_vertex_for_scan(PlannedStmt *stmt, RptTransferMgr *mgr, Plan *scan) +{ + int rtindex = ((Scan *) scan)->scanrelid; + std::map::iterator it; + RptVertexInfo v; + + it = mgr->vertex_index_by_rtindex.find(rtindex); + if (it != mgr->vertex_index_by_rtindex.end()) + return &mgr->vertices[it->second]; + + v.rtindex = rtindex; + v.scan = scan; + v.cardinality = (int64) scan->plan_rows; + v.filtered = rpt_scan_is_filtered(scan); + v.intermediate = false; + v.unfiltered = false; + + if (rtindex >= 1 && rtindex <= list_length(stmt->rtable)) + { + RangeTblEntry *rte = rt_fetch(rtindex, stmt->rtable); + + if (rte->rtekind == RTE_RELATION) + { + v.relid = rte->relid; + GpPolicy *policy = RptPgCall([&]() { return GpPolicyFetch(rte->relid); }); + v.replicated = policy->ptype == POLICYTYPE_REPLICATED; + v.numsegments = policy->numsegments; + if (policy->ptype == POLICYTYPE_PARTITIONED && policy->nattrs > 0) + { + v.distkeys.assign(policy->attrs, policy->attrs + policy->nattrs); + v.distopclasses.assign(policy->opclasses, policy->opclasses + policy->nattrs); + } + RptPgCall([&]() { pfree(policy); }); + } + } + + mgr->vertex_index_by_rtindex[rtindex] = (int) mgr->vertices.size(); + mgr->vertices.push_back(v); + return &mgr->vertices.back(); +} + +/* + * Follow one join-key column down to the base scan that originates it. + * Modeled on AnserResolveKeyScan (gpcontrib/anser/src/anserplan.c) but not + * calling it: Anser's resolver stops at SeqScan and descends only Hash, + * Motion, HashJoin and its own nodes, where we accept the index and bitmap + * scans as termini as well and see through MergeJoin, NestLoop, Sort and + * Material too. Where Anser's loop stops at the parent of the scan (it + * needs the parent to relink the scan under a filter node), we only need the + * scan and its key attno, so the terminus test runs on the node itself: the + * descent steps into the scan, and the scan's own targetlist names its + * table's columns with varno == scanrelid after set_plan_references. + * + * False means no single base scan holds the whole key column -- the column + * comes from an Append, an Agg, a set operation, a SubqueryScan, a CTE, or + * anything else not on the descent list. That is a correctness gate, not a + * missed optimization: a filter built on a partial source of the column + * would reject rows that can join, so the caller drops the edge. + */ +static bool +rpt_resolve_key_scan(Plan *top, AttrNumber key_attno, + Plan **scan_out, AttrNumber *attno_out) +{ + Plan *node = top; + AttrNumber attno = key_attno; + + for (;;) + { + TargetEntry *tle; + Var *var; + Plan *child; + + if (node == NULL || + attno < 1 || attno > list_length(node->targetlist)) + return false; + + tle = (TargetEntry *) list_nth(node->targetlist, attno - 1); + if (tle == NULL || !IsA(tle->expr, Var)) + return false; + var = (Var *) tle->expr; + + if (IsA(node, IndexOnlyScan) && var->varno == INDEX_VAR) + { + IndexOnlyScan *ios = (IndexOnlyScan *) node; + + if (var->varattno < 1 || var->varattno > list_length(ios->indextlist)) + return false; + tle = (TargetEntry *) list_nth(ios->indextlist, var->varattno - 1); + if (!IsA(tle->expr, Var)) + return false; + var = (Var *) tle->expr; + } + + /* + * Terminus: a base scan the reducer could replay. The key column is + * the Var's attno in the scanned relation. A targetlist Var naming + * anything but the scan's own rangetable entry (a whole-row Var, a + * placeholder) is not a plain column and ends the descent. + */ + if (rpt_is_vertex_scan(node)) + { + Scan *scan = (Scan *) node; + + if (scan->scanrelid == 0 || var->varno != (int) scan->scanrelid) + return false; + *scan_out = node; + *attno_out = var->varattno; + return true; + } + + /* + * Which child supplies this column. As in AnserResolveKeyScan, a + * join is the only node here with two children; unlike it, all three + * join types are descended, not just HashJoin. An Anser filter node + * is a passthrough with an identity targetlist (its Vars carry + * INDEX_VAR into custom_plans), so a scan Anser already filtered + * stays reachable. + */ + if (var->varno == OUTER_VAR) + child = outerPlan(node); + else if (var->varno == INNER_VAR && + (IsA(node, HashJoin) || IsA(node, MergeJoin) || + IsA(node, NestLoop))) + child = innerPlan(node); + else if (var->varno == INDEX_VAR && AnserIsRuntimeFilterScan(node)) + child = (Plan *) linitial(((CustomScan *) node)->custom_plans); + else + return false; + + if (child == NULL) + return false; + + /* + * Anything not on this list is a region boundary: Append, + * MergeAppend, Agg, ProjectSet, SubqueryScan, CteScan, Unique, Limit, + * SetOp, WindowAgg, Result, the dynamic scans, and the rest. The + * walk descends into them to find joins, but a key column never + * crosses them. + */ + if (!rpt_is_vertex_scan(child) && + !IsA(child, Hash) && !IsA(child, Motion) && + !IsA(child, HashJoin) && !IsA(child, MergeJoin) && + !IsA(child, NestLoop) && + !IsA(child, Sort) && !IsA(child, Material) && + !AnserIsRuntimeFilterScan(child)) + return false; + + node = child; + attno = var->varattno; + } +} + +/* Raw Datum bytes are a valid Bloom key only for these canonical built-in + * types. A custom by-value type can equate distinct representations even + * through its own default equality operator. Domains are unwrapped first. */ +static bool +rpt_key_type_supported(Oid type) +{ + switch (type) + { + case BOOLOID: + case CHAROID: + case INT2OID: + case INT4OID: + case INT8OID: + case OIDOID: + case XIDOID: + case CIDOID: + case DATEOID: + case TIMEOID: + case TIMESTAMPOID: + case TIMESTAMPTZOID: + case MONEYOID: + case PG_LSNOID: + case XID8OID: + return true; + default: + return false; + } +} + +/* + * Validate one equijoin clause and split it into outer/inner Vars. + * + * The shape and the operator validation are exactly anser_hashjoin_keys's + * (gpcontrib/anser/src/anserplan.c), lifted from HashJoin->hashclauses to + * any clause list: a two-argument OpExpr whose RelabelType-stripped args are + * one OUTER_VAR and one INNER_VAR Var, over the same by-value base type on + * both sides, restricted to trusted built-ins, with opno equal to TYPECACHE_EQ_OPR + * equality operator. Anser hashes raw Datum bytes, and the transfer filters + * will too, so SQL equality must coincide with bitwise Datum equality of the + * key; anything looser could prune rows that actually join. A NestLoop + * clause with a Param on either side fails the Var test, as required -- + * such a clause is not a plain col = col join. + * + * Unlike Anser, which accepts only single-clause equijoins, callers pass one + * clause at a time: edge weight is the shared-attribute count, so + * multi-clause joins must be taken apart rather than refused. + */ +static bool +rpt_equality_clause(Node *clause, Var **outer_var, Var **inner_var, + Oid *key_type) +{ + OpExpr *op; + Node *l; + Node *r; + Var *lvar; + Var *rvar; + Oid keytype; + TypeCacheEntry *tce; + + if (clause == NULL || !IsA(clause, OpExpr)) + return false; + op = (OpExpr *) clause; + if (list_length(op->args) != 2) + return false; + + l = (Node *) linitial(op->args); + r = (Node *) lsecond(op->args); + while (l != NULL && IsA(l, RelabelType)) + l = (Node *) ((RelabelType *) l)->arg; + while (r != NULL && IsA(r, RelabelType)) + r = (Node *) ((RelabelType *) r)->arg; + if (l == NULL || r == NULL || !IsA(l, Var) || !IsA(r, Var)) + return false; + + if (((Var *) l)->varno == OUTER_VAR && ((Var *) r)->varno == INNER_VAR) + { + lvar = (Var *) l; + rvar = (Var *) r; + } + else if (((Var *) l)->varno == INNER_VAR && ((Var *) r)->varno == OUTER_VAR) + { + lvar = (Var *) r; + rvar = (Var *) l; + } + else + return false; + *outer_var = lvar; + *inner_var = rvar; + + if ((*inner_var)->vartype != (*outer_var)->vartype) + return false; + + keytype = RptPgCall([&]() { return getBaseType((*inner_var)->vartype); }); + if (!RptPgCall([&]() { return get_typbyval(keytype); })) + return false; + if (!rpt_key_type_supported(keytype)) + return false; + + /* + * The clause has to be the type's own equality rather than just some + * hashable operator spelled "=". The type allowlist above supplies the + * separate proof that equal keys have the same Datum representation. + */ + tce = RptPgCall([&]() { return lookup_type_cache(keytype, TYPECACHE_EQ_OPR); }); + if (!OidIsValid(tce->eq_opr) || op->opno != tce->eq_opr) + return false; + + *key_type = keytype; + return true; +} + +/* + * Resolve one validated equijoin clause to its two base scans and record the + * edge between their vertices. + * + * left_rtindex/right_rtindex are canonicalized (lower rtindex on the left), not the + * join's outer/inner: every clause of a vertex pair merges into one + * RptEdgeInfo and the neighbor matrix is symmetric, so a second join over + * the same pair with the opposite orientation must land on the same edge + * rather than colliding with its mirror image. The clause's key pair is + * stored in the same canonical order, and the protect flags name the + * protected *vertex*: JOIN_LEFT protects the outer side's vertex (its rows + * survive whether or not they match, so it must never be reduced by the + * other side's keys), JOIN_RIGHT the inner side's, INNER and SEMI neither, + * and every other join type (ANTI, FULL, LASJ_NOTIN, ...) makes no edge at + * all. protect_left therefore means left must NOT be reduced by right's + * keys. + * + * key_type is the validated equality input type of the first clause recorded + * for the edge; the data model carries one type per edge even though + * different clauses of a pair could equate different types. + */ +static void +rpt_add_edge(PlannedStmt *stmt, RptTransferMgr *mgr, JoinType jointype, + Plan *outer_plan, AttrNumber outer_attno, + Plan *inner_plan, AttrNumber inner_attno, Oid key_type) +{ + Plan *outer_scan; + Plan *inner_scan; + AttrNumber outer_key; + AttrNumber inner_key; + int outer_rtindex; + int inner_rtindex; + RptVertexInfo *outer_v; + RptVertexInfo *inner_v; + int left_rtindex; + int right_rtindex; + RptKey left_key; + RptKey right_key; + int protected_rtindex = 0; + RptEdgeInfo *edge; + + /* ANTI, FULL and the GP extras make no usable edge at all. */ + if (jointype != JOIN_INNER && jointype != JOIN_SEMI && + jointype != JOIN_LEFT && jointype != JOIN_RIGHT) + return; + + if (!rpt_resolve_key_scan(outer_plan, outer_attno, + &outer_scan, &outer_key)) + return; + if (!rpt_resolve_key_scan(inner_plan, inner_attno, + &inner_scan, &inner_key)) + return; + + outer_rtindex = ((Scan *) outer_scan)->scanrelid; + inner_rtindex = ((Scan *) inner_scan)->scanrelid; + + /* + * Create both vertices before taking pointers into mgr.vertices: creating + * the second can reallocate the vector the first pointer refers into. + */ + rpt_vertex_for_scan(stmt, mgr, outer_scan); + rpt_vertex_for_scan(stmt, mgr, inner_scan); + outer_v = &mgr->vertices[mgr->vertex_index_by_rtindex[outer_rtindex]]; + inner_v = &mgr->vertices[mgr->vertex_index_by_rtindex[inner_rtindex]]; + + /* The preserved side of an outer join is protected from reduction. */ + if (jointype == JOIN_LEFT) + protected_rtindex = outer_rtindex; + else if (jointype == JOIN_RIGHT) + protected_rtindex = inner_rtindex; + + if (outer_rtindex <= inner_rtindex) + { + left_rtindex = outer_rtindex; + left_key.rtindex = outer_rtindex; + left_key.attno = outer_key; + right_rtindex = inner_rtindex; + right_key.rtindex = inner_rtindex; + right_key.attno = inner_key; + } + else + { + left_rtindex = inner_rtindex; + left_key.rtindex = inner_rtindex; + left_key.attno = inner_key; + right_rtindex = outer_rtindex; + right_key.rtindex = outer_rtindex; + right_key.attno = outer_key; + } + + edge = &mgr->neighbor_matrix[left_rtindex][right_rtindex]; + if (edge->clauses.empty()) + { + edge->left_rtindex = left_rtindex; + edge->right_rtindex = right_rtindex; + edge->key_type = key_type; + edge->protect_left = false; + edge->protect_right = false; + } + edge->clauses.push_back(std::make_pair(left_key, right_key)); + if (protected_rtindex == left_rtindex) + edge->protect_left = true; + else if (protected_rtindex == right_rtindex) + edge->protect_right = true; + + outer_v->join_keys.insert(outer_rtindex == left_rtindex ? left_key : right_key); + inner_v->join_keys.insert(inner_rtindex == left_rtindex ? left_key : right_key); + + /* The matrix is symmetric: both directions name the same edge value. */ + mgr->neighbor_matrix[right_rtindex][left_rtindex] = *edge; +} + +/* + * Run one join's clause list through validation, resolution and edge + * recording. Clauses that fail validation or resolution are skipped + * individually; a join contributes an edge only for the clauses that + * survive. + */ +static void +rpt_collect_clauses(PlannedStmt *stmt, RptTransferMgr *mgr, JoinType jointype, + Plan *outer, Plan *inner, List *clauses) +{ + ListCell *lc; + + foreach(lc, clauses) + { + Var *outer_var; + Var *inner_var; + Oid key_type; + + if (!rpt_equality_clause((Node *) lfirst(lc), + &outer_var, &inner_var, &key_type)) + continue; + + rpt_add_edge(stmt, mgr, jointype, + outer, outer_var->varattno, + inner, inner_var->varattno, key_type); + } +} + +/* + * Manual recursive walk, after anser_inject_walk + * (gpcontrib/anser/src/anserplan.c): the outerPlan/innerPlan spine plus the + * child lists anser's walk omits (Append, MergeAppend, Sequence, BitmapAnd, + * BitmapOr, SubqueryScan, CustomScan custom_plans), widened the same way + * anser_max_plan_node_id widens its own. plan->initPlan is skipped on + * purpose: the SubPlan's Plan tree sits in stmt->subplans, which + * RptCollectGraph walks separately. Descending these lists finds joins + * behind region boundaries; the resolver still refuses to cross them, so a + * join inside a UNION arm can link scans of that arm but never out of it. + */ +static void +rpt_walk(PlannedStmt *stmt, RptTransferMgr *mgr, Plan *plan) +{ + ListCell *lc; + + if (plan == NULL) + return; + + switch (nodeTag(plan)) + { + case T_HashJoin: + rpt_collect_clauses(stmt, mgr, ((HashJoin *) plan)->join.jointype, + outerPlan(plan), innerPlan(plan), + ((HashJoin *) plan)->hashclauses); + break; + case T_MergeJoin: + rpt_collect_clauses(stmt, mgr, ((MergeJoin *) plan)->join.jointype, + outerPlan(plan), innerPlan(plan), + ((MergeJoin *) plan)->mergeclauses); + break; + case T_NestLoop: + rpt_collect_clauses(stmt, mgr, ((NestLoop *) plan)->join.jointype, + outerPlan(plan), innerPlan(plan), + ((NestLoop *) plan)->join.joinqual); + break; + default: + break; + } + + rpt_walk(stmt, mgr, outerPlan(plan)); + rpt_walk(stmt, mgr, innerPlan(plan)); + + switch (nodeTag(plan)) + { + case T_Append: + foreach(lc, ((Append *) plan)->appendplans) + rpt_walk(stmt, mgr, (Plan *) lfirst(lc)); + break; + case T_MergeAppend: + foreach(lc, ((MergeAppend *) plan)->mergeplans) + rpt_walk(stmt, mgr, (Plan *) lfirst(lc)); + break; + case T_Sequence: + foreach(lc, ((Sequence *) plan)->subplans) + rpt_walk(stmt, mgr, (Plan *) lfirst(lc)); + break; + case T_BitmapAnd: + foreach(lc, ((BitmapAnd *) plan)->bitmapplans) + rpt_walk(stmt, mgr, (Plan *) lfirst(lc)); + break; + case T_BitmapOr: + foreach(lc, ((BitmapOr *) plan)->bitmapplans) + rpt_walk(stmt, mgr, (Plan *) lfirst(lc)); + break; + case T_SubqueryScan: + rpt_walk(stmt, mgr, ((SubqueryScan *) plan)->subplan); + break; + case T_CustomScan: + foreach(lc, ((CustomScan *) plan)->custom_plans) + rpt_walk(stmt, mgr, (Plan *) lfirst(lc)); + break; + default: + break; + } +} + +/* + * Extract the join graph of a finished statement: walk the main plan tree + * and every subplan, create a vertex for each base scan a resolved equijoin + * edge reaches, and record the edges in the (symmetric) neighbor matrix. + * Only vertices, by_rti and neighbor_matrix are filled here; the + * classification, equivalence classes and the transfer plan itself are + * rptgraph.cpp's and rptplan.cpp's passes over the same manager. + */ +void +RptCollectGraph(PlannedStmt *stmt, RptTransferMgr *mgr) +{ + ListCell *lc; + + if (stmt == NULL || stmt->planTree == NULL) + return; + + rpt_walk(stmt, mgr, stmt->planTree); + foreach(lc, stmt->subplans) + rpt_walk(stmt, mgr, (Plan *) lfirst(lc)); +} + +/* Catalog NDV is global. A negative stadistinct is a fraction of reltuples. + * This is a survey estimate, never a predicate-transfer correctness input. */ +double +RptEstimateDistinct(const RptVertexInfo &vertex, AttrNumber attno) +{ + if (!OidIsValid(vertex.relid) || attno <= 0) + return -1; + HeapTuple stats = RptPgCall([&]() { return SearchSysCache3(STATRELATTINH, + ObjectIdGetDatum(vertex.relid), Int16GetDatum(attno), BoolGetDatum(false)); }); + if (!HeapTupleIsValid(stats)) + return -1; + double distinct = ((Form_pg_statistic) GETSTRUCT(stats))->stadistinct; + RptPgCall([&]() { ReleaseSysCache(stats); }); + if (distinct < 0) + { + HeapTuple relation = RptPgCall([&]() { return SearchSysCache1(RELOID, + ObjectIdGetDatum(vertex.relid)); }); + if (!HeapTupleIsValid(relation)) + return -1; + double rows = ((Form_pg_class) GETSTRUCT(relation))->reltuples; + RptPgCall([&]() { ReleaseSysCache(relation); }); + if (rows < 0) + return -1; + distinct *= -rows; + } + return distinct > 0 ? distinct : -1; +} diff --git a/gpcontrib/robust_predicate_transfer/test/diagnose_rpt_test.py b/gpcontrib/robust_predicate_transfer/test/diagnose_rpt_test.py new file mode 100644 index 00000000000..b4aeeeb4ed4 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/test/diagnose_rpt_test.py @@ -0,0 +1,106 @@ +#!/usr/bin/env python3 +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""Offline checks for the forward diagnostic runner and watchdog.""" +import importlib.util +import json +from pathlib import Path +import subprocess +import sys +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[1] +SCRIPT = ROOT / "scripts" / "diagnose_rpt.py" +spec = importlib.util.spec_from_file_location("diagnose_rpt", SCRIPT) +diag = importlib.util.module_from_spec(spec) +spec.loader.exec_module(diag) + + +class DiagnosticTest(unittest.TestCase): + def test_instrumentation_retains_assertions_and_reports_dynamic_queries(self): + source = (ROOT / "sql" / "rpt_forward.sql").read_text() + output = diag.instrument_sql(source) + self.assertIn("IF pg_temp.rpt_fwd_removed(plan) <= 0 THEN", output) + self.assertIn("RPT STEP explain analyze=% SQL=%", output) + self.assertIn("RPT STEP results rpt=% SQL=%", output) + self.assertIn("RPT STEP transfer_plan SQL=%", output) + self.assertIn("dump := public.rpt_transfer_plan(q);", output) + self.assertIn("RPT STEP SPI local chain", output) + + def test_changed_fixture_fails_instead_of_silently_losing_markers(self): + with self.assertRaises(ValueError): + diag.instrument_sql("SELECT 1;") + + def test_backward_decision_summary(self): + dump = """backward consumer 2 producer 1 eligible=yes reason=eligible +backward class root=1 rep=1.2 consumers=1 bytes=1048576 est_cost_ms=12.900 decision=inject +backward consumer 2 root=1 rep=1.2 key=rpt_b_1_1_2 defer_first=yes rows_removed=98 decision=inject +backward consumer 3 root=1 rep=1.2 key=- defer_first=no rows_removed=-1 decision=sort between consumer and join +""" + report = diag.summarize_dump(dump) + self.assertEqual(report["classes"], 1) + self.assertEqual(report["injected_consumers"], 1) + self.assertEqual(report["refused_consumers"], {"sort between consumer and join": 1}) + self.assertAlmostEqual(report["estimated_cost_ms"], 12.9) + log = "NOTICE: RPT DUMP BEGIN SQL=SELECT 1\nNOTICE: " + dump + "NOTICE: RPT DUMP END\n" + self.assertEqual(diag.summarize_log(log), [dict(sql="SELECT 1", **report)]) + + def test_backward_instrumentation(self): + for suite in ("backward", "backward_faults"): + output = diag.instrument_sql((ROOT / "sql" / ("rpt_" + suite + ".sql")).read_text(), suite) + self.assertIn("RPT DUMP BEGIN SQL=%", output) + self.assertIn("RPT STEP results SQL=%", output) + self.assertIn("RAISE EXCEPTION", output) + + def run_fixture(self, hangs): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + fake_psql = root / "psql" + fake_psql.write_text("#!" + sys.executable + "\n" + "import sys, time\n" + "if '-c' in sys.argv:\n" + " print(sys.argv[sys.argv.index('-c') + 1])\n" + " sys.exit(0)\n" + "print('RPT STEP fixture', flush=True)\n" + + ("time.sleep(30)\n" if hangs else "")) + fake_psql.chmod(0o755) + output = root / "output" + result = subprocess.run([sys.executable, str(SCRIPT), "--psql", str(fake_psql), + "--output", str(output), "--wall-timeout", + "0.2" if hangs else "5", "--cancel-grace", "0.1"], + capture_output=True, text=True, timeout=10) + state = json.loads((output / "result.json").read_text()) + self.assertEqual(state["timed_out"], hangs) + self.assertEqual(result.returncode, 2 if hangs else 0, result.stderr) + if hangs: + self.assertIn("pg_stat_activity", (output / "activity-before-cancel.json").read_text()) + self.assertIn("pg_cancel_backend", (output / "cancel.txt").read_text()) + self.assertTrue((output / "activity-after-cancel.json").is_file()) + self.assertIn("not terminated", result.stdout) + self.assertIn("RPT STEP fixture", (output / "client.log").read_text()) + + def test_successful_workload(self): + self.run_fixture(False) + + def test_external_watchdog_survives_an_unresponsive_client(self): + self.run_fixture(True) + + +if __name__ == "__main__": + unittest.main() diff --git a/gpcontrib/robust_predicate_transfer/test/forward_walk_test.cpp b/gpcontrib/robust_predicate_transfer/test/forward_walk_test.cpp new file mode 100644 index 00000000000..013027b0404 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/test/forward_walk_test.cpp @@ -0,0 +1,110 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * forward_walk_test.cpp + * SubPlan context tests using the real backend plan walker. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/test/forward_walk_test.cpp + * + *------------------------------------------------------------------------- + */ +#include "../src/rptforward.cpp" +#undef fprintf +#define CHECK(c) do { if (!(c)) { fprintf(stderr, "check failed: %s:%d: %s\n", __FILE__, __LINE__, #c); abort(); } } while (0) + +static Plan *anser_node; +static Plan *local_node; +extern "C" { +sigjmp_buf *PG_exception_stack = NULL; +ErrorContextCallback *error_context_stack = NULL; +int dispatch_nest_level = 0; +void ExceptionalCondition(const char *, const char *, int) { abort(); } +void check_stack_depth(void) {} +bool AnserIsRuntimeFilterScan(const Plan *p) { return p == anser_node; } +bool RptIsLocalScan(const Plan *p) { return p == local_node; } +bool is_plan_node(Node *p) +{ + return IsA(p, Result) || IsA(p, SeqScan) || IsA(p, CustomScan); +} +bool expression_tree_walker_impl(Node *node, tree_walker_callback walker, void *ctx) +{ + if (!node) return false; + CHECK(IsA(node, List)); + ListCell *lc; + foreach(lc, (List *) node) + if (walker((Node *) lfirst(lc), ctx)) return true; + return false; +} +bool query_tree_walker_impl(Query *, tree_walker_callback, void *, int) { abort(); } +} + +int main() +{ + PlannedStmt stmt = {}; + stmt.type = T_PlannedStmt; + Result root = {}, subroot = {}; + root.plan.type = subroot.plan.type = T_Result; + stmt.planTree = &root.plan; + ListCell plan_cell = {}, expression_cell = {}; + List plans = {}, expressions = {}; + plans.type = expressions.type = T_List; + plans.length = plans.max_length = expressions.length = expressions.max_length = 1; + plans.elements = &plan_cell; expressions.elements = &expression_cell; + plan_cell.ptr_value = &subroot; + stmt.subplans = &plans; + SubPlan subplan = {}; + subplan.xpr.type = T_SubPlan; + subplan.plan_id = 1; + expression_cell.ptr_value = &subplan; + + /* Resolving an existing InitPlan must not assert or miss its children. */ + root.plan.initPlan = &expressions; + CHECK(!rpt_has_runtime_filters(&stmt)); + CustomScan marker = {}; + marker.scan.plan.type = T_CustomScan; + subroot.plan.lefttree = &marker.scan.plan; + anser_node = &marker.scan.plan; + CHECK(rpt_has_runtime_filters(&stmt)); + anser_node = NULL; + local_node = &marker.scan.plan; + CHECK(rpt_has_runtime_filters(&stmt)); + local_node = NULL; + + /* Correlated/expression SubPlans use the same lookup context. */ + root.plan.initPlan = NIL; + root.plan.qual = &expressions; + CHECK(!rpt_has_runtime_filters(&stmt)); + anser_node = &marker.scan.plan; + CHECK(rpt_has_runtime_filters(&stmt)); + /* The parent walk shares the statement lookup context and preserves + * ambiguous placements as refusals rather than choosing the last one. */ + std::set targets = {&marker.scan.plan}; + std::map slices; + auto parents = rpt_find_parents(&stmt, targets, &slices); + CHECK(parents.empty()); /* SubPlan lookup does not enter its plan. */ + root.plan.lefttree = &marker.scan.plan; + parents = rpt_find_parents(&stmt, targets, &slices); + CHECK(parents.at(&marker.scan.plan) == &root.plan); + root.plan.righttree = &marker.scan.plan; + parents = rpt_find_parents(&stmt, targets, &slices); + CHECK(parents.at(&marker.scan.plan) == NULL); + CHECK(PG_exception_stack == NULL && error_context_stack == NULL); + return 0; +} diff --git a/gpcontrib/robust_predicate_transfer/test/graph_test.cpp b/gpcontrib/robust_predicate_transfer/test/graph_test.cpp new file mode 100644 index 00000000000..513f0d12c30 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/test/graph_test.cpp @@ -0,0 +1,1251 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * graph_test.cpp + * Deterministic tests of the production graph, planner, resolver and + * PostgreSQL/C++ error boundary, using synthetic plans and C-call stubs. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/test/graph_test.cpp + * + *------------------------------------------------------------------------- + */ +/* Standalone tests of the production graph/plan code. PostgreSQL calls are + * stubbed below; no server or optimizer choices affect these graph fixtures. */ +#include "../src/rptgraph.cpp" +#include "../src/rptplan.cpp" +#include "../src/rptregion.cpp" +#include "../src/rpteligible.cpp" +#include "../include/rptqual.h" +#include "../src/rptforwardplan.cpp" +#include "../src/rptbackwardplan.cpp" + +#include +#include +#include +#undef vsnprintf + +/* Unlike assert(), checks and their side effects remain active with NDEBUG. */ +#define CHECK(condition) \ + do { \ + if (!(condition)) { \ + fputs("check failed: " #condition "\n", stderr); \ + abort(); \ + } \ + } while (0) + +struct TestResolution +{ + Plan *parent; + Plan *scan; + AttrNumber attno; + int slice_index; + bool for_consumer; +}; +static std::map test_resolutions; + +extern "C" { +sigjmp_buf *PG_exception_stack = NULL; +ErrorContextCallback *error_context_stack = NULL; +int dispatch_nest_level = 0; +HeapTuple SearchSysCache1(int, Datum) { abort(); } +HeapTuple SearchSysCache3(int, Datum, Datum, Datum) { abort(); } +void ReleaseSysCache(HeapTuple) { abort(); } +bool AnserResolveKeyScan(Plan *top, AttrNumber slot, int, bool consumer, + Plan **parent, Plan **scan, AttrNumber *attno, int *slice_index) +{ + CHECK(slot > 0); + auto found = test_resolutions.find(top); + if (found == test_resolutions.end()) + return false; + CHECK(found->second.for_consumer == consumer); + *parent = found->second.parent; + *scan = found->second.scan; + *attno = found->second.attno; + *slice_index = found->second.slice_index; + return true; +} +Oid getBaseType(Oid type) { return type; } +bool get_typbyval(Oid) { return true; } +TypeCacheEntry *lookup_type_cache(Oid, int) { abort(); } +GpPolicy *GpPolicyFetch(Oid) { abort(); } +void pfree(void *) { abort(); } +bool AnserIsRuntimeFilterScan(const Plan *) { return false; } + +void ExceptionalCondition(const char *, const char *, int) { abort(); } +void exec_init_plan_tree_base(plan_tree_base_prefix *, PlannedStmt *) {} +bool plan_tree_walker(Node *node, bool (*walker)(Node *, void *), void *ctx, bool) +{ + Plan *plan = (Plan *) node; + return walker((Node *) outerPlan(plan), ctx) || + walker((Node *) innerPlan(plan), ctx); +} +void appendStringInfo(StringInfo str, const char *format, ...) +{ + va_list args; + va_start(args, format); + int count = vsnprintf(str->data + str->len, str->maxlen - str->len, format, args); + va_end(args); + CHECK(count >= 0 && count < str->maxlen - str->len); + str->len += count; +} +void appendStringInfoString(StringInfo str, const char *text) +{ + appendStringInfo(str, "%s", text); +} +void appendStringInfoChar(StringInfo str, char ch) +{ + appendStringInfo(str, "%c", ch); +} +} +bool rpt_enable = false; +bool rpt_debug = false; +bool rpt_backward = true; +int rpt_max_backward_consumers = 4; +int rpt_max_chain_depth = 4; + +static void vertex(RptTransferMgr &mgr, int rtindex, int rows, bool filtered) +{ + RptVertexInfo v = {}; + v.rtindex = rtindex; + v.cardinality = rows; + v.filtered = filtered; + mgr.vertex_index_by_rtindex[rtindex] = mgr.vertices.size(); + mgr.vertices.push_back(v); +} + +static RptEdgeInfo edge(int a, int b, bool protect_a = false, + bool protect_b = false, int col_a = 1, int col_b = 1) +{ + RptEdgeInfo e = {}; + e.left_rtindex = a; + e.right_rtindex = b; + e.key_type = 23; + e.protect_left = protect_a; + e.protect_right = protect_b; + e.clauses.push_back(std::make_pair(RptKey{a, (AttrNumber) col_a}, + RptKey{b, (AttrNumber) col_b})); + return e; +} + +static void connect(RptTransferMgr &mgr, const RptEdgeInfo &e) +{ + rpt_upsert_edge(&mgr, e); + mgr.vertices[mgr.vertex_index_by_rtindex[e.left_rtindex]].join_keys.insert(e.clauses[0].first); + mgr.vertices[mgr.vertex_index_by_rtindex[e.right_rtindex]].join_keys.insert(e.clauses[0].second); +} + +static void bypass_direction(bool reverse) +{ + RptTransferMgr mgr; + vertex(mgr, 1, 100, true); + vertex(mgr, 2, 10, true); + vertex(mgr, 3, 50, false); + /* 1 LEFT JOIN (2 JOIN 3): only 1 -> 3 -> 2 is legal. */ + connect(mgr, reverse ? edge(3, 1, false, true) : edge(1, 3, true)); + connect(mgr, reverse ? edge(3, 2) : edge(2, 3)); + mgr.vertices[2].unfiltered = true; + rpt_skip_unfiltered(&mgr); + const auto &e = mgr.neighbor_matrix.at(1).at(2); + CHECK(rpt_can_flow(&e, 1, 2)); + CHECK(!rpt_can_flow(&e, 2, 1)); + CHECK(!rpt_can_flow(&mgr.neighbor_matrix.at(2).at(1), 2, 1)); +} + +static void protection_and_idempotence() +{ + RptTransferMgr mgr; + CHECK(rpt_upsert_edge(&mgr, edge(1, 2, true))); + CHECK(!rpt_upsert_edge(&mgr, edge(1, 2, true))); + CHECK(!rpt_upsert_edge(&mgr, edge(2, 1))); + CHECK(mgr.neighbor_matrix[1][2].protect_left); + CHECK(rpt_upsert_edge(&mgr, edge(2, 1, true))); + CHECK(mgr.neighbor_matrix[1][2].protect_right); +} + +static RptTransferMgr star(int cap) +{ + RptTransferMgr mgr; + rpt_max_chain_depth = cap; + for (int rtindex = 1; rtindex <= 4; ++rtindex) + vertex(mgr, rtindex, 100 / rtindex, true); + for (int rtindex = 2; rtindex <= 4; ++rtindex) + connect(mgr, edge(1, rtindex)); + RptBuildClasses(&mgr); + RptBuildTransferPlan(NULL, &mgr); + return mgr; +} + +static void asymmetric_shape() +{ + auto mgr = star(4); + CHECK(mgr.groups.size() == 1); + CHECK(mgr.transfer_order_rtindexes == std::vector({1, 2, 3, 4})); + CHECK(mgr.reducer_order_rtindexes == std::vector({4, 3, 2})); + for (int rtindex = 2; rtindex <= 4; ++rtindex) + { + CHECK(mgr.graph[rtindex].forward_stage.out.size() == 1); + CHECK(mgr.graph[rtindex].forward_stage.out[0].other_rtindex == rtindex - 1); + CHECK(mgr.graph[rtindex].backward_stage.in.size() == 1); + CHECK(mgr.graph[rtindex].backward_stage.in[0].other_rtindex == 1); + } + CHECK(mgr.graph[1].forward_stage.out.empty()); + CHECK(mgr.graph[1].backward_stage.out.size() == 3); + mgr = star(1); + CHECK(mgr.graph[4].forward_stage.out.size() == 1); + CHECK(mgr.graph[3].forward_stage.out.empty()); + CHECK(mgr.graph[2].forward_stage.in.empty()); +} + +static void unfiltered_cycle() +{ + RptTransferMgr mgr; + for (int rtindex = 1; rtindex <= 4; ++rtindex) + vertex(mgr, rtindex, 10, rtindex == 1); + connect(mgr, edge(1, 2)); + connect(mgr, edge(2, 3)); + connect(mgr, edge(3, 4)); + connect(mgr, edge(4, 2)); + RptBuildClasses(&mgr); /* Must terminate, with no unfiltered producers. */ + for (const auto &row : mgr.neighbor_matrix) + for (const auto &entry : row.second) + if (row.first != 1) + CHECK(!rpt_can_flow(&entry.second, row.first, entry.first)); +} + +/* A single-column identity targetlist, allocated without PostgreSQL memory. */ +struct TestTargetList +{ + Var var = {}; + TargetEntry entry = {}; + ListCell cell = {}; + List list = {}; + + void bind(Plan *plan, int varno, int attno) + { + NodeSetTag(&var, T_Var); + var.varno = varno; + var.varattno = attno; + entry.expr = (Expr *) &var; + entry.resno = 1; + cell.ptr_value = &entry; + list.type = T_List; + list.length = list.max_length = 1; + list.elements = &cell; + plan->targetlist = &list; + } +}; + +struct TestBackwardPlan +{ + SeqScan consumer = {}; + SeqScan producer = {}; + Motion motion = {}; + Hash hash = {}; + HashJoin join = {}; + PlannedStmt stmt = {}; + TestTargetList lists[4]; + RptTransferMgr mgr; + + TestBackwardPlan() + { + test_resolutions.clear(); + NodeSetTag(&consumer, T_SeqScan); + NodeSetTag(&producer, T_SeqScan); + consumer.scan.scanrelid = 1; + producer.scan.scanrelid = 2; + lists[0].bind((Plan *) &consumer, 1, 3); + lists[1].bind((Plan *) &producer, 2, 3); + NodeSetTag(&motion, T_Motion); + motion.motionID = 1; + outerPlan(&motion) = (Plan *) &producer; + lists[2].bind((Plan *) &motion, OUTER_VAR, 1); + NodeSetTag(&hash, T_Hash); + outerPlan(&hash) = (Plan *) &motion; + lists[3].bind((Plan *) &hash, OUTER_VAR, 1); + NodeSetTag(&join, T_HashJoin); + join.join.jointype = JOIN_INNER; + outerPlan(&join) = (Plan *) &consumer; + innerPlan(&join) = (Plan *) &hash; + stmt.planTree = (Plan *) &join; + vertex(mgr, 1, 1000, false); + vertex(mgr, 2, 100, true); + mgr.vertices[0].scan = (Plan *) &consumer; + mgr.vertices[1].scan = (Plan *) &producer; + connect(mgr, edge(1, 2, false, false, 3, 3)); + RptBuildClasses(&mgr); + test_resolutions[(Plan *) &hash] = {(Plan *) &motion, (Plan *) &producer, 1, 1, false}; + RptBuildTransferPlan(&stmt, &mgr); + } +}; + +static void strict_backward_eligibility() +{ + TestBackwardPlan plan; + CHECK(plan.mgr.backward_consumers.size() == 1); + auto decision = plan.mgr.backward_consumers[0]; + CHECK(decision.eligible && decision.rule9 && decision.defer_first); + CHECK(decision.producer_rtindex == 2 && decision.consumer_rtindex == 1); + CHECK(decision.consumer_attno == 1 && decision.producer_attno == 1); + CHECK(decision.consumer_parent == (Plan *) &plan.join); + CHECK(decision.producer_parent == (Plan *) &plan.motion); + CHECK(decision.producer_slice_index == 1 && decision.consumer_slice_index == 0); + CHECK(plan.mgr.consumer_ok_by_rtindex[1]); + + for (NodeTag tag : {T_Sort, T_Agg, T_Material, T_Unique, T_WindowAgg, T_Hash, T_SetOp}) + { + Plan blocker = {}; + blocker.type = tag; + outerPlan(&blocker) = (Plan *) &plan.consumer; + outerPlan(&plan.join) = &blocker; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(!plan.mgr.backward_consumers[0].eligible); + CHECK(plan.mgr.backward_consumers[0].rule9); + CHECK(plan.mgr.backward_consumers[0].reason.find("between consumer and join") != std::string::npos); + } + outerPlan(&plan.join) = (Plan *) &plan.consumer; + plan.join.join.prefetch_inner = true; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(plan.mgr.backward_consumers[0].eligible); + CHECK(!plan.mgr.backward_consumers[0].defer_first); + plan.join.join.prefetch_inner = false; + plan.join.join.jointype = JOIN_RIGHT; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(plan.mgr.backward_consumers[0].eligible); + CHECK(!plan.mgr.backward_consumers[0].defer_first); + plan.join.join.jointype = JOIN_INNER; + + HashJoin nested = {}; + Hash nested_hash = {}; + Plan other = {}; + NodeSetTag(&nested, T_HashJoin); + NodeSetTag(&nested_hash, T_Hash); + nested.join.jointype = JOIN_LEFT; + outerPlan(&nested) = &other; + innerPlan(&nested) = (Plan *) &nested_hash; + outerPlan(&nested_hash) = (Plan *) &plan.consumer; + outerPlan(&plan.join) = (Plan *) &nested; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(plan.mgr.backward_consumers[0].reason == "nullable side of outer join"); + + TestTargetList nested_list; + nested.join.jointype = JOIN_INNER; + outerPlan(&nested) = (Plan *) &plan.consumer; + innerPlan(&nested) = &other; + nested_list.bind((Plan *) &nested, OUTER_VAR, 1); + test_resolutions[(Plan *) &nested] = {(Plan *) &nested, (Plan *) &plan.consumer, 1, 0, true}; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(plan.mgr.backward_consumers[0].reason == "join between consumer and join"); + plan.join.join.prefetch_inner = true; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(plan.mgr.backward_consumers[0].eligible); + CHECK(!plan.mgr.backward_consumers[0].defer_first); + plan.join.join.prefetch_inner = false; + + outerPlan(&plan.join) = (Plan *) &plan.hash; + innerPlan(&plan.join) = (Plan *) &plan.consumer; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(!plan.mgr.backward_consumers[0].rule9); + CHECK(plan.mgr.backward_consumers[0].reason == "producer is not on a qualifying hash build side"); + outerPlan(&plan.join) = (Plan *) &plan.consumer; + innerPlan(&plan.join) = (Plan *) &plan.hash; + plan.motion.motionID = 0; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(plan.mgr.backward_consumers[0].reason == "same or unknown slice"); + plan.motion.motionID = 1; + plan.mgr.root_rtindexes.clear(); + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(plan.mgr.backward_consumers[0].reason == "bridge-led class is deferred"); + plan.mgr.graph[2].backward_stage.out[0].local = true; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(plan.mgr.backward_consumers[0].reason == "local edge uses core runtime filtering"); +} + +static void subplans_and_external_parameters_are_refused() +{ + TestBackwardPlan plan; + Bitmapset *parameters = (Bitmapset *) calloc(1, offsetof(Bitmapset, words) + sizeof(bitmapword)); + parameters->nwords = 1; + parameters->words[0] = 1; + plan.hash.plan.extParam = parameters; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(!plan.mgr.backward_consumers[0].eligible); + CHECK(plan.mgr.backward_consumers[0].reason == "parameterized producer can be rescanned"); + plan.hash.plan.extParam = NULL; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(plan.mgr.backward_consumers[0].eligible); + plan.hash.plan.extParam = NULL; + free(parameters); + + /* The Motion still gives valid slices inside the SubPlan. A separate + * placement flag must refuse it regardless of slice-number changes. */ + ListCell cell = {}; + cell.ptr_value = &plan.join; + List subplans = {}; + subplans.type = T_List; + subplans.length = subplans.max_length = 1; + subplans.elements = &cell; + Plan main = {}; + NodeSetTag(&main, T_Result); + plan.stmt.planTree = &main; + plan.stmt.subplans = &subplans; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(!plan.mgr.backward_consumers[0].eligible); + CHECK(plan.mgr.backward_consumers[0].reason == "scan inside a SubPlan"); + plan.mgr.graph[2].backward_stage.out[0].local = true; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(plan.mgr.backward_consumers[0].reason == "scan inside a SubPlan"); +} + +static void every_global_producer_is_required() +{ + RptTransferMgr mgr; + vertex(mgr, 1, 100, false); + RptBackwardConsumer first; + first.consumer_rtindex = 1; + first.producer_rtindex = 2; + first.eligible = true; + RptBackwardConsumer second = first; + second.producer_rtindex = 3; + second.eligible = false; + mgr.backward_consumers = {first, second}; + rpt_consumer_verdicts(&mgr); + CHECK(!mgr.consumer_ok_by_rtindex[1]); + mgr.backward_consumers[1].local = true; + rpt_consumer_verdicts(&mgr); + CHECK(mgr.consumer_ok_by_rtindex[1]); + mgr.backward_consumers[0].local = true; + rpt_consumer_verdicts(&mgr); + CHECK(!mgr.consumer_ok_by_rtindex[1]); +} + +static void backward_consumer_cap() +{ + RptTransferMgr mgr; + for (int i = 1; i <= 3; ++i) + { + vertex(mgr, i, 100 * i, false); + RptBackwardConsumer decision; + decision.class_key = {9, 1}; + decision.consumer_rtindex = i; + decision.strict_eligible = decision.eligible = true; + decision.estimated_rows_removed = 100 * i; + mgr.backward_consumers.push_back(decision); + } + rpt_max_backward_consumers = 1; + rpt_cap_backward_consumers(&mgr); + CHECK(!mgr.backward_consumers[0].eligible); + CHECK(!mgr.backward_consumers[1].eligible); + CHECK(mgr.backward_consumers[2].eligible); + CHECK(mgr.backward_consumers[0].reason == "backward consumer cap exceeded"); + rpt_max_backward_consumers = 4; +} + +static void bridges_and_locality() +{ + RptTransferMgr mgr; + vertex(mgr, 1, 100, true); + vertex(mgr, 2, 50, false); + vertex(mgr, 3, 20, true); + connect(mgr, edge(1, 2)); + connect(mgr, edge(2, 3, false, false, 2)); + RptBuildClasses(&mgr); + rpt_max_chain_depth = 4; + RptBuildTransferPlan(NULL, &mgr); + CHECK(mgr.groups.size() == 2); + CHECK(mgr.bridge_rtindexes == std::set({2})); + CHECK(mgr.reducer_order_rtindexes == std::vector({3, 2})); + RptTransferMgr runtime; + RptBuildForwardPlan(&mgr, &runtime); + CHECK(runtime.root_rtindexes.count(1)); + CHECK(runtime.bridge_rtindexes.count(2)); + CHECK(!runtime.graph.at(2).backward_stage.out.empty()); + for (auto &v : mgr.vertices) + { + v.numsegments = 3; + v.distkeys = {1}; + v.distopclasses = {1978}; + } + RptGraphEdge hop = {}; + hop.other_rtindex = 2; + hop.left_keys = {{1, 1}}; + hop.right_keys = {{2, 1}}; + CHECK(rpt_local_hop(&mgr, 1, hop)); + mgr.vertices[1].numsegments = 2; + CHECK(!rpt_local_hop(&mgr, 1, hop)); + mgr.vertices[1].numsegments = 3; + mgr.vertices[1].distopclasses = {1}; + CHECK(!rpt_local_hop(&mgr, 1, hop)); + mgr.vertices[1].distopclasses = {1978}; + mgr.vertices[1].distkeys = {2}; + CHECK(!rpt_local_hop(&mgr, 1, hop)); +} + +static void hash_policy(RptVertexInfo &v, int segments = 3) +{ + v.numsegments = segments; + v.distkeys = {1}; + v.distopclasses = {1978}; +} + +static void local_star_and_backward() +{ + auto mgr = star(4); + for (auto &v : mgr.vertices) + hash_policy(v); + rpt_classify_hops(&mgr); + int forward = 0; + int backward = 0; + for (const auto &node : mgr.graph) + { + for (const auto &hop : node.second.forward_stage.out) + { + ++forward; + CHECK(hop.local); /* Includes synthetic 4->3 and 3->2. */ + CHECK(mgr.graph.at(hop.other_rtindex).forward_stage.in[0].local); + } + for (const auto &hop : node.second.backward_stage.out) + { + ++backward; + CHECK(hop.local); + CHECK(mgr.graph.at(hop.other_rtindex).backward_stage.in[0].local); + } + } + CHECK(forward == 3 && backward == 3); + char buffer[16384] = {}; + StringInfoData dump = {}; + dump.data = buffer; + dump.maxlen = sizeof(buffer); + PlannedStmt stmt = {}; + stmt.type = T_PlannedStmt; + RangeTblEntry entries[4] = {}; + Alias aliases[4] = {}; + ListCell cells[4] = {}; + List rtable = {}; + rtable.type = T_List; + rtable.length = rtable.max_length = 4; + rtable.elements = cells; + stmt.rtable = &rtable; + char employee[] = "employee", manager[] = "manager", other[] = "other"; + for (int i = 0; i < 4; ++i) + { + entries[i].type = T_RangeTblEntry; + entries[i].relid = 99; /* Same relation, distinct query references. */ + entries[i].eref = &aliases[i]; + aliases[i].type = T_Alias; + aliases[i].aliasname = i == 0 ? employee : i == 1 ? manager : other; + cells[i].ptr_value = &entries[i]; + } + RptDumpTransferPlan(&stmt, &mgr, &dump); + std::string text(buffer); + CHECK(text.find("vertex 1 employee rows=") != std::string::npos); + CHECK(text.find("vertex 2 manager rows=") != std::string::npos); + /* Changing the RTE after graph construction is reflected immediately. */ + aliases[0].aliasname = manager; + dump.len = 0; + buffer[0] = '\0'; + RptDumpTransferPlan(&stmt, &mgr, &dump); + CHECK(std::string(buffer).find("vertex 1 manager rows=") != std::string::npos); + CHECK(text.find("merge=global") == std::string::npos); + CHECK(text.find("forward hop 3->2 merge=local\n") != std::string::npos); + CHECK(text.find("forward hop 4->3 merge=local\n") != std::string::npos); + CHECK(text.find("backward hop 1->4 merge=local\n") != std::string::npos); + + /* A class can mix local and global broadcast recipients. */ + mgr.vertices[mgr.vertex_index_by_rtindex[4]].numsegments = 2; + rpt_classify_hops(&mgr); + for (const auto &hop : mgr.graph[1].backward_stage.out) + CHECK(hop.local == (hop.other_rtindex != 4)); + + /* Bypassing an unfiltered table preserves policy-local transfer too. */ + RptTransferMgr bypass; + vertex(bypass, 1, 100, true); + vertex(bypass, 2, 10, true); + vertex(bypass, 3, 50, false); + for (auto &v : bypass.vertices) + hash_policy(v); + connect(bypass, edge(1, 3, true)); + connect(bypass, edge(2, 3)); + bypass.vertices[2].unfiltered = true; + rpt_skip_unfiltered(&bypass); + const auto &e = bypass.neighbor_matrix.at(1).at(2); + RptGraphEdge hop = {}; + hop.other_rtindex = 2; + hop.left_keys = {e.clauses[0].first}; + hop.right_keys = {e.clauses[0].second}; + CHECK(rpt_local_hop(&bypass, 1, hop)); +} + +static void replicated_and_compound_policies() +{ + RptTransferMgr mgr; + vertex(mgr, 1, 100, true); + vertex(mgr, 2, 10, true); + for (auto &v : mgr.vertices) + hash_policy(v); + RptGraphEdge hop = {}; + hop.other_rtindex = 2; + hop.left_keys = {{1, 1}}; + hop.right_keys = {{2, 1}}; + /* No neighbor-matrix edge is needed to prove policy locality. */ + CHECK(rpt_local_hop(&mgr, 1, hop)); + auto &src = mgr.vertices[0]; + auto &dst = mgr.vertices[1]; + src.distkeys = {1, 2}; + src.distopclasses = {1978, 1978}; + dst.distkeys = {1, 2}; + dst.distopclasses = {1978, 1978}; + CHECK(!rpt_local_hop(&mgr, 1, hop)); /* Missing a distribution column. */ + hop.left_keys.push_back({1, 2}); + hop.right_keys.push_back({2, 2}); + CHECK(rpt_local_hop(&mgr, 1, hop)); + std::swap(dst.distkeys[0], dst.distkeys[1]); + CHECK(!rpt_local_hop(&mgr, 1, hop)); /* Hash order matters. */ + + src.replicated = true; + src.distkeys.clear(); + src.distopclasses.clear(); + CHECK(rpt_local_hop(&mgr, 1, hop)); + src.numsegments = 4; + CHECK(rpt_local_hop(&mgr, 1, hop)); + src.numsegments = 2; + CHECK(!rpt_local_hop(&mgr, 1, hop)); + src.numsegments = 3; + dst.replicated = true; + CHECK(!rpt_local_hop(&mgr, 1, hop)); /* Into replicated always global. */ + src.replicated = false; + CHECK(!rpt_local_hop(&mgr, 1, hop)); + dst.replicated = false; + CHECK(!rpt_local_hop(&mgr, 1, hop)); /* Random/unknown source. */ +} + +static void index_only_endpoint() +{ + IndexOnlyScan scan = {}; + NodeSetTag(&scan, T_IndexOnlyScan); + scan.scan.scanrelid = 7; + TargetEntry output = {}; + Var index_var = {}; + NodeSetTag(&index_var, T_Var); + index_var.varno = INDEX_VAR; + index_var.varattno = 1; + output.expr = (Expr *) &index_var; + Var base_var = {}; + NodeSetTag(&base_var, T_Var); + base_var.varno = 7; + base_var.varattno = 3; + TargetEntry index_column = {}; + index_column.expr = (Expr *) &base_var; + /* A one-cell PostgreSQL list, without requiring palloc in the driver. */ + ListCell out_cell = {}; + out_cell.ptr_value = &output; + List out_list = {}; + out_list.type = T_List; + out_list.length = out_list.max_length = 1; + out_list.elements = &out_cell; + ListCell index_cell = {}; + index_cell.ptr_value = &index_column; + List index_list = out_list; + index_list.elements = &index_cell; + scan.scan.plan.targetlist = &out_list; + scan.indextlist = &index_list; + Plan *resolved = NULL; + AttrNumber attno = 0; + CHECK(rpt_resolve_key_scan((Plan *) &scan, 1, &resolved, &attno)); + CHECK(resolved == (Plan *) &scan && attno == 3); +} + +static void join_protection() +{ + for (JoinType type : {JOIN_INNER, JOIN_SEMI, JOIN_LEFT, JOIN_RIGHT, + JOIN_FULL, JOIN_ANTI, JOIN_LASJ_NOTIN}) + { + RptTransferMgr mgr; + SeqScan scans[2] = {}; + Var vars[2] = {}; + TargetEntry entries[2] = {}; + ListCell cells[2] = {}; + List lists[2] = {}; + for (int i = 0; i < 2; ++i) + { + NodeSetTag(&scans[i], T_SeqScan); + scans[i].scan.scanrelid = i + 1; + scans[i].scan.plan.plan_rows = 100 - i; + NodeSetTag(&vars[i], T_Var); + vars[i].varno = i + 1; + vars[i].varattno = 1; + entries[i].expr = (Expr *) &vars[i]; + cells[i].ptr_value = &entries[i]; + lists[i].type = T_List; + lists[i].length = lists[i].max_length = 1; + lists[i].elements = &cells[i]; + scans[i].scan.plan.targetlist = &lists[i]; + scans[i].scan.plan.qual = &lists[i]; /* mark both scans filtered */ + } + PlannedStmt stmt = {}; + rpt_add_edge(&stmt, &mgr, type, (Plan *) &scans[0], 1, + (Plan *) &scans[1], 1, 23); + if (type == JOIN_FULL || type == JOIN_ANTI || type == JOIN_LASJ_NOTIN) + { + CHECK(mgr.vertices.empty() && mgr.neighbor_matrix.empty()); + continue; + } + const auto &e = mgr.neighbor_matrix.at(1).at(2); + CHECK(e.protect_left == (type == JOIN_LEFT)); + CHECK(e.protect_right == (type == JOIN_RIGHT)); + RptBuildClasses(&mgr); + RptBuildTransferPlan(NULL, &mgr); + if (type == JOIN_LEFT || type == JOIN_RIGHT) + { + int preserved = type == JOIN_LEFT ? 1 : 2; + CHECK(mgr.graph[preserved].forward_stage.in.empty()); + CHECK(mgr.graph[preserved].backward_stage.in.empty()); + RptTransferMgr runtime; + RptBuildForwardPlan(&mgr, &runtime); + CHECK(runtime.graph[preserved].forward_stage.in.empty()); + CHECK(runtime.graph[preserved].backward_stage.in.empty()); + } + } +} + +static void directed_parent_choice() +{ + RptTransferMgr mgr; + for (int rtindex = 1; rtindex <= 3; ++rtindex) + vertex(mgr, rtindex, 100 / rtindex, true); + std::set constructed = {1, 2}; + std::set remaining = {3}; + std::map forward_depth = {{1, 0}, {2, 3}}; + std::map backward_depth = forward_depth; + int forward_parent_rtindex, backward_parent_rtindex, target_rtindex; + + connect(mgr, edge(1, 3)); + connect(mgr, edge(2, 3, true)); /* deepest parent blocks 3 -> 2 */ + CHECK(rpt_find_edge(&mgr, constructed, remaining, forward_depth, backward_depth, + &forward_parent_rtindex, &backward_parent_rtindex, &target_rtindex)); + CHECK(target_rtindex == 3 && forward_parent_rtindex == 1 && backward_parent_rtindex == 1); + + mgr.neighbor_matrix.clear(); + connect(mgr, edge(1, 3, false, true)); /* shallowest blocks 1 -> 3 */ + connect(mgr, edge(2, 3)); + CHECK(rpt_find_edge(&mgr, constructed, remaining, forward_depth, backward_depth, + &forward_parent_rtindex, &backward_parent_rtindex, &target_rtindex)); + CHECK(forward_parent_rtindex == 2 && backward_parent_rtindex == 2); + + mgr.neighbor_matrix.clear(); + RptEdgeInfo heavy = edge(1, 3, true); + heavy.clauses.push_back(std::make_pair(RptKey{1, 2}, RptKey{3, 2})); + connect(mgr, heavy); + connect(mgr, edge(2, 3)); + CHECK(rpt_find_edge(&mgr, constructed, remaining, forward_depth, backward_depth, + &forward_parent_rtindex, &backward_parent_rtindex, &target_rtindex)); + CHECK(forward_parent_rtindex == 2 && backward_parent_rtindex == 1); /* lower-weight open edge */ + + mgr.neighbor_matrix.clear(); + connect(mgr, edge(1, 3, true)); + connect(mgr, edge(2, 3, true)); + CHECK(rpt_find_edge(&mgr, constructed, remaining, forward_depth, backward_depth, + &forward_parent_rtindex, &backward_parent_rtindex, &target_rtindex)); + CHECK(forward_parent_rtindex == -1 && backward_parent_rtindex == 1); + + mgr.neighbor_matrix.clear(); + connect(mgr, edge(1, 3, false, true)); + connect(mgr, edge(2, 3, false, true)); + CHECK(rpt_find_edge(&mgr, constructed, remaining, forward_depth, backward_depth, + &forward_parent_rtindex, &backward_parent_rtindex, &target_rtindex)); + CHECK(forward_parent_rtindex == 2 && backward_parent_rtindex == -1); +} + +static void forward_root_includes_unfiltered_fact() +{ + RptTransferMgr analysis; + vertex(analysis, 1, 1000000, false); + vertex(analysis, 2, 100, true); + vertex(analysis, 3, 50, true); + connect(analysis, edge(1, 2)); + connect(analysis, edge(1, 3)); + RptBuildClasses(&analysis); + RptBuildTransferPlan(NULL, &analysis); + CHECK(analysis.root_rtindexes.count(2)); + RptTransferMgr forward; + RptBuildForwardPlan(&analysis, &forward); + CHECK(forward.root_rtindexes.size() == 1 && forward.root_rtindexes.count(1)); + CHECK(!forward.graph.at(1).forward_stage.in.empty()); + CHECK(forward.graph.at(1).forward_stage.out.empty()); + CHECK(forward.reducer_order_rtindexes.size() == 2); + CHECK(analysis.root_rtindexes.count(2)); + CHECK(analysis.groups.begin()->second.leader_rtindex == 2); + CHECK(forward.backward_consumers.empty()); + CHECK(!forward.graph.at(1).backward_stage.out.empty()); + for (const auto &group : forward.groups) + CHECK(group.second.leader_rtindex == 1); + /* Rebuilding must discard the previous schedule. */ + RptBuildForwardPlan(&analysis, &forward); + CHECK(forward.reducer_order_rtindexes.size() == 2); +} + +static void error_unwinds() +{ + int destroyed = 0; + struct Owner { + int &count; + ~Owner() { ++count; } + }; + try + { + Owner owner{destroyed}; + RptPgCall([]() { siglongjmp(*PG_exception_stack, 1); }); + abort(); + } + catch (const RptPgError &) {} + CHECK(destroyed == 1); + CHECK(PG_exception_stack == NULL); + /* A C++ callback exception must restore PG's exception stack too. */ + try + { + RptPgCall([]() { throw std::bad_alloc(); }); + abort(); + } + catch (const std::bad_alloc &) {} + CHECK(PG_exception_stack == NULL); +} + +static bool size_one_mib(double, int64 *elems, int64 *payload, int64 *planned) +{ + *elems = 1000; *payload = 1 << 20; *planned = 1 << 20; + return true; +} + +static bool size_declined(double, int64 *, int64 *, int64 *) +{ + return false; +} + +static RptTransferMgr forward_star() +{ + RptTransferMgr analysis; + vertex(analysis, 1, 1000000, false); + vertex(analysis, 2, 100, true); + vertex(analysis, 3, 50, true); + connect(analysis, edge(1, 2)); + connect(analysis, edge(1, 3)); + RptBuildClasses(&analysis); + rpt_max_chain_depth = 4; + RptBuildTransferPlan(NULL, &analysis); + RptTransferMgr forward; + RptBuildForwardPlan(&analysis, &forward); + return forward; +} + +static RptForwardParams forward_params(double budget, double min_rows) +{ + RptForwardParams p; + p.width = 3; p.ms_per_mib_part = 3.5; + p.max_exchange_ms = budget; p.min_root_rows = min_rows; + return p; +} + +static void forward_selection_star() +{ + RptTransferMgr forward = forward_star(); + std::vector comps; + RptSelectForward(forward, forward_params(-1, 0), {}, size_one_mib, nullptr, &comps); + CHECK(comps.size() == 1); + const RptForwardComponent &c = comps[0]; + CHECK(c.root_rtindex == 1 && c.accepted && c.reason == "inject"); + CHECK(!c.chain_rtindexes.empty() && !c.channels.empty()); + CHECK(std::find(c.chain_rtindexes.begin(), c.chain_rtindexes.end(), 1) == c.chain_rtindexes.end()); + double cost = 0; + bool reaches_root = false; + for (const RptForwardChannel &ch : c.channels) + { + /* producers before consumers: a channel's source precedes its target */ + auto source_position = std::find(c.chain_rtindexes.begin(), c.chain_rtindexes.end(), ch.source_rtindex); + CHECK(source_position != c.chain_rtindexes.end()); + if (ch.target_rtindex == c.root_rtindex) + reaches_root = true; + else + { + auto target_position = std::find(c.chain_rtindexes.begin(), c.chain_rtindexes.end(), ch.target_rtindex); + CHECK(target_position != c.chain_rtindexes.end() && source_position < target_position); + } + CHECK(ch.condition_key.empty()); /* assigned by the shared injector */ + CHECK(ch.source_key.rtindex == ch.source_rtindex && ch.target_key.rtindex == ch.target_rtindex); + CHECK(ch.est_cost_ms > 10.49 && ch.est_cost_ms < 10.51); /* 3.5 * 3 * 1 MiB */ + cost += ch.est_cost_ms; + } + CHECK(reaches_root); + CHECK(c.est_cost_ms > cost - 1e-9 && c.est_cost_ms < cost + 1e-9); + std::string dump; + RptDumpForward(comps, &dump); + CHECK(dump.find("forward component root=1") == 0); + CHECK(dump.find("decision=inject") != std::string::npos); +} + +static void forward_selection_gates() +{ + RptTransferMgr forward = forward_star(); + std::vector comps; + + RptSelectForward(forward, forward_params(-1, 2000000), {}, size_one_mib, nullptr, &comps); + CHECK(!comps[0].accepted && comps[0].reason == "root rows below rpt.forward_min_root_rows"); + for (const RptForwardChannel &ch : comps[0].channels) + CHECK(ch.condition_key.empty()); + + RptSelectForward(forward, forward_params(1.0, 0), {}, size_one_mib, nullptr, &comps); + CHECK(!comps[0].accepted && + comps[0].reason == "estimated exchange cost above rpt.forward_max_exchange_ms"); + + RptSelectForward(forward, forward_params(-1, 0), {}, size_declined, nullptr, &comps); + CHECK(!comps[0].accepted && comps[0].reason.find("filter sizing declined") == 0); + + /* nothing filtered: no table can produce, so there is no chain */ + RptTransferMgr analysis; + vertex(analysis, 1, 1000000, false); + vertex(analysis, 2, 100, false); + connect(analysis, edge(1, 2)); + RptBuildClasses(&analysis); + RptBuildTransferPlan(NULL, &analysis); + RptTransferMgr bare; + RptBuildForwardPlan(&analysis, &bare); + RptSelectForward(bare, forward_params(-1, 0), {}, size_one_mib, nullptr, &comps); + for (const RptForwardComponent &c : comps) + CHECK(!c.accepted && c.reason == "no forward edges"); + + /* ids restart per call and only accepted components consume them */ + RptSelectForward(forward, forward_params(-1, 0), {}, size_one_mib, nullptr, &comps); + CHECK(comps[0].channels[0].condition_id == 0); +} + +/* Snowflake: fact 1 joins dimension 2 on 1.1 = 2.1, dimension 2 joins + * subdimension 3 on 2.2 = 3.1 -- a genuine chain 3 -> 2 -> 1. Colocated + * means 2 and 3 are both hash-distributed on their join columns, so 3 -> 2 + * is a local hop; 2 -> 1 never is. */ +static RptTransferMgr forward_chain(bool colocated) +{ + RptTransferMgr analysis; + vertex(analysis, 1, 1000000, false); + vertex(analysis, 2, 100, true); + vertex(analysis, 3, 50, true); + for (auto &v : analysis.vertices) + hash_policy(v); + analysis.vertices[analysis.vertex_index_by_rtindex[2]].distkeys = {(AttrNumber) (colocated ? 2 : 3)}; + connect(analysis, edge(1, 2)); + connect(analysis, edge(2, 3, false, false, 2, 1)); + RptBuildClasses(&analysis); + rpt_max_chain_depth = 4; + RptBuildTransferPlan(NULL, &analysis); + RptTransferMgr forward; + RptBuildForwardPlan(&analysis, &forward); + return forward; +} + +static std::vector sized_rows; + +static bool size_recording(double rows, int64 *elems, int64 *payload, int64 *planned) +{ + sized_rows.push_back(rows); + return size_one_mib(rows, elems, payload, planned); +} + +static double ndv_small_dims(const RptVertexInfo &v, AttrNumber) +{ + return v.rtindex == 1 ? 1000 : 100; +} + +static double ndv_large_dims(const RptVertexInfo &v, AttrNumber) +{ + return v.rtindex == 1 ? 100 : 1000; +} + +static void forward_selection_chain_and_selectivity() +{ + RptTransferMgr forward = forward_chain(false); + std::vector comps; + RptForwardParams params = forward_params(-1, 0); + std::map est; + + params.max_selectivity = 0.35; + est[2].rows = 100; est[2].selectivity = 0.1; + est[3].rows = 50; est[3].selectivity = 0.5; + sized_rows.clear(); + RptSelectForward(forward, params, est, size_recording, nullptr, &comps); + CHECK(comps.size() == 1 && comps[0].accepted); + CHECK(comps[0].chain_rtindexes == std::vector({3, 2})); + CHECK(comps[0].channels.size() == 2); + CHECK(comps[0].channels[0].source_rtindex == 3 && comps[0].channels[0].target_rtindex == 2); + CHECK(comps[0].channels[1].source_rtindex == 2 && comps[0].channels[1].target_rtindex == 1); + /* 2 keeps 0.1 of itself times 0.5 from 3; the root keeps what 2 keeps */ + CHECK(comps[0].est_selectivity > 0.0499 && comps[0].est_selectivity < 0.0501); + /* filters are sized for rows after the vertex's own reducer quals */ + CHECK(sized_rows == std::vector({25, 10})); + + /* NDV: a smaller source domain keeps proportionally less of the target */ + RptSelectForward(forward, params, est, size_one_mib, ndv_small_dims, &comps); + CHECK(comps[0].est_selectivity > 0.00499 && comps[0].est_selectivity < 0.00501); + /* a larger source domain cannot keep more than its own fraction */ + RptSelectForward(forward, params, est, size_one_mib, ndv_large_dims, &comps); + CHECK(comps[0].est_selectivity > 0.0499 && comps[0].est_selectivity < 0.0501); + + /* Quals dropped in the reducer: nothing selective is left, the + * filters grow to the whole table, and lambda refuses the chain. */ + est[2].selectivity = 1; est[2].quals_dropped = true; + est[3].selectivity = 1; est[3].quals_dropped = true; + sized_rows.clear(); + RptSelectForward(forward, params, est, size_recording, nullptr, &comps); + CHECK(!comps[0].accepted && + comps[0].reason == "estimated root selectivity not below rpt.forward_max_selectivity"); + CHECK(sized_rows == std::vector({50, 100})); + for (const RptForwardChannel &ch : comps[0].channels) + CHECK(ch.condition_key.empty()); + + /* Just below lambda passes, exactly lambda does not; 1 disables it. */ + est[2].selectivity = 0.34; + RptSelectForward(forward, params, est, size_one_mib, nullptr, &comps); + CHECK(comps[0].accepted); + est[2].selectivity = 0.35; + RptSelectForward(forward, params, est, size_one_mib, nullptr, &comps); + CHECK(!comps[0].accepted); + params.max_selectivity = 1; + RptSelectForward(forward, params, est, size_one_mib, nullptr, &comps); + CHECK(comps[0].accepted); +} + +/* A local hop consumed only inside the reducer is in-process and free; the + * hop into the root stays on Anser. */ +static void forward_selection_in_process() +{ + RptTransferMgr forward = forward_chain(true); + std::vector comps; + RptForwardParams params = forward_params(-1, 0); + + RptSelectForward(forward, params, {}, size_one_mib, nullptr, &comps); + CHECK(comps[0].accepted && comps[0].channels.size() == 2); + const RptForwardChannel &inner = comps[0].channels[0]; + const RptForwardChannel &into_root = comps[0].channels[1]; + CHECK(inner.target_rtindex == 2 && inner.local && inner.in_process && inner.est_cost_ms == 0); + CHECK(into_root.target_rtindex == 1 && !into_root.local && !into_root.in_process); + CHECK(comps[0].est_cost_ms > 10.49 && comps[0].est_cost_ms < 10.51); + std::string dump; + RptDumpForward(comps, &dump); + CHECK(dump.find(" local in-process\n") != std::string::npos); + + forward = forward_chain(false); + RptSelectForward(forward, params, {}, size_one_mib, nullptr, &comps); + for (const RptForwardChannel &ch : comps[0].channels) + CHECK(!ch.local && !ch.in_process); +} + +static void forward_delivery_process_counts() +{ + auto schedule = forward_chain(false); + auto params = forward_params(-1, 0); + params.delivery_ms_per_mib = 0.8; + for (const auto &vertex : schedule.vertices) + params.main_consumer_processes[vertex.rtindex] = 6; + std::vector components; + RptSelectForward(schedule, params, {}, size_one_mib, nullptr, &components); + for (const auto &channel : components[0].channels) + { + double expected = 10.5 + 0.8 * + (channel.target_rtindex == components[0].root_rtindex ? 6 : 3); + CHECK(std::abs(channel.est_cost_ms - expected) < 1e-6); + } +} + +static void qual_function_allowlist() +{ + CHECK(RptQualFunctionSafe(F_INT4LT)); + CHECK(RptQualFunctionSafe(F_INT8EQ)); + CHECK(RptQualFunctionSafe(F_INT24NE)); + CHECK(RptQualFunctionSafe(F_OIDGE)); + CHECK(!RptQualFunctionSafe(F_INT4DIV)); + CHECK(!RptQualFunctionSafe(F_INT4PL)); + CHECK(!RptQualFunctionSafe(1000000)); /* user function, even if leakproof */ + CHECK(!RptQualFunctionSafe(InvalidOid)); + CHECK(RptQualHashFunctionSafe(F_HASHINT4)); + CHECK(!RptQualHashFunctionSafe(F_INT4EQ)); + CHECK(!RptQualHashFunctionSafe(1000000)); +} + +static void raw_datum_key_types() +{ + for (Oid type : {BOOLOID, CHAROID, INT2OID, INT4OID, INT8OID, OIDOID, + XIDOID, CIDOID, DATEOID, TIMEOID, TIMESTAMPOID, + TIMESTAMPTZOID, MONEYOID, PG_LSNOID, XID8OID}) + CHECK(rpt_key_type_supported(type)); + for (Oid type : std::vector{InvalidOid, FLOAT4OID, FLOAT8OID, TEXTOID, NUMERICOID, + 1000000}) + CHECK(!rpt_key_type_supported(type)); + + /* Even if a custom type is by-value, refuse it before consulting its + * own default equality (the type-cache stub aborts if reached). */ + Var outer = {}, inner = {}; + outer.xpr.type = inner.xpr.type = T_Var; + outer.varno = OUTER_VAR; inner.varno = INNER_VAR; + outer.vartype = inner.vartype = 1000000; + ListCell args[2] = {}; + args[0].ptr_value = &outer; args[1].ptr_value = &inner; + List arguments = {}; + arguments.type = T_List; + arguments.length = arguments.max_length = 2; + arguments.elements = args; + OpExpr equality = {}; + equality.xpr.type = T_OpExpr; + equality.args = &arguments; + equality.opno = 1000001; + Var *out = NULL, *in = NULL; + Oid keytype = InvalidOid; + CHECK(!rpt_equality_clause((Node *) &equality, &out, &in, &keytype)); +} + +static void backward_selection() +{ + RptTransferMgr schedule; + vertex(schedule, 1, 1000, true); + vertex(schedule, 2, 10000, false); + vertex(schedule, 3, 10000, false); + schedule.root_rtindexes.insert(1); + RptBackwardConsumer decision; + decision.class_key = decision.producer_key = {1, 1}; + decision.consumer_key = {2, 1}; + decision.producer_rtindex = 1; + decision.consumer_rtindex = 2; + decision.producer_slice_index = 1; + decision.consumer_slice_index = 2; + decision.eligible = decision.strict_eligible = true; + decision.estimated_rows_removed = 9000; + decision.reason = "eligible"; + schedule.backward_consumers.push_back(decision); + RptForwardParams params = forward_params(-1, 0); + params.max_selectivity = 0.35; + params.delivery_ms_per_mib = 0.8; + std::vector classes; + std::map widths = {{1, 3}, {2, 6}}; /* parallel consumer */ + auto select = [&](RptFilterSizeFn size_fn = size_one_mib, + const std::vector &forward = {}) { + RptSelectBackward(schedule, forward, params, widths, size_fn, &classes); + }; + select(); + CHECK(classes.size() == 1 && classes[0].accepted); + CHECK(classes[0].consumers[0].accepted); + CHECK(classes[0].est_cost_ms > 15.29 && classes[0].est_cost_ms < 15.31); + CHECK(classes[0].estimated_producer_rows == 1000); + params.max_exchange_ms = 15; + select(); + CHECK(!classes[0].accepted && classes[0].reason.find("exchange cost") != std::string::npos); + CHECK(!classes[0].consumers[0].accepted); + params.max_exchange_ms = -1; + schedule.backward_consumers[0].estimated_rows_removed = 0; + select(); + CHECK(!classes[0].accepted); + CHECK(classes[0].consumers[0].reason.find("member selectivity") != std::string::npos); + RptForwardComponent forward; + forward.root_rtindex = 1; forward.accepted = true; forward.est_selectivity = 0.1; + select(size_one_mib, {forward}); + CHECK(classes[0].accepted && classes[0].estimated_producer_rows == 100); + CHECK(classes[0].consumers[0].estimated_rows_removed == 9000); + select([](double rows, int64 *elems, int64 *payload, int64 *bytes) { + *elems = rows; *payload = *bytes = rows * 2; return true; + }, {forward}); + CHECK(classes[0].total_elems == 100 && classes[0].planned_bytes == 200); + forward.accepted = false; + select(size_one_mib, {forward}); + CHECK(!classes[0].accepted && classes[0].estimated_producer_rows == 1000); + schedule.backward_consumers[0].estimated_rows_removed = -1; + select(); + CHECK(!classes[0].accepted); /* unknown NDV */ + params.max_selectivity = 1; + select(); + CHECK(classes[0].accepted); + params.max_exchange_ms = 0; + select(); CHECK(!classes[0].accepted); /* unknown NDV still obeys budget */ + params.max_exchange_ms = -1; + select([](double, int64 *, int64 *, int64 *) { return false; }); + CHECK(!classes[0].accepted && classes[0].reason == "backward filter sizing declined"); + widths[1] = 0; + select(); CHECK(!classes[0].accepted); + widths[1] = 3; + widths[2] = 0; + select(); CHECK(!classes[0].accepted); + widths[2] = 3; + schedule.root_rtindexes.clear(); + select(); CHECK(classes[0].reason == "bridge-led class is deferred"); + schedule.root_rtindexes.insert(1); + /* A refused first member must not hide a later member's producer width. */ + auto refused = schedule.backward_consumers[0]; + refused.eligible = false; refused.reason = "backward consumer cap exceeded"; + refused.producer_slice_index = -1; + schedule.backward_consumers.insert(schedule.backward_consumers.begin(), refused); + select(); + CHECK(classes[0].accepted && !classes[0].consumers[0].accepted); + CHECK(classes[0].consumers[1].accepted); +} + +static void unfiltered_backward_edges() +{ + for (bool filtered : {false, true}) + { + RptTransferMgr analysis; + vertex(analysis, 1, 10000, filtered); + vertex(analysis, 2, 1000, filtered); + connect(analysis, edge(1, 2)); + RptBuildClasses(&analysis); + CHECK(!analysis.logical_neighbor_matrix.at(1).empty()); + if (!filtered) CHECK(analysis.neighbor_matrix.at(1).empty()); + RptTransferMgr runtime; + RptBuildForwardPlan(&analysis, &runtime); + CHECK(runtime.root_rtindexes == std::set({1})); + CHECK(runtime.graph.at(1).backward_stage.out.size() == 1); + CHECK(runtime.graph.at(1).backward_stage.out[0].other_rtindex == 2); + CHECK(runtime.graph.at(2).backward_stage.in.size() == 1); + if (!filtered) CHECK(runtime.graph.at(1).forward_stage.in.empty()); + } +} + +int main() +{ + unfiltered_backward_edges(); + forward_delivery_process_counts(); + backward_selection(); + raw_datum_key_types(); + qual_function_allowlist(); + forward_selection_chain_and_selectivity(); + forward_selection_in_process(); + forward_selection_star(); + forward_selection_gates(); + bypass_direction(false); + bypass_direction(true); + protection_and_idempotence(); + asymmetric_shape(); + unfiltered_cycle(); + strict_backward_eligibility(); + subplans_and_external_parameters_are_refused(); + every_global_producer_is_required(); + backward_consumer_cap(); + bridges_and_locality(); + local_star_and_backward(); + replicated_and_compound_policies(); + index_only_endpoint(); + join_protection(); + directed_parent_choice(); + forward_root_includes_unfiltered_fact(); + error_unwinds(); + return 0; +} diff --git a/gpcontrib/robust_predicate_transfer/test/inject_test.cpp b/gpcontrib/robust_predicate_transfer/test/inject_test.cpp new file mode 100644 index 00000000000..20f8b683bf8 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/test/inject_test.cpp @@ -0,0 +1,434 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * inject_test.cpp + * Deterministic tests of the production graph, planner, resolver and + * PostgreSQL/C++ error boundary, using synthetic plans and C-call stubs. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/test/inject_test.cpp + * + *------------------------------------------------------------------------- + */ +#include "../src/rptinject.cpp" +#include "../src/rptforward.cpp" +#include "../src/rptbackwardplan.cpp" +#include "../src/rptbackward.cpp" +extern "C" { +#include "ansertlist.h" +} +#include +#include +#include +#include +#undef vsnprintf +#undef fprintf +#define CHECK(c) do { if (!(c)) { fprintf(stderr, "check failed: %s:%d: %s\n", __FILE__, __LINE__, #c); abort(); } } while (0) + +static std::vector allocations; +struct BuiltFilter +{ + bool producer; + Plan *child; + AttrNumber attno; + uint32 condition_id; + int processes; + bool defer_first; +}; +static std::vector built_filters; + +extern "C" { +sigjmp_buf *PG_exception_stack = NULL; +ErrorContextCallback *error_context_stack = NULL; +MemoryContext CurrentMemoryContext = NULL; +int dispatch_nest_level = 0; +void *palloc(Size n) { void *p = malloc(n); CHECK(p); allocations.push_back(p); return p; } +void *palloc0(Size n) { void *p = palloc(n); memset(p, 0, n); return p; } +void *MemoryContextAllocZero(MemoryContext, Size n) { return palloc0(n); } +void *MemoryContextAllocZeroAligned(MemoryContext, Size n) { return palloc0(n); } +bool errstart_cold(int, const char *) { abort(); } +void ExceptionalCondition(const char *, const char *, int) { abort(); } +bool errstart(int, const char *) { abort(); } +void errfinish(const char *, int, const char *) { abort(); } +void errmsg_internal(const char *, ...) { abort(); } +char *psprintf(const char *fmt, ...) +{ + char *p = (char *) palloc(128); + va_list args; va_start(args, fmt); vsnprintf(p, 128, fmt, args); va_end(args); + return p; +} +static List *append_cell(List *list, NodeTag tag, ListCell cell) +{ + int n = list_length(list); + List *out = (List *) palloc0(sizeof(List)); + out->type = tag; out->length = out->max_length = n + 1; + out->elements = (ListCell *) palloc(sizeof(ListCell) * (n + 1)); + if (n) memcpy(out->elements, list->elements, sizeof(ListCell) * n); + out->elements[n] = cell; + return out; +} +List *list_make1_impl(NodeTag tag, ListCell c) { return append_cell(NIL, tag, c); } +List *lappend(List *l, void *p) { return append_cell(l, T_List, list_make_ptr_cell(p)); } +List *lappend_oid(List *l, Oid o) { return append_cell(l, T_OidList, list_make_oid_cell(o)); } +List *list_copy(const List *l) +{ + List *out = NIL; + for (int i = 0; i < list_length(l); ++i) out = append_cell(out, l->type, l->elements[i]); + return out; +} +Node *makeBoolConst(bool value, bool isnull) +{ + Const *c = makeNode(Const); c->consttype = BOOLOID; c->constvalue = BoolGetDatum(value); + c->constisnull = isnull; return (Node *) c; +} +static int max_plan_node_id(Plan *p) +{ + if (!p) return 0; + int m = Max(p->plan_node_id, Max(max_plan_node_id(p->lefttree), max_plan_node_id(p->righttree))); + if (IsA(p, Sequence)) + { + ListCell *lc; + foreach(lc, ((Sequence *) p)->subplans) m = Max(m, max_plan_node_id((Plan *) lfirst(lc))); + } + return m; +} +TargetEntry *get_tle_by_resno(List *list, AttrNumber resno) +{ + ListCell *lc; + foreach(lc, list) + if (((TargetEntry *) lfirst(lc))->resno == resno) + return (TargetEntry *) lfirst(lc); + return NULL; +} +void *copyObjectImpl(const void *input) +{ + if (input == NULL) return NULL; + const Node *node = (const Node *) input; + if (IsA(node, Var)) + { + Var *out = (Var *) palloc(sizeof(Var)); + memcpy(out, input, sizeof(Var)); return out; + } + if (IsA(node, TargetEntry)) + { + TargetEntry *out = (TargetEntry *) palloc(sizeof(TargetEntry)); + memcpy(out, input, sizeof(TargetEntry)); + out->expr = (Expr *) copyObjectImpl(out->expr); return out; + } + CHECK(IsA(node, List)); + List *out = NIL; ListCell *lc; + foreach(lc, (List *) input) out = lappend(out, copyObjectImpl(lfirst(lc))); + return out; +} +Node *expression_tree_mutator_impl(Node *node, tree_mutator_callback mutator, void *ctx) +{ + if (!node) return NULL; + if (IsA(node, Var)) return (Node *) copyObjectImpl(node); + if (IsA(node, TargetEntry)) + { + TargetEntry *out = (TargetEntry *) copyObjectImpl(node); + out->expr = (Expr *) mutator((Node *) ((TargetEntry *) node)->expr, ctx); + return (Node *) out; + } + CHECK(IsA(node, List)); + List *out = NIL; ListCell *lc; + foreach(lc, (List *) node) out = lappend(out, mutator((Node *) lfirst(lc), ctx)); + return (Node *) out; +} +int AnserSliceProducers(PlannedStmt *, int slice_index) +{ + return slice_index >= 0 ? 3 : 0; +} +CustomScan *AnserBuildBloomProducerScan(Plan *child, AttrNumber attno, uint32 condition_id, + const char *, int64, Size, int64, int processes) +{ + built_filters.push_back({true, child, attno, condition_id, processes, false}); + CustomScan *cs = makeNode(CustomScan); + cs->custom_plans = list_make1(child); + return cs; +} +CustomScan *AnserBuildBloomConsumerScan(Plan *child, AttrNumber attno, uint32 condition_id, + const char *, int64, Size, int64, int processes, bool defer_first) +{ + built_filters.push_back({false, child, attno, condition_id, processes, defer_first}); + CustomScan *cs = makeNode(CustomScan); + cs->custom_plans = list_make1(child); + return cs; +} +int AnserMaxPlanNodeId(PlannedStmt *s) +{ + int m = max_plan_node_id(s->planTree); ListCell *lc; + foreach(lc, s->subplans) m = Max(m, max_plan_node_id((Plan *) lfirst(lc))); + return m; +} +} + +static void collect_plan_node_ids(Plan *p, std::set &plan_node_ids) +{ + if (!p) return; + CHECK(plan_node_ids.insert(p->plan_node_id).second); + collect_plan_node_ids(p->lefttree, plan_node_ids); collect_plan_node_ids(p->righttree, plan_node_ids); + if (IsA(p, Sequence)) + { + ListCell *lc; + foreach(lc, ((Sequence *) p)->subplans) collect_plan_node_ids((Plan *) lfirst(lc), plan_node_ids); + } +} + +static void layout(bool existing) +{ + PlannedStmt stmt = {}; + Result main = {}; main.plan.type = T_Result; main.plan.plan_node_id = 5; + stmt.planTree = &main.plan; + PlanSlice initial[2] = {}; + initial[0].sliceIndex = 0; initial[0].parentIndex = -1; + initial[1].sliceIndex = 1; initial[1].parentIndex = -1; + stmt.slices = initial; stmt.numSlices = existing ? 2 : 1; + Result prior = {}; prior.plan.type = T_Result; prior.plan.plan_node_id = 90; + int prior_slice = 1; + SubPlan old_init = {}; + if (existing) + { + stmt.subplans = list_make1(&prior); + stmt.subplan_sliceIds = &prior_slice; + stmt.paramExecTypes = list_make1_oid(INT4OID); + stmt.planTree->initPlan = list_make1(&old_init); + } + Result child = {}; child.plan.type = T_Result; child.plan.plan_node_id = 110; + List *children = list_make1(&child); + int old_slices = stmt.numSlices; + RptAttachReducerInitPlan(&stmt, children, 8); + CHECK(stmt.numSlices == old_slices + 2); + CHECK(memcmp(stmt.slices, initial, sizeof(PlanSlice) * old_slices) == 0); + for (int i = 0; i < stmt.numSlices; ++i) CHECK(stmt.slices[i].sliceIndex == i); + PlanSlice &r = stmt.slices[old_slices], &s = stmt.slices[old_slices + 1]; + CHECK(r.parentIndex == -1 && r.gangType == GANGTYPE_UNALLOCATED && r.segindex == -1); + CHECK(s.parentIndex == old_slices && s.gangType == GANGTYPE_PRIMARY_READER); + CHECK(s.numsegments == 8 && s.segindex == 0 && s.parallel_workers == 0); + CHECK(!s.directDispatch.isDirectDispatch && s.directDispatch.contentIds == NIL); + CHECK(list_length(stmt.subplans) == (existing ? 2 : 1)); + CHECK(list_length(stmt.paramExecTypes) == (existing ? 2 : 1)); + CHECK(llast_oid(stmt.paramExecTypes) == BOOLOID); + CHECK(list_length(stmt.planTree->initPlan) == (existing ? 2 : 1)); + if (existing) + { + CHECK(stmt.subplan_sliceIds[0] == 1 && linitial(stmt.subplans) == &prior); + CHECK(linitial_oid(stmt.paramExecTypes) == INT4OID); + CHECK(linitial(stmt.planTree->initPlan) == &old_init); + } + SubPlan *sp = (SubPlan *) llast(stmt.planTree->initPlan); + CHECK(sp->is_initplan && sp->subLinkType == EXISTS_SUBLINK); + CHECK(sp->plan_id == list_length(stmt.subplans)); + CHECK(list_length(sp->setParam) == 1 && linitial_int(sp->setParam) == (existing ? 1 : 0)); + CHECK(sp->parParam == NIL && sp->args == NIL); + CHECK(stmt.subplan_sliceIds[sp->plan_id - 1] == old_slices); + Motion *motion = (Motion *) llast(stmt.subplans); + CHECK(IsA(motion, Motion) && motion->motionID == old_slices + 1); + CHECK(motion->motionType == MOTIONTYPE_GATHER); + CHECK(motion->plan.flow->flotype == FLOW_SINGLETON); + Sequence *seq = (Sequence *) motion->plan.lefttree; + CHECK(IsA(seq, Sequence) && list_length(seq->subplans) == 2); + CHECK(list_length(children) == 1); + CHECK(linitial(seq->subplans) == &child); + CHECK(seq->plan.flow->flotype == FLOW_PARTITIONED && seq->plan.flow->numsegments == 8); + Result *empty = (Result *) llast(seq->subplans); + CHECK(IsA(empty, Result) && IsA(empty->resconstantqual, List)); + List *quals = (List *) empty->resconstantqual; + CHECK(list_length(quals) == 1 && IsA(linitial(quals), Const)); + Const *qual = (Const *) linitial(quals); + CHECK(qual->consttype == BOOLOID && !qual->constisnull); + CHECK(!DatumGetBool(qual->constvalue)); + std::set plan_node_ids; + collect_plan_node_ids(stmt.planTree, plan_node_ids); + ListCell *lc; foreach(lc, stmt.subplans) collect_plan_node_ids((Plan *) lfirst(lc), plan_node_ids); + CHECK(plan_node_ids.size() == (existing ? 6U : 5U)); + int previous_max = 110; + CHECK(empty->plan.plan_node_id > previous_max && + seq->plan.plan_node_id > previous_max && motion->plan.plan_node_id > previous_max); +} +static void backward_stacks(bool refused, bool materialize, bool with_forward) +{ + RptTransferMgr schedule; + SeqScan scans[2] = {}; + TargetEntry entries[2] = {}; + Var keys[2] = {}; + Hash hash = {}; hash.plan.type = T_Hash; + Motion motion = {}; motion.plan.type = T_Motion; + HashJoin join = {}; join.join.plan.type = T_HashJoin; + join.join.plan.righttree = &hash.plan; + join.join.plan.lefttree = &motion.plan; + PlannedStmt stmt = {}; stmt.type = T_PlannedStmt; + stmt.planTree = &join.join.plan; + PlanSlice slices[3] = {}; stmt.slices = slices; stmt.numSlices = 3; + for (int i = 0; i < 2; ++i) + { + scans[i].scan.plan.type = T_SeqScan; + scans[i].scan.scanrelid = i + 1; + keys[i].xpr.type = T_Var; + keys[i].varno = i + 1; keys[i].varattno = 2; + entries[i].xpr.type = T_TargetEntry; entries[i].expr = (Expr *) &keys[i]; + entries[i].resno = 1; /* physical attno differs from output position */ + scans[i].scan.plan.targetlist = list_make1(&entries[i]); + RptVertexInfo vertex = {}; + vertex.rtindex = i + 1; vertex.scan = &scans[i].scan.plan; + vertex.cardinality = i == 0 ? 1000 : 10000; + schedule.vertex_index_by_rtindex[i + 1] = i; + schedule.vertices.push_back(vertex); + } + hash.plan.lefttree = &scans[0].scan.plan; + motion.plan.lefttree = &scans[1].scan.plan; + schedule.root_rtindexes.insert(1); + RptBackwardConsumer decision; + decision.class_key = decision.producer_key = {1, 2}; + decision.consumer_key = {2, 2}; + decision.producer_rtindex = 1; decision.consumer_rtindex = 2; + decision.producer_parent = &hash.plan; decision.consumer_parent = &motion.plan; + decision.producer_attno = decision.consumer_attno = 1; + decision.producer_slice_index = 1; decision.consumer_slice_index = 2; + decision.eligible = true; decision.defer_first = true; + decision.estimated_rows_removed = 9000; + schedule.backward_consumers.push_back(decision); + /* A second class on the same scans exercises repeated stacking/relinking. */ + decision.class_key.attno = 3; + schedule.backward_consumers.push_back(decision); + RptInjectionState state; + state.parents[&scans[0].scan.plan] = &hash.plan; + state.parents[&scans[1].scan.plan] = &motion.plan; + for (auto &scan : scans) state.tops[&scan.scan.plan] = &scan.scan.plan; + state.next_plan_node_id = 100; + std::vector forward_components(1); + forward_components[0].accepted = true; + for (int i = 0; i < 7; ++i) + { + RptForwardChannel channel; + channel.source_rtindex = i + 1; channel.target_rtindex = i + 2; + forward_components[0].channels.push_back(channel); + } + rpt_assign_forward_channels(forward_components, state); + CHECK(state.next_condition_id == 7); + for (int i = 0; i < 7; ++i) + CHECK(forward_components[0].channels[i].condition_id == uint32(i)); + CustomScan forward = {}; + if (with_forward) + { + forward.scan.plan.type = T_CustomScan; + forward.custom_plans = list_make1(&scans[0]); + RptRelinkStack(&state, &scans[0].scan.plan, &forward.scan.plan); + } + if (refused) + schedule.backward_consumers[0].consumer_attno = + schedule.backward_consumers[1].consumer_attno = 99; + Hash hash_before = hash; Motion motion_before = motion; + auto tops_before = state.tops; + PlanSlice slices_before[3]; memcpy(slices_before, slices, sizeof(slices)); + size_t allocations_before = allocations.size(); + built_filters.clear(); + RptForwardParams params; params.max_exchange_ms = -1; + std::string trace; + bool injected = false; + RptInjectBackward(&stmt, schedule, {}, params, + [](double, int64 *elems, int64 *payload, int64 *bytes) { + *elems = 1000; *payload = *bytes = 1024; return true; + }, materialize, &state, &trace, &injected); + CHECK(stmt.slices == slices && stmt.numSlices == 3); + CHECK(memcmp(slices_before, slices, sizeof(slices)) == 0); + CHECK(stmt.subplans == NIL && stmt.paramExecTypes == NIL); + if (refused || !materialize) + { + CHECK(!injected && built_filters.empty()); + CHECK(memcmp(&hash_before, &hash, sizeof(hash)) == 0); + CHECK(memcmp(&motion_before, &motion, sizeof(motion)) == 0); + CHECK(state.tops == tops_before && allocations.size() == allocations_before); + CHECK(state.next_plan_node_id == 100); + CHECK(trace.find(refused ? "placement changed" : "decision=inject") != std::string::npos); + } + else + { + CHECK(injected && built_filters.size() == 4); + CHECK(state.next_condition_id == 9 && state.next_plan_node_id == 104); + for (int i = 0; i < 2; ++i) + { + CHECK(built_filters[2*i].producer && !built_filters[2*i+1].producer); + CHECK(built_filters[2*i].condition_id == uint32(7+i)); + CHECK(built_filters[2*i+1].condition_id == uint32(7+i)); + CHECK(built_filters[2*i].processes == 3 && built_filters[2*i+1].defer_first); + CHECK(built_filters[2*i].attno == 1 && built_filters[2*i+1].attno == 1); + } + Plan *producer = hash.plan.lefttree, *consumer = motion.plan.lefttree; + CHECK(producer == state.tops.at(&scans[0].scan.plan)); + CHECK(consumer == state.tops.at(&scans[1].scan.plan)); + CHECK(producer->plan_node_id == 102 && consumer->plan_node_id == 103); + producer = (Plan *) linitial(((CustomScan *) producer)->custom_plans); + consumer = (Plan *) linitial(((CustomScan *) consumer)->custom_plans); + CHECK(producer->plan_node_id == 100 && consumer->plan_node_id == 101); + CHECK(linitial(((CustomScan *) producer)->custom_plans) == tops_before.at(&scans[0].scan.plan)); + CHECK(linitial(((CustomScan *) consumer)->custom_plans) == tops_before.at(&scans[1].scan.plan)); + } +} + +static void stacked_deparse_targetlists() +{ + SeqScan base = {}; base.scan.plan.type = T_SeqScan; + Var columns[2] = {}; TargetEntry entries[2] = {}; + for (int i = 0; i < 2; ++i) + { + columns[i].xpr.type = T_Var; columns[i].varno = 42; + columns[i].varattno = i + 7; + entries[i].xpr.type = T_TargetEntry; entries[i].expr = (Expr *) &columns[i]; + entries[i].resno = i + 1; entries[i].resjunk = i == 1; + base.scan.plan.targetlist = lappend(base.scan.plan.targetlist, &entries[i]); + } + CustomScan stack[8] = {}; Plan *child = &base.scan.plan; + for (int depth = 0; depth < 8; ++depth) + { + CustomScan *node = &stack[depth]; node->scan.plan.type = T_CustomScan; + node->custom_scan_tlist = AnserFilterScanTlist(child); + for (int i = 0; i < 2; ++i) + { + TargetEntry *resolved = get_tle_by_resno(node->custom_scan_tlist, i + 1); + CHECK(((Var *) resolved->expr)->varno == 42); + CHECK(((Var *) resolved->expr)->varattno == i + 7); + CHECK(resolved->resjunk == (i == 1)); + Var *var = (Var *) copyObjectImpl(&columns[i]); + var->varno = INDEX_VAR; var->varattno = i + 1; + TargetEntry *tle = (TargetEntry *) copyObjectImpl(&entries[i]); + tle->expr = (Expr *) var; + node->scan.plan.targetlist = lappend(node->scan.plan.targetlist, tle); + } + node->custom_plans = list_make1(child); + child = &node->scan.plan; + } + /* Resolve the referenced slot, not the tlist's positional order. */ + ((Var *) ((TargetEntry *) linitial(stack[7].scan.plan.targetlist))->expr)->varattno = 2; + List *resolved = AnserFilterScanTlist(&stack[7].scan.plan); + CHECK(((Var *) ((TargetEntry *) linitial(resolved))->expr)->varattno == 8); + CHECK(columns[0].varno == 42 && columns[0].varattno == 7); +} + +int main() +{ + stacked_deparse_targetlists(); + backward_stacks(false, true, true); + backward_stacks(false, true, false); + backward_stacks(true, true, true); + backward_stacks(false, false, true); + layout(false); layout(true); + for (void *p : allocations) free(p); + return 0; +} diff --git a/gpcontrib/robust_predicate_transfer/test/survey_test.py b/gpcontrib/robust_predicate_transfer/test/survey_test.py new file mode 100644 index 00000000000..5979cf7011e --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/test/survey_test.py @@ -0,0 +1,163 @@ +#------------------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +"""Tests for survey accounting and non-executing SQL generation.""" +import importlib.util +import json +from pathlib import Path +import unittest +import tempfile +from unittest.mock import patch +from contextlib import redirect_stdout +import io +import subprocess + +spec = importlib.util.spec_from_file_location( + "rpt_survey", Path(__file__).resolve().parents[1] / "scripts" / "survey.py") +survey = importlib.util.module_from_spec(spec) +spec.loader.exec_module(survey) + + +class SurveyTests(unittest.TestCase): + def test_no_region_is_zero_not_missing_data(self): + self.assertEqual(survey.parse_dump("no transfer regions\n")["strict"], 0) + with self.assertRaises(ValueError): + survey.parse_dump("transfer order: 1 2\n") + + def test_comparison_counts_channels_not_both_endpoints(self): + explain = [{"Plan": {"Node Type": "Custom Scan", + "Custom Plan Provider": "Anser Bloom Producer", + "Plans": [{"Custom Plan Provider": "Anser Bloom Consumer"}]}, + "Optimizer": "GPORCA"}] + output = (survey.BEGIN_DUMP + "\n" + + "survey classes=2 root_classes=1 global_edges=3 rule9=2 " + "strict=1 eligible=1 known_rows_removed=250 unknown_estimates=0\n" + + survey.BEGIN_ANSER + "\n" + json.dumps(explain) + "\n" + survey.END + "\n") + metrics, dump, plan = survey.parse_output(output) + self.assertEqual(metrics["anser_joins"], 1) + self.assertEqual(metrics["strict"], 1) + self.assertEqual(metrics["anser_optimizer_used"], "GPORCA") + self.assertEqual(metrics["known_rows_removed"], 250) + + def test_largest_root_runtime_metrics_are_separate(self): + dump = ("survey classes=1 root_classes=1 global_edges=1 rule9=1 strict=1 eligible=1 " + "known_rows_removed=90 unknown_estimates=0\n" + "runtime survey classes=2 root_classes=1 strict=2 eligible=1\n" + "backward consumer 2 root=1 rep=1.1 decision=inject\n" + "backward consumer 3 root=1 rep=1.1 decision=backward consumer cap exceeded\n") + result = survey.parse_dump(dump) + self.assertEqual(result["classes"], 1) + self.assertEqual(result["runtime_classes"], 2) + self.assertEqual(result["runtime_strict"], 2) + self.assertEqual(result["runtime_injected_consumers"], 1) + self.assertEqual(result["runtime_refusals"], {"backward consumer cap exceeded": 1}) + + def test_incomplete_output_cannot_be_reported_as_zero(self): + with self.assertRaises(ValueError): + survey.parse_output(survey.BEGIN_DUMP + "\nno transfer regions\n") + with self.assertRaises(ValueError): + survey.parse_dump("survey classes=1 root_classes=1 global_edges=1 " + "rule9=1 strict=2 eligible=1 known_rows_removed=0 unknown_estimates=0") + + def test_queries_are_planned_in_read_only_transaction(self): + sql = survey.survey_sql("SELECT 'O''Brien' -- trailing comment", "on", 1000, "imdb,public") + self.assertIn("BEGIN READ ONLY;", sql) + self.assertIn("SET LOCAL rpt.enable = off;", sql) + self.assertIn("SET LOCAL rpt.backward = on;", sql) + self.assertIn("SET LOCAL anser.runtime_filter = on;", sql) + self.assertIn("EXPLAIN (FORMAT JSON) SELECT 'O''Brien' -- trailing comment\n;", sql) + self.assertNotIn("EXPLAIN ANALYZE", sql) + self.assertTrue(sql.endswith("ROLLBACK;")) + self.assertEqual(survey.sql_literal("a'b"), "'a''b'") + + def test_templates_count_selects_not_files(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) + (path / "14.sql").write_text("-- variant a\nSELECT 14; /* variant b */ SELECT 15; -- end\n") + (path / "15.sql").write_text("WITH revenue AS (SELECT 1 n) SELECT * FROM revenue;") + queries = survey.load_queries(path) + self.assertEqual(len(queries), 3) + self.assertEqual([q["statement"] for q in queries], [1, 2, 1]) + self.assertEqual(survey.EXPECTED["TPC-DS"], 103) + + def test_standard_tpcds_layout_and_split_variants_have_103_statements(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) + for number in range(1, 100): + sql = "SELECT %d;" % number + if number in (14, 23, 24, 39): + sql += " SELECT %d;" % number + (path / (str(number) + ".sql")).write_text(sql) + self.assertEqual(len(list(path.glob("*.sql"))), 99) + self.assertEqual(len(survey.load_queries(path)), 103) + for number in (14, 23, 24, 39): + (path / (str(number) + ".sql")).unlink() + for suffix in ("a", "b"): + (path / (str(number) + suffix + ".sql")).write_text("SELECT %d;" % number) + self.assertEqual(len(list(path.glob("*.sql"))), 103) + self.assertEqual(len(survey.load_queries(path)), 103) + + def test_driver_completes_by_statement_count(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + arguments = ["survey.py"] + for suite, flag in [("JOB", "job"), ("TPC-H", "tpch"), ("TPC-DS", "tpcds")]: + path = root / suite + path.mkdir() + (path / "one.sql").write_text("SELECT 1; SELECT 2;" if flag == "tpcds" else "SELECT 1;") + arguments += ["--" + flag, str(path)] + output = root / "output" + arguments += ["--output", str(output)] + text = (survey.BEGIN_DUMP + "\nno transfer regions\n" + survey.BEGIN_ANSER + + "\n" + json.dumps([{"Plan": {"Node Type": "Result"}}]) + "\n" + survey.END + "\n") + result = subprocess.CompletedProcess([], 0, stdout=text, stderr="") + with patch("sys.argv", arguments), patch.object(survey, "EXPECTED", {"JOB": 1, "TPC-H": 1, "TPC-DS": 2}), \ + patch.object(survey.subprocess, "run", return_value=result), redirect_stdout(io.StringIO()): + self.assertEqual(survey.main(), 0) + report = json.loads((output / "results.json").read_text()) + self.assertTrue(report["survey_complete"]) + self.assertEqual(report["expected_statements"]["TPC-DS"], 2) + self.assertEqual(len(report["results"]), 8) + tpcds = [row for row in report["results"] if row["suite"] == "TPC-DS"] + self.assertEqual([row["statement"] for row in tpcds], [1, 2, 1, 2]) + + def test_split_respects_literals_and_nested_comments(self): + sql = "SELECT ';', $$x;y$$, $tag$a;b$tag$, \"a;b\"; /* outer /* inner ; */ end */ SELECT 2;" + self.assertEqual(len(survey.split_select_statements(sql)), 2) + self.assertEqual(len(survey.split_select_statements("SELECT E'a\\\';b'; SELECT 2;")), 2) + self.assertEqual(survey.split_select_statements("-- only comment\n /* another */"), []) + for sql in ["SELECT 'unterminated", "SELECT $$unterminated", "/* unterminated"]: + with self.assertRaises(ValueError): + survey.split_select_statements(sql) + + def test_q15_view_form_is_rejected_with_cte_instruction(self): + with self.assertRaisesRegex(ValueError, "CTE form of TPC-H Q15"): + survey.split_select_statements("CREATE VIEW revenue AS SELECT 1; SELECT * FROM revenue; DROP VIEW revenue;") + + def test_failed_queries_stay_in_denominator(self): + records = [dict(suite="JOB", optimizer="on", status="ok", strict=1, + eligible=1, anser_joins=1), + dict(suite="JOB", optimizer="on", status="error")] + text = survey.summarize(records, False) + self.assertIn("Survey complete: no", text) + self.assertIn("| JOB | on | 1/2 | 1/2 | 1/2 | 1/2 |", text) + + +if __name__ == "__main__": + unittest.main() diff --git a/licenses/LICENSE-mit-duckdb-rpt.txt b/licenses/LICENSE-mit-duckdb-rpt.txt new file mode 100644 index 00000000000..ddbb7cc497a --- /dev/null +++ b/licenses/LICENSE-mit-duckdb-rpt.txt @@ -0,0 +1,24 @@ +The RPT+ transfer-graph code in gpcontrib/robust_predicate_transfer is ported +from the dynamic-predicate-transfer artifact +(https://github.com/embryo-labs/dynamic-predicate-transfer), which carries the +following license: + +Copyright 2018-2025 Stichting DuckDB Foundation + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/pom.xml b/pom.xml index 5db6c10b684..b1970df21f9 100644 --- a/pom.xml +++ b/pom.xml @@ -1428,6 +1428,9 @@ code or new licensing patterns. gpcontrib/reject_partition_fullscan/Makefile gpcontrib/reject_partition_fullscan/reject_partition_fullscan.control + gpcontrib/anser/**/*.sql + gpcontrib/anser/**/*.out + diff --git a/src/Makefile.global.in b/src/Makefile.global.in index bb9d4474de0..d235317d36a 100644 --- a/src/Makefile.global.in +++ b/src/Makefile.global.in @@ -266,6 +266,8 @@ BITCODE_CXXFLAGS = @BITCODE_CXXFLAGS@ have_yaml = @have_yaml@ YAML_LIBS = @YAML_LIBS@ with_zstd = @with_zstd@ +with_anser = @with_anser@ +with_rpt = @with_rpt@ ZSTD_CFLAGS = @ZSTD_CFLAGS@ ZSTD_LIBS = @ZSTD_LIBS@ EVENT_LIBS = @EVENT_LIBS@ diff --git a/src/backend/cdb/cdbpath.c b/src/backend/cdb/cdbpath.c index cc703b6e1a7..95db110d668 100644 --- a/src/backend/cdb/cdbpath.c +++ b/src/backend/cdb/cdbpath.c @@ -2853,9 +2853,12 @@ create_motion_path_for_merge(PlannerInfo *root, List *resultRelations, GpPolicy /* * The statement that insert/update/delete on replicated table has to * be dispatched to each segment and executed on each segment. Thus - * the targetlist cannot contain volatile functions. + * the targetlist cannot contain volatile functions. MERGE action + * expressions (WHEN conditions, SET and VALUES) are kept in + * mergeActionLists, not in the subpath targetlist, so check them too. */ - if (contain_volatile_functions((Node *) (subpath->pathtarget->exprs))) + if (contain_volatile_functions((Node *) (subpath->pathtarget->exprs)) || + contain_volatile_functions((Node *) mergeActionLists)) elog(ERROR, "could not devise a plan."); } else diff --git a/src/backend/cdb/cdbutil.c b/src/backend/cdb/cdbutil.c index f286f2d1450..82d0e1d0c09 100644 --- a/src/backend/cdb/cdbutil.c +++ b/src/backend/cdb/cdbutil.c @@ -954,6 +954,20 @@ cleanupQE(SegmentDatabaseDescriptor *segdbDesc) if (cdbconn_isBadConnection(segdbDesc)) return false; + /* + * Unflushed output bytes at cleanup time can only be a partially + * delivered asynchronous payload (an Anser sideband message; command + * traffic is fully flushed before cleanup runs). The peer may already + * have read a prefix of it, and nobody will flush the rest now that the + * command is over (checkDispatchResult only flushes connections whose + * command is still running), so the connection is poisoned for reuse: + * the next dispatch would overwrite the remainder of the buffer + * (PQsendGpQuery_shared) and the peer would misread its stream. Decline + * to recycle it; a fresh connection is created on the next dispatch. + */ + if (segdbDesc->conn->outCount > 0) + return false; + /* if segment is down, the gang can not be reused */ if (FtsIsSegmentDown(segdbDesc->segment_database_info)) return false; @@ -3361,6 +3375,20 @@ cleanupQE(SegmentDatabaseDescriptor *segdbDesc) if (cdbconn_isBadConnection(segdbDesc)) return false; + /* + * Unflushed output bytes at cleanup time can only be a partially + * delivered asynchronous payload (an Anser sideband message; command + * traffic is fully flushed before cleanup runs). The peer may already + * have read a prefix of it, and nobody will flush the rest now that the + * command is over (checkDispatchResult only flushes connections whose + * command is still running), so the connection is poisoned for reuse: + * the next dispatch would overwrite the remainder of the buffer + * (PQsendGpQuery_shared) and the peer would misread its stream. Decline + * to recycle it; a fresh connection is created on the next dispatch. + */ + if (segdbDesc->conn->outCount > 0) + return false; + /* if segment is down, the gang can not be reused */ if (FtsIsSegmentDown(segdbDesc->segment_database_info)) return false; diff --git a/src/backend/cdb/dispatcher/cdbdisp_async.c b/src/backend/cdb/dispatcher/cdbdisp_async.c index eb8e4714396..c1440693901 100644 --- a/src/backend/cdb/dispatcher/cdbdisp_async.c +++ b/src/backend/cdb/dispatcher/cdbdisp_async.c @@ -43,6 +43,7 @@ #include "miscadmin.h" #include "commands/sequence.h" #include "access/xact.h" +#include "utils/faultinjector.h" #include "utils/timestamp.h" #define DISPATCH_WAIT_TIMEOUT_MSEC 2000 @@ -116,6 +117,12 @@ static void cdbdisp_waitDispatchFinish_async(struct CdbDispatcherState *ds); static bool cdbdisp_checkForCancel_async(struct CdbDispatcherState *ds); static int *cdbdisp_getWaitSocketFds_async(struct CdbDispatcherState *ds, int *nsocks); +/* + * Lets an extension handle its own NOTIFY channels arriving from QEs; see the + * declaration in cdbdisp.h for the contract. + */ +cdbdisp_notify_hook_type cdbdisp_notify_hook = NULL; + DispatcherInternalFuncs DispatcherAsyncFuncs = { cdbdisp_checkForCancel_async, @@ -136,6 +143,8 @@ static void checkDispatchResult(CdbDispatcherState *ds, int timeout_sec); static bool processResults(CdbDispatchResult *dispatchResult); +static void processNotifies(CdbDispatchResult *dispatchResult); + static void signalQEs(CdbDispatchCmdAsync *pParms); @@ -458,6 +467,7 @@ checkDispatchResult(CdbDispatcherState *ds, int timeout_sec) int timeout = 0; bool sentSignal = false; struct pollfd *fds; + bool *buffered; struct timeval start_ts, now; #ifdef USE_INTERNAL_FTS uint8 ftsVersion = 0; @@ -467,6 +477,12 @@ checkDispatchResult(CdbDispatcherState *ds, int timeout_sec) db_count = pParms->dispatchCount; fds = (struct pollfd *) palloc(db_count * sizeof(struct pollfd)); + /* + * Parallel to fds[]: whether that connection already holds a complete + * message in libpq's buffer rather than in its socket. See the loop below. + */ + buffered = (bool *) palloc0(db_count * sizeof(bool)); + /* * OK, we are finished submitting the command to the segdbs. Now, we have * to wait for them to finish. @@ -478,6 +494,7 @@ checkDispatchResult(CdbDispatcherState *ds, int timeout_sec) int n; int nfds = 0; int ack_count = 0; + bool anyBuffered = false; PGconn *conn; /* @@ -540,14 +557,52 @@ checkDispatchResult(CdbDispatcherState *ds, int timeout_sec) if (conn->outCount > 0) { /* + * Non-blocking, and the POLLOUT below is what finishes the + * job. A blocking flush here waits in pqWait() with no + * deadline and no CHECK_FOR_INTERRUPTS, so a peer that has + * stopped reading its end takes the whole dispatcher with it + * -- including this function's other role, which is to notice + * a cancel for the caller (cdbdisp_checkForCancel_async + * arrives here with timeout_sec = 0 precisely so as not to + * wait). cdbdisp_waitDispatchFinish_async already drains its + * output this way, so this is the same shape, not a new one. + * * Don't error out here, let following poll() routine to * handle it. */ - if (pqFlush(conn) < 0) + if (pqFlushNonBlocking(conn) < 0) elog(LOG, "Failed flushing outbound data to %s:%s", segdbDesc->whoami, PQerrorMessage(conn)); } + /* + * Take any notifications off this connection before deciding + * whether to wait on it. + * + * The buffered[] check below cannot see them. PQisBusy() parses + * whatever is already in the buffer, and libpq's parser handles a + * NOTIFY by queueing it on the connection and carrying on + * (pqParseInput3's 'A' arm calls getNotify(), then breaks); it + * never touches asyncStatus. So a notification can be complete + * and ready while PQisBusy() still answers true for the command + * that has not finished, leaving buffered[] false. + * + * That would only be survivable while the socket stayed readable, + * and it need not be: the flush just above can have absorbed those + * very bytes, because a write that cannot complete in one go reads + * the peer's pending output to avoid deadlocking against it. The + * notification is then in our own memory with nothing left in the + * socket, and poll() sleeps until the QE happens to send something + * else -- which, for a sender that does not wait for a reply, is + * not until its command completes, since tuples travel over the + * interconnect rather than over this connection. + * + * Draining here costs a pointer check when nothing is queued. + * PQnotifies() runs the parser itself, so this also does the + * parsing PQisBusy() would have done a few lines below. + */ + processNotifies(dispatchResult); + /* * Add socket to fd_set if still connected. */ @@ -555,6 +610,44 @@ checkDispatchResult(CdbDispatcherState *ds, int timeout_sec) Assert(sock >= 0); fds[nfds].fd = sock; fds[nfds].events = POLLIN; + + /* + * Anything the flush above could not place goes out when the peer + * makes room, which is what this asks to be woken for. Only the + * events mask grows: the array keeps one entry per connection in + * the same order, so handlePollSuccess() -- which walks it with + * its own counter and asserts the socket matches -- is unaffected, + * and it tests POLLIN alone. + */ + if (conn->outCount > 0) + fds[nfds].events |= POLLOUT; + + /* + * poll() reports what is in the socket, which is only the same + * thing as "what is left to read" while we are the only thing + * moving bytes off this connection. Anything else that writes to + * it through libpq breaks that: when a write cannot complete in one + * go, pqSendSome() calls pqReadData() to absorb whatever the peer + * has already sent (fe-misc.c), deliberately, so that a large write + * cannot deadlock against a peer that is writing back at us. The + * bytes then sit in conn->inBuffer, the socket goes quiet, and a QE + * that has finished and gone idle will never make it readable + * again -- so the results are in our own memory and we sleep + * forever waiting to be told about them. + * + * PQisBusy() runs libpq's parser over the buffer it already has and + * never touches the socket, so asking is free. A false answer means + * a complete message is ready now; take it in this round instead of + * polling, exactly as libpq's own async protocol prescribes (call + * poll() only once PQisBusy() says there is nothing to parse). + * + * This is the same class of lost wakeup as the outbound flush just + * above, and the same reason for handling it here. + */ + buffered[nfds] = !PQisBusy(conn); + if (buffered[nfds]) + anyBuffered = true; + nfds++; } @@ -582,6 +675,14 @@ checkDispatchResult(CdbDispatcherState *ds, int timeout_sec) else timeout = DISPATCH_WAIT_CANCEL_TIMEOUT_MSEC; + /* + * Something is already parseable, so there is nothing to wait for. + * Still poll, with no timeout, to pick up whatever else has arrived in + * the same round. + */ + if (anyBuffered) + timeout = 0; + n = poll(fds, nfds, timeout); /* @@ -620,6 +721,25 @@ checkDispatchResult(CdbDispatcherState *ds, int timeout_sec) if (timeout_sec >= 0 && diff_us >= timeout_sec * 1000000L) break; } + /* + * Buffered input counts as readable. poll() cleared revents and + * reported only the sockets, so put the buffered ones back before + * handing the array on -- handlePollSuccess() walks the same + * connections in the same order and reads revents to decide which to + * process. Checked ahead of the n == 0 arm: a round that found + * something to parse is not an expired wait, and treating it as one + * would signal the QEs and then loop back to the same state forever. + */ + else if (anyBuffered) + { + for (i = 0; i < nfds; i++) + { + if (buffered[i]) + fds[i].revents |= POLLIN; + } + + handlePollSuccess(pParms, fds); + } /* If the time limit expires, poll() returns 0 */ else if (n == 0) { @@ -657,6 +777,7 @@ checkDispatchResult(CdbDispatcherState *ds, int timeout_sec) handlePollSuccess(pParms, fds); } + pfree(buffered); pfree(fds); } @@ -968,6 +1089,32 @@ checkSegmentAlive(CdbDispatchCmdAsync *pParms) } } +/* + * Write a sideband message nobody asked for, to order the stream a particular + * way under the nextval_sideband_before_response fault. + * + * Only the framing matters, so the body is a marker rather than anything a + * subscriber could use: every reader of this connection has to get past a + * message that is not its own, and what is inside it never comes into that. + * A QE that happens to be waiting for a real filter logs that it could not + * read one and carries on unfiltered, which is the same fail-open path as a + * truncated delivery. + */ +static inline void +send_fake_sideband_message(PGconn *conn) +{ + static const char marker[] = "fault"; + + if (pqPutMsgStart(GP_SIDEBAND_MESSAGE, conn) < 0) + elog(ERROR, "Failed to send sideband message: %s", PQerrorMessage(conn)); + if (pqPutnchar(marker, sizeof(marker), conn) < 0) + elog(ERROR, "Failed to send sideband message: %s", PQerrorMessage(conn)); + if (pqPutMsgEnd(conn) < 0) + elog(ERROR, "Failed to send sideband message: %s", PQerrorMessage(conn)); + if (pqFlush(conn) < 0) + elog(ERROR, "Failed to send sideband message: %s", PQerrorMessage(conn)); +} + static inline void send_sequence_response(PGconn *conn, Oid oid, int64 last, int64 cached, int64 increment, bool overflow, bool error) { @@ -988,6 +1135,112 @@ send_sequence_response(PGconn *conn, Oid oid, int64 last, int64 cached, int64 in elog(ERROR, "Failed to send sequence response: %s", PQerrorMessage(conn)); } +/* + * Hand over any notifications this QE has sent us. + * + * Kept separate from processResults() because it must run on every path that + * consumed input, not only the one where the QE still has work to do. A + * notification that arrives in the same read as the QE's command completion is + * already parsed out of the socket and queued on the connection, but that + * connection is about to be dropped from the poll set (stillRunning goes + * false), so nothing would ever wake us for it again -- the notification would + * be silently lost. nextval() never hit this because its QE blocks for a + * reply, leaving the command incomplete; a fire-and-forget sender does hit it. + */ +static void +processNotifies(CdbDispatchResult *dispatchResult) +{ + SegmentDatabaseDescriptor *segdbDesc = dispatchResult->segdbDesc; + + PGnotify *qnotifies = PQnotifies(segdbDesc->conn); + while(qnotifies && elog_geterrcode() == 0) + { + if (strcmp(qnotifies->relname, CDB_NOTIFY_NEXTVAL) == 0) + { + /* + * If there was nextval request then respond back on this libpq + * connection with the next value. Check and process nextval + * message only if QD has not already hit the error. Since QD could + * have hit the error while processing the previous nextval_qd() + * request itself and since full error handling is not complete yet + * (ex: releasing all the locks, etc.), shouldn't attempt to call + * nextval_qd() again. + */ + + CHECK_FOR_INTERRUPTS(); + + int64 last = 0; + int64 cached = 0; + int64 increment = 0; + bool overflow = false; + Oid dbid; + Oid seq_oid; + + if (sscanf(qnotifies->extra, "%u:%u", &dbid, &seq_oid) != 2) + elog(ERROR, "invalid nextval message"); + + if (dbid != MyDatabaseId) + elog(ERROR, "nextval message database id:%u doesn't match my database id:%u", + dbid, MyDatabaseId); + + PG_TRY(); + { + nextval_qd(seq_oid, &last, &cached, &increment, &overflow); + } + PG_CATCH(); + { + send_sequence_response(segdbDesc->conn, seq_oid, last, cached, increment, overflow, true /* error */); + PG_RE_THROW(); + } + PG_END_TRY(); + + /* + * Put a sideband message in front of the reply, so the QE meets + * one while it is reading for something else. That ordering is a + * race in the field -- it needs a consumer to have given up on a + * filter the coordinator then delivers anyway -- and this makes it + * certain instead. The QE must skip the message and go on waiting + * for its reply; taking the type byte and erroring would leave the + * body in the stream and desynchronise the connection. + */ +#ifdef FAULT_INJECTOR + if (SIMPLE_FAULT_INJECTOR("nextval_sideband_before_response") == + FaultInjectorTypeSkip) + send_fake_sideband_message(segdbDesc->conn); +#endif + + /* respond back on this libpq connection with the next value */ + send_sequence_response(segdbDesc->conn, seq_oid, last, cached, increment, overflow, false /* error */); + } + else if (strcmp(qnotifies->relname, CDB_NOTIFY_ENDPOINT_ACK) == 0) + { + qnotifies->next = (struct pgNotify *) dispatchResult->ackPGNotifies; + dispatchResult->ackPGNotifies = qnotifies; + + /* Don't free the notify here since it in queue now */ + qnotifies = NULL; + } + else if (cdbdisp_notify_hook != NULL && + cdbdisp_notify_hook(dispatchResult, qnotifies)) + { + /* Consumed by an extension; nothing further to do here. */ + } + else + { + /* Got an unknown PGnotify, just record it in log */ + if (qnotifies->relname) + elog(LOG, "got an unknown notify message : %s", qnotifies->relname); + } + + if (qnotifies) + PQfreemem(qnotifies); + qnotifies = PQnotifies(segdbDesc->conn); + } + + forwardQENotices(); + +} + /* * Receive and process input from one QE. * @@ -1058,7 +1311,12 @@ processResults(CdbDispatchResult *dispatchResult) if (!pRes) { ELOG_DISPATCHER_DEBUG("%s -> idle", segdbDesc->whoami); - /* this is normal end of command */ + /* + * Normal end of command. Take any notifications with us: this + * connection is about to leave the poll set, so this is the last + * chance to see them. + */ + processNotifies(dispatchResult); return true; } @@ -1155,72 +1413,7 @@ processResults(CdbDispatchResult *dispatchResult) } forwardQENotices(); - - PGnotify *qnotifies = PQnotifies(segdbDesc->conn); - while(qnotifies && elog_geterrcode() == 0) - { - if (strcmp(qnotifies->relname, CDB_NOTIFY_NEXTVAL) == 0) - { - /* - * If there was nextval request then respond back on this libpq - * connection with the next value. Check and process nextval - * message only if QD has not already hit the error. Since QD could - * have hit the error while processing the previous nextval_qd() - * request itself and since full error handling is not complete yet - * (ex: releasing all the locks, etc.), shouldn't attempt to call - * nextval_qd() again. - */ - - CHECK_FOR_INTERRUPTS(); - - int64 last; - int64 cached; - int64 increment; - bool overflow; - Oid dbid; - Oid seq_oid; - - if (sscanf(qnotifies->extra, "%u:%u", &dbid, &seq_oid) != 2) - elog(ERROR, "invalid nextval message"); - - if (dbid != MyDatabaseId) - elog(ERROR, "nextval message database id:%u doesn't match my database id:%u", - dbid, MyDatabaseId); - - PG_TRY(); - { - nextval_qd(seq_oid, &last, &cached, &increment, &overflow); - } - PG_CATCH(); - { - send_sequence_response(segdbDesc->conn, seq_oid, last, cached, increment, overflow, true /* error */); - PG_RE_THROW(); - } - PG_END_TRY(); - /* respond back on this libpq connection with the next value */ - send_sequence_response(segdbDesc->conn, seq_oid, last, cached, increment, overflow, false /* error */); - } - else if (strcmp(qnotifies->relname, CDB_NOTIFY_ENDPOINT_ACK) == 0) - { - qnotifies->next = (struct pgNotify *) dispatchResult->ackPGNotifies; - dispatchResult->ackPGNotifies = qnotifies; - - /* Don't free the notify here since it in queue now */ - qnotifies = NULL; - } - else - { - /* Got an unknown PGnotify, just record it in log */ - if (qnotifies->relname) - elog(LOG, "got an unknown notify message : %s", qnotifies->relname); - } - - if (qnotifies) - PQfreemem(qnotifies); - qnotifies = PQnotifies(segdbDesc->conn); - } - - forwardQENotices(); + processNotifies(dispatchResult); return false; /* we must keep on monitoring this socket */ } diff --git a/src/backend/commands/explain_gp.c b/src/backend/commands/explain_gp.c index c3ff48d483b..25f482a9c27 100644 --- a/src/backend/commands/explain_gp.c +++ b/src/backend/commands/explain_gp.c @@ -816,6 +816,7 @@ cdbexplain_collectStatsFromNode(PlanState *planstate, CdbExplain_SendStatCtx *ct si->ntuples = instr->ntuples; si->ntuples2 = instr->ntuples2; si->nloops = instr->nloops; + si->bufusage = instr->bufusage; si->nfiltered1 = instr->nfiltered1; si->nfiltered2 = instr->nfiltered2; si->prf_work = instr->prf_work; @@ -951,7 +952,6 @@ cdbexplain_depStatAcc_saveText(CdbExplain_DepStatAcc *acc, } } /* cdbexplain_depStatAcc_saveText */ - /* * cdbexplain_depositStatsToNode * @@ -992,6 +992,7 @@ cdbexplain_depositStatsToNode(PlanState *planstate, CdbExplain_RecvStatCtx *ctx) CdbExplain_DepStatAcc vmem_reserved; CdbExplain_DepStatAcc totalPartTableScanned; CdbExplain_DepStatAcc sortSpaceUsed[NUM_SORT_SPACE_TYPE][NUM_SORT_METHOD]; + BufferUsage bufusage; int imsgptr; int nInst; @@ -1016,6 +1017,7 @@ cdbexplain_depositStatsToNode(PlanState *planstate, CdbExplain_RecvStatCtx *ctx) cdbexplain_depStatAcc_init0(&workmemwanted); cdbexplain_depStatAcc_init0(&totalWorkfileCreated); cdbexplain_depStatAcc_init0(&totalPartTableScanned); + memset(&bufusage, 0, sizeof(bufusage)); for (int i = 0; i < NUM_SORT_METHOD; i++) { for (int j = 0; j < NUM_SORT_SPACE_TYPE; j++) @@ -1061,6 +1063,7 @@ cdbexplain_depositStatsToNode(PlanState *planstate, CdbExplain_RecvStatCtx *ctx) cdbexplain_depStatAcc_upd(&workmemwanted, rsi->workmemwanted, rsh, rsi, nsi); cdbexplain_depStatAcc_upd(&totalWorkfileCreated, (rsi->workfileCreated ? 1 : 0), rsh, rsi, nsi); cdbexplain_depStatAcc_upd(&totalPartTableScanned, rsi->numPartScanned, rsh, rsi, nsi); + BufferUsageAdd(&bufusage, &rsi->bufusage); Assert(rsi->sortstats.sortMethod < NUM_SORT_METHOD); Assert(rsi->sortstats.spaceType < NUM_SORT_SPACE_TYPE); if (rsi->sortstats.sortMethod != SORT_TYPE_STILL_IN_PROGRESS) @@ -1132,6 +1135,9 @@ cdbexplain_depositStatsToNode(PlanState *planstate, CdbExplain_RecvStatCtx *ctx) instr->firststart = nodeAcc->nsimax->firststart; } + /* Buffer usage is summed across all workers, not taken from the winner. */ + instr->bufusage = bufusage; + /* Save extra message text for the most interesting winning qExecs. */ if (ctx->extratextbuf) { diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c index 29992eb02fd..bf49e3d758d 100644 --- a/src/backend/commands/sequence.c +++ b/src/backend/commands/sequence.c @@ -2149,22 +2149,51 @@ cdb_sequence_nextval_qe(Relation seqrel, /* * Read nextval response from QD. + * + * The loop is here because this is not the only thing the QD sends down + * this connection: a sideband message may arrive for a waiter that has + * already stopped waiting for it, and it can land in the middle of this + * exchange. Skipping one and carrying on is the only safe answer, since + * the type byte has been taken by then and there is no way to put it back + * for someone else to read. */ - do + for (;;) { - pq_startmsgread(); - retval = pq_getbyte_if_available(&qtype); - if (retval == 0) + StringInfoData sideband; + + do { - pq_endmsgread(); - CHECK_FOR_INTERRUPTS(); - } + pq_startmsgread(); + retval = pq_getbyte_if_available(&qtype); + if (retval == 0) + { + pq_endmsgread(); + CHECK_FOR_INTERRUPTS(); + } - if (retval == EOF) + if (retval == EOF) + ereport(ERROR, + (errcode(ERRCODE_INTERNAL_ERROR), + errmsg("nextval: connection is gone unexpectedly"))); + } while (retval != 1); + + if (qtype != GP_SIDEBAND_MESSAGE) + break; + + /* + * Not ours. Read the body out whole and drop it: whoever it was meant + * for is no longer reading, and leaving it in the stream would put + * every later read at the wrong offset -- which does not surface here + * but as an absurd allocation or an invalid message type somewhere + * else entirely, after the connection has gone back to the gang pool. + */ + initStringInfo(&sideband); + if (pq_getmessage(&sideband, PQ_LARGE_MESSAGE_LIMIT) != 0) ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("nextval: connection is gone unexpectedly"))); - } while (retval != 1); + pfree(sideband.data); + } if (qtype == 'X') ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("nextval: QD closed the connection"))); diff --git a/src/backend/executor/instrument.c b/src/backend/executor/instrument.c index 3fd39dfb167..354a029c440 100644 --- a/src/backend/executor/instrument.c +++ b/src/backend/executor/instrument.c @@ -32,7 +32,6 @@ static BufferUsage save_pgBufferUsage; WalUsage pgWalUsage; static WalUsage save_pgWalUsage; -static void BufferUsageAdd(BufferUsage *dst, const BufferUsage *add); static void WalUsageAdd(WalUsage *dst, WalUsage *add); /* GPDB specific */ @@ -87,6 +86,7 @@ InstrInit(Instrumentation *instr, int instrument_options) instr->need_bufusage = (instrument_options & INSTRUMENT_BUFFERS) != 0; instr->need_walusage = (instrument_options & INSTRUMENT_WAL) != 0; instr->need_timer = (instrument_options & INSTRUMENT_TIMER) != 0; + instr->need_cdb = (instrument_options & INSTRUMENT_CDB) != 0; } /* Entry to a plan node */ @@ -271,7 +271,7 @@ InstrAccumParallelQuery(BufferUsage *bufusage, WalUsage *walusage) } /* dst += add */ -static void +void BufferUsageAdd(BufferUsage *dst, const BufferUsage *add) { dst->shared_blks_hit += add->shared_blks_hit; @@ -506,8 +506,9 @@ pickInstrFromShmem(const Plan *plan, int instrument_options) MemoryContextSwitchTo(contextSave); } - if (NULL != instr && instrument_options & (INSTRUMENT_TIMER | INSTRUMENT_CDB)) + if (instr != NULL) { + instr->need_bufusage = (instrument_options & INSTRUMENT_BUFFERS) != 0; instr->need_timer = (instrument_options & INSTRUMENT_TIMER) != 0; instr->need_cdb = (instrument_options & INSTRUMENT_CDB) != 0; } diff --git a/src/backend/gpopt/config/CConfigParamMapping.cpp b/src/backend/gpopt/config/CConfigParamMapping.cpp index bdeb3cbb411..ab23ec820dc 100644 --- a/src/backend/gpopt/config/CConfigParamMapping.cpp +++ b/src/backend/gpopt/config/CConfigParamMapping.cpp @@ -346,6 +346,16 @@ CConfigParamMapping::SConfigMappingElem CConfigParamMapping::m_elements[] = { GPOS_WSZ_LIT( "Disable streaming hash agg in ORCA-generated local partial aggregations.")}, + {EopttraceChildOptMemo, &optimizer_enable_child_opt_memo, + false, // m_negate_param + GPOS_WSZ_LIT( + "Reuse the child contexts that the same request of a group expression reached under another context.")}, + + {EopttraceVerifyChildOptMemo, &optimizer_verify_child_opt_memo, + false, // m_negate_param + GPOS_WSZ_LIT( + "Optimize the children of requests that could reuse child contexts and report differences.")}, + }; //--------------------------------------------------------------------------- diff --git a/src/backend/gporca/libgpdbcost/src/CCostModelGPDB.cpp b/src/backend/gporca/libgpdbcost/src/CCostModelGPDB.cpp index f4dad6c59df..b42b75aa65c 100644 --- a/src/backend/gporca/libgpdbcost/src/CCostModelGPDB.cpp +++ b/src/backend/gporca/libgpdbcost/src/CCostModelGPDB.cpp @@ -1824,8 +1824,12 @@ CCostModelGPDB::GetCommonIndexData(T *ptr, ULONG &ulIndexKeys, const IMDIndex *pmdindex = md_accessor->RetrieveIndex(ptr->Pindexdesc()->MDId()); - pdrgpcrIndexColumns = CXformUtils::PdrgpcrIndexKeys( - mp, ptr->PdrgpcrOutput(), pmdindex, pmdrel); + // Costing has always weighed every column stored in the index (keys and + // INCLUDE columns alike) in ComputeUnusedIndexWeight; keep that behavior + // here so plan costs are unaffected by PdrgpcrIndexKeys becoming key-only. + pdrgpcrIndexColumns = CXformUtils::PdrgpcrIndexColumns( + mp, ptr->PdrgpcrOutput(), pmdindex, pmdrel, + CXformUtils::EicKeyAndIncluded); stats = ptr->PstatsBaseTable(); } diff --git a/src/backend/gporca/libgpopt/include/gpopt/base/CPartitionPropagationSpec.h b/src/backend/gporca/libgpopt/include/gpopt/base/CPartitionPropagationSpec.h index 92921bd389a..0ec52dd65c7 100644 --- a/src/backend/gporca/libgpopt/include/gpopt/base/CPartitionPropagationSpec.h +++ b/src/backend/gporca/libgpopt/include/gpopt/base/CPartitionPropagationSpec.h @@ -177,6 +177,13 @@ class CPartitionPropagationSpec : public CPropSpec BOOL ContainsAnyConsumers() const; + // does this spec carry no partition propagation requests? + BOOL + IsEmpty() const + { + return 0 == m_part_prop_spec_infos->Size(); + } + // equality check to determine compatibility of derived & required properties BOOL Equals(const CPartitionPropagationSpec *ppps) const; diff --git a/src/backend/gporca/libgpopt/include/gpopt/engine/CEngine.h b/src/backend/gporca/libgpopt/include/gpopt/engine/CEngine.h index 42564394740..bc6e009dcec 100644 --- a/src/backend/gporca/libgpopt/include/gpopt/engine/CEngine.h +++ b/src/backend/gporca/libgpopt/include/gpopt/engine/CEngine.h @@ -256,6 +256,10 @@ class CEngine : public DbgPrintMixin BOOL FCheckReqdProps(CExpressionHandle &exprhdl, CReqdPropPlan *prpp, ULONG ulOptReq); + // check whether a join's non-DPE propagation request is redundant + BOOL FRedundantPartPropagationRequest(CExpressionHandle &exprhdl, + CReqdPropPlan *prpp, ULONG ulOptReq); + // check enforceable properties; // return false if it's impossible for the operator to satisfy one or more BOOL FCheckEnfdProps(CMemoryPool *mp, CGroupExpression *pgexpr, @@ -373,6 +377,72 @@ class CEngine : public DbgPrintMixin CCostContext *pccChild, ULONG child_index, CCost *pcostLowerBound); + // events of the child optimization memo + enum EChildOptMemoEvent + { + EcomeLookup, // a request looked for child contexts to reuse + EcomeHit, // a request reused child contexts + EcomePrunedHit, // a request reusing child contexts was pruned + EcomeDistMismatch, // an entry failed the distribution check + EcomeShadowed, // a child context is shadowed by an equal one + EcomeInsert, // a request stored the child contexts it reached + EcomeVerifyMismatch, // verify: the children reached other contexts + EcomeVerifyPruneDiff, // verify: pruning after a child differed + EcomeVerifyIncomplete, // verify: child optimization failed otherwise + + EcomeSentinel + }; + +private: + // counters of child optimization memo events + ULONG m_rgulChildOptMemoEvents[EcomeSentinel]{}; + +public: + // count an event of the child optimization memo + void + CountChildOptMemoEvent(EChildOptMemoEvent ecome) + { + m_rgulChildOptMemoEvents[ecome]++; + } + + // check if a request of the given group expression can reuse and store + // child contexts + BOOL FChildOptMemoApplicable(CGroupExpression *pgexpr, + BOOL fOptimizeCTESequence) const; + + // find child contexts that the same request reached under another + // context and would reach under the given context as well + COptimizationContextArray *PdrgpocChildOptMemo(CMemoryPool *mp, + CGroupExpression *pgexpr, + COptimizationContext *poc, + ULONG ulOptReq); + + // check if a request reusing child contexts is pruned after one of them, + // as it would be when optimizing its children + BOOL FSafeToPruneWithChildOptMemo(CGroupExpression *pgexpr, + COptimizationContext *poc, + COptimizationContextArray *pdrgpoc, + CCost *pcostLowerBound); + + // store the child contexts reached by a request + void AddChildOptMemo(CMemoryPool *mp, CGroupExpression *pgexpr, + COptimizationContext *poc, ULONG ulOptReq, + COptimizationContextArray *pdrgpoc); + + // compare the child contexts reached by a request with the ones the + // memo offered, in verify mode + void VerifyChildOptMemo(CGroupExpression *pgexpr, COptimizationContext *poc, + ULONG ulOptReq, + COptimizationContextArray *pdrgpocMemo, + COptimizationContextArray *pdrgpoc, + BOOL fMemoPredictsPrune); + + // count and report a difference found in verify mode + void ReportChildOptMemoDifference(EChildOptMemoEvent ecome, + CGroupExpression *pgexpr, + COptimizationContext *poc, + ULONG ulOptReq); + // print IOstream &OsPrint(IOstream &) const; diff --git a/src/backend/gporca/libgpopt/include/gpopt/search/CGroup.h b/src/backend/gporca/libgpopt/include/gpopt/search/CGroup.h index eaeaf22024f..8bd9ede7e42 100644 --- a/src/backend/gporca/libgpopt/include/gpopt/search/CGroup.h +++ b/src/backend/gporca/libgpopt/include/gpopt/search/CGroup.h @@ -542,6 +542,10 @@ class CGroup : public CRefCount, public DbgPrintMixin // insert given context into contexts hash table COptimizationContext *PocInsert(COptimizationContext *poc); + // check if looking up the requirements of a context of this group finds + // that context, and not another context with equal requirements + BOOL FLookupFinds(COptimizationContext *poc); + // update the best group cost under the given optimization context void UpdateBestCost(COptimizationContext *poc, CCostContext *pcc); diff --git a/src/backend/gporca/libgpopt/include/gpopt/search/CGroupExpression.h b/src/backend/gporca/libgpopt/include/gpopt/search/CGroupExpression.h index 1edfd359774..cf01e25da2c 100644 --- a/src/backend/gporca/libgpopt/include/gpopt/search/CGroupExpression.h +++ b/src/backend/gporca/libgpopt/include/gpopt/search/CGroupExpression.h @@ -26,6 +26,53 @@ namespace gpopt { using namespace gpos; +//--------------------------------------------------------------------------- +// @class: +// CChildOptMemoEntry +// +// @doc: +// Child contexts that a request of a group expression reached, with the +// required plan properties of the context the request was made under +// +//--------------------------------------------------------------------------- +class CChildOptMemoEntry +{ +public: + // optimization request number + ULONG m_ulOptReq; + + // search stage of the context the request was made under + ULONG m_ulSearchStage; + + // required plan properties of that context + CReqdPropPlan *m_prpp; + + // child contexts the request reached, one per non-scalar child + COptimizationContextArray *m_pdrgpoc; + + CChildOptMemoEntry(ULONG ulOptReq, ULONG ulSearchStage, CReqdPropPlan *prpp, + COptimizationContextArray *pdrgpoc) + : m_ulOptReq(ulOptReq), + m_ulSearchStage(ulSearchStage), + m_prpp(prpp), + m_pdrgpoc(pdrgpoc) + { + } + + CChildOptMemoEntry(const CChildOptMemoEntry &) = delete; + + CChildOptMemoEntry &operator=(const CChildOptMemoEntry &) = delete; + + ~CChildOptMemoEntry() + { + m_prpp->Release(); + m_pdrgpoc->Release(); + } +}; + +using ChildOptMemoArray = + CDynamicPtrArray>; + //--------------------------------------------------------------------------- // @class: // CGroupExpression @@ -130,6 +177,9 @@ class CGroupExpression : public CRefCount, // hashtable of cost contexts ShtCC m_sht; + // child contexts reached by requests of this group expression + ChildOptMemoArray *m_pdrgpcome{nullptr}; + // set group back pointer void SetGroup(CGroup *pgroup); @@ -201,6 +251,18 @@ class CGroupExpression : public CRefCount, // cleanup cost contexts void CleanupContexts(); + // child contexts reached by requests of this group expression + ChildOptMemoArray * + PdrgpcomeChildOptMemo() const + { + return m_pdrgpcome; + } + + // store the child contexts reached by a request + void AddChildOptMemo(CMemoryPool *mp, ULONG ulOptReq, ULONG ulSearchStage, + CReqdPropPlan *prpp, + COptimizationContextArray *pdrgpoc); + // check if cost context already exists in group expression hash table BOOL FCostContextExists(COptimizationContext *poc, COptimizationContextArray *pdrgpoc); diff --git a/src/backend/gporca/libgpopt/include/gpopt/search/CJobGroupExpressionOptimization.h b/src/backend/gporca/libgpopt/include/gpopt/search/CJobGroupExpressionOptimization.h index 91dd018bc19..d35906d2c5c 100644 --- a/src/backend/gporca/libgpopt/include/gpopt/search/CJobGroupExpressionOptimization.h +++ b/src/backend/gporca/libgpopt/include/gpopt/search/CJobGroupExpressionOptimization.h @@ -117,6 +117,20 @@ class CJobGroupExpressionOptimization : public CJobGroupExpression // a handle object for required relational property computation CExpressionHandle *m_pexprhdlRel; + // flag to indicate if the child contexts were reused from the child + // optimization memo instead of optimizing the children + BOOL m_fChildOptMemoHit; + + // verify mode: child contexts the memo offered, compared with the ones + // reached by optimizing the children + COptimizationContextArray *m_pdrgpocMemo; + + // verify mode: flag to indicate if the memo predicted pruning after a child + BOOL m_fMemoPredictsPrune; + + // flag to indicate if child optimization was pruned after a child + BOOL m_fPrunedAfterChild; + // initialization routine for child groups optimization void InitChildGroupsOptimization(CSchedulerContext *psc); diff --git a/src/backend/gporca/libgpopt/include/gpopt/xforms/CXformUtils.h b/src/backend/gporca/libgpopt/include/gpopt/xforms/CXformUtils.h index 6494cfc992a..92f41a5530c 100644 --- a/src/backend/gporca/libgpopt/include/gpopt/xforms/CXformUtils.h +++ b/src/backend/gporca/libgpopt/include/gpopt/xforms/CXformUtils.h @@ -68,6 +68,19 @@ using CExpressionArrays = CDynamicPtrArray; //--------------------------------------------------------------------------- class CXformUtils { +public: + // which columns of an index to consider + enum EIndexCols + { + // KEY columns only: these are the only columns an index condition + // (index qual) may be built on + EicKey, + + // KEY columns followed by INCLUDE ("payload") columns: everything + // physically stored in the index + EicKeyAndIncluded + }; + private: // create a logical assert for the not nullable columns of the given table // on top of the given child expression @@ -83,11 +96,12 @@ class CXformUtils CColRefArray *colref_array); // return the set of columns from the given array of columns which appear - // in the index included / key columns + // in the index columns of the specified type (key only, or key + included) static CColRefSet *PcrsIndexColumns(CMemoryPool *mp, CColRefArray *colref_array, const IMDIndex *pmdindex, - const IMDRelation *pmdrel); + const IMDRelation *pmdrel, + EIndexCols eic); // return the set of columns from the given array of columns which are // returnable through the index (to determine index-only scan capable) @@ -96,13 +110,6 @@ class CXformUtils const IMDIndex *pmdindex, const IMDRelation *pmdrel); - // return the ordered array of columns from the given array of columns which appear - // in the index included / key columns - static CColRefArray *PdrgpcrIndexColumns(CMemoryPool *mp, - CColRefArray *colref_array, - const IMDIndex *pmdindex, - const IMDRelation *pmdrel); - // lookup join keys in scalar child group static void LookupJoinKeys(CMemoryPool *mp, CExpression *pexpr, CExpressionArray **ppdrgpexprOuter, @@ -369,8 +376,18 @@ class CXformUtils static CWStringConst *PstrErrorMessage(CMemoryPool *mp, ULONG major, ULONG minor, ...); + // return the ordered array of columns from the given array of columns which + // appear in the index columns of the specified type (key only, or + // key + included) + static CColRefArray *PdrgpcrIndexColumns(CMemoryPool *mp, + CColRefArray *colref_array, + const IMDIndex *pmdindex, + const IMDRelation *pmdrel, + EIndexCols eic); + // return the array of key columns from the given array of columns which appear - // in the index key columns + // in the index key columns (INCLUDE columns are deliberately excluded: + // a predicate on them must never become an index condition) static CColRefArray *PdrgpcrIndexKeys(CMemoryPool *mp, CColRefArray *colref_array, const IMDIndex *pmdindex, diff --git a/src/backend/gporca/libgpopt/src/engine/CEngine.cpp b/src/backend/gporca/libgpopt/src/engine/CEngine.cpp index e68bc85f248..9fd7bdbfa99 100644 --- a/src/backend/gporca/libgpopt/src/engine/CEngine.cpp +++ b/src/backend/gporca/libgpopt/src/engine/CEngine.cpp @@ -20,11 +20,13 @@ #include "gpos/task/CAutoTaskProxy.h" #include "gpos/task/CAutoTraceFlag.h" +#include "gpopt/base/CCTEReq.h" #include "gpopt/base/CCostContext.h" #include "gpopt/base/CDrvdPropCtxtPlan.h" #include "gpopt/base/CEnfdPartitionPropagation.h" #include "gpopt/base/COptCtxt.h" #include "gpopt/base/COptimizationContext.h" +#include "gpopt/base/CPartInfo.h" #include "gpopt/base/CQueryContext.h" #include "gpopt/base/CReqdPropPlan.h" #include "gpopt/base/CReqdPropRelational.h" @@ -1378,6 +1380,451 @@ CEngine::DbgPrintExpr(int group_no, int context_no) #endif // GPOS_DEBUG +//--------------------------------------------------------------------------- +// @function: +// FEqualExceptDistribution +// +// @doc: +// Check if two required plan properties are equal apart from the +// required distribution +// +//--------------------------------------------------------------------------- +static BOOL +FEqualExceptDistribution(CReqdPropPlan *prppFst, CReqdPropPlan *prppSnd) +{ + if (!prppFst->PcrsRequired()->Equals(prppSnd->PcrsRequired()) || + !prppFst->Pcter()->Equals(prppSnd->Pcter()) || + !prppFst->Peo()->Matches(prppSnd->Peo()) || + !prppFst->Per()->Matches(prppSnd->Per())) + { + return false; + } + + if (nullptr == prppFst->Pepp() || nullptr == prppSnd->Pepp()) + { + return nullptr == prppFst->Pepp() && nullptr == prppSnd->Pepp(); + } + + return prppFst->Pepp()->Matches(prppSnd->Pepp()); +} + + +//--------------------------------------------------------------------------- +// @function: +// UlNonScalarChildPos +// +// @doc: +// Position of a child in an array of child contexts, which holds one +// context per non-scalar child +// +//--------------------------------------------------------------------------- +static ULONG +UlNonScalarChildPos(CGroupExpression *pgexpr, ULONG child_index) +{ + ULONG ulPos = 0; + for (ULONG ul = 0; ul < child_index; ul++) + { + if (!(*pgexpr)[ul]->FScalar()) + { + ulPos++; + } + } + + return ulPos; +} + + +//--------------------------------------------------------------------------- +// @function: +// FSameChildContexts +// +// @doc: +// Check if two arrays hold the same child contexts +// +//--------------------------------------------------------------------------- +static BOOL +FSameChildContexts(COptimizationContextArray *pdrgpocFst, + COptimizationContextArray *pdrgpocSnd) +{ + const ULONG size = pdrgpocFst->Size(); + if (size != pdrgpocSnd->Size()) + { + return false; + } + + for (ULONG ul = 0; ul < size; ul++) + { + if ((*pdrgpocFst)[ul] != (*pdrgpocSnd)[ul]) + { + return false; + } + } + + return true; +} + + +//--------------------------------------------------------------------------- +// @function: +// FChildDistributionsMatch +// +// @doc: +// Check if a request made under the given context requires from each +// child the distribution that the child's context in pdrgpoc was +// created for. +// +// The caller has checked that the context's other requirements equal +// the ones the child contexts were reached under. The other child +// requirements only depend on those, on the request, and on the plans +// of the children optimized before, which are the best plans of the +// same child contexts. So the distribution is the only child +// requirement that can differ. +// +// The plan properties of earlier children are derived again, as +// optimizing the children does in DerivePrevChildProps(), rather than +// taken from the child's cost context: some PdsRequired() +// implementations change the derived distribution of the first child +// in place, and the properties cached on the cost context are shared. +// +//--------------------------------------------------------------------------- +static BOOL +FChildDistributionsMatch(CMemoryPool *mp, CGroupExpression *pgexpr, + COptimizationContext *poc, ULONG ulOptReq, + COptimizationContextArray *pdrgpoc, + BOOL *pfShadowed) +{ + *pfShadowed = false; + + CPhysical *popPhysical = CPhysical::PopConvert(pgexpr->Pop()); + ULONG ulOrderReq = 0; + ULONG ulDistrReq = 0; + ULONG ulRewindReq = 0; + ULONG ulPartPropagateReq = 0; + popPhysical->LookupRequest(ulOptReq, &ulOrderReq, &ulDistrReq, + &ulRewindReq, &ulPartPropagateReq); + + CExpressionHandle exprhdl(mp); + exprhdl.Attach(pgexpr); + exprhdl.DeriveProps(nullptr /*pdpctxt*/); + exprhdl.InitReqdProps(poc->Prpp()); + + CDrvdPropArray *pdrgpdp = GPOS_NEW(mp) CDrvdPropArray(mp); + BOOL fMatch = true; + ULONG ulPrevChildIndex = gpos::ulong_max; + ULONG child_index = exprhdl.UlFirstOptimizedChildIndex(); + do + { + CGroup *pgroupChild = (*pgexpr)[child_index]; + if (pgroupChild->FScalar()) + { + ulPrevChildIndex = child_index; + continue; + } + + COptimizationContext *pocChild = + (*pdrgpoc)[UlNonScalarChildPos(pgexpr, child_index)]; + CCostContext *pccChild = pocChild->PccBest(); + if (nullptr == pccChild || + (pgroupChild == pgexpr->Pgroup() && pocChild->Matches(poc))) + { + // the child has no plan, or it would be the context itself + fMatch = false; + break; + } + + // as DerivePrevChildProps(): the plan of the previous child, if that + // one is not scalar + if (gpos::ulong_max != ulPrevChildIndex && + !(*pgexpr)[ulPrevChildIndex]->FScalar()) + { + CCostContext *pccPrev = + (*pdrgpoc)[UlNonScalarChildPos(pgexpr, ulPrevChildIndex)] + ->PccBest(); + CExpressionHandle exprhdlPrev(mp); + exprhdlPrev.Attach(pccPrev); + exprhdlPrev.DerivePlanPropsForCostContext(); + exprhdlPrev.Pdp()->AddRef(); + pdrgpdp->Append(exprhdlPrev.Pdp()); + } + + CEnfdDistribution *ped = popPhysical->Ped( + mp, exprhdl, poc->Prpp(), child_index, pdrgpdp, ulDistrReq); + fMatch = ped->Matches(pocChild->Prpp()->Ped()); + ped->Release(); + if (!fMatch) + { + break; + } + + // the request reaches the child context that a lookup of its + // requirements finds, which is another one if the group holds + // several contexts with equal requirements + if (!pgroupChild->FLookupFinds(pocChild)) + { + *pfShadowed = true; + fMatch = false; + break; + } + + // later children see the requirements of this child as they would + // after optimizing it + pocChild->Prpp()->AddRef(); + exprhdl.CopyChildReqdProps(child_index, pocChild->Prpp()); + ulPrevChildIndex = child_index; + } while (exprhdl.FNextChildIndex(&child_index)); + + pdrgpdp->Release(); + + return fMatch; +} + + +//--------------------------------------------------------------------------- +// @function: +// CEngine::FChildOptMemoApplicable +// +// @doc: +// Check if a request of the given group expression can reuse and store +// child contexts +// +//--------------------------------------------------------------------------- +BOOL +CEngine::FChildOptMemoApplicable(CGroupExpression *pgexpr, + BOOL fOptimizeCTESequence) const +{ + if (!GPOS_FTRACE(EopttraceChildOptMemo) && + !GPOS_FTRACE(EopttraceVerifyChildOptMemo)) + { + return false; + } + + // a CTE sequence may optimize its producer under requirements derived + // from the consumers, and with several search stages the children of a + // request use the best context of any stage + if (fOptimizeCTESequence || 1 < UlSearchStages()) + { + return false; + } + + const ULONG arity = pgexpr->Arity(); + for (ULONG ul = 0; ul < arity; ul++) + { + if (!(*pgexpr)[ul]->FScalar()) + { + return true; + } + } + + return false; +} + + +//--------------------------------------------------------------------------- +// @function: +// CEngine::PdrgpocChildOptMemo +// +// @doc: +// Find child contexts that the same request of the group expression +// reached under another context, and that it would reach under the +// given context as well. The other context must have the same required +// plan properties apart from distribution; the required distribution +// of each child is then computed again and compared. +// +//--------------------------------------------------------------------------- +COptimizationContextArray * +CEngine::PdrgpocChildOptMemo(CMemoryPool *mp, CGroupExpression *pgexpr, + COptimizationContext *poc, ULONG ulOptReq) +{ + CountChildOptMemoEvent(EcomeLookup); + + ChildOptMemoArray *pdrgpcome = pgexpr->PdrgpcomeChildOptMemo(); + if (nullptr == pdrgpcome) + { + return nullptr; + } + + CReqdPropPlan *prpp = poc->Prpp(); + const ULONG size = pdrgpcome->Size(); + for (ULONG ul = 0; ul < size; ul++) + { + CChildOptMemoEntry *pcome = (*pdrgpcome)[ul]; + if (pcome->m_ulOptReq != ulOptReq || + pcome->m_ulSearchStage != poc->UlSearchStageIndex() || + !FEqualExceptDistribution(pcome->m_prpp, prpp)) + { + continue; + } + + BOOL fShadowed = false; + if (FChildDistributionsMatch(mp, pgexpr, poc, ulOptReq, + pcome->m_pdrgpoc, &fShadowed)) + { + pcome->m_pdrgpoc->AddRef(); + return pcome->m_pdrgpoc; + } + + CountChildOptMemoEvent(fShadowed ? EcomeShadowed : EcomeDistMismatch); + } + + return nullptr; +} + + +//--------------------------------------------------------------------------- +// @function: +// CEngine::FSafeToPruneWithChildOptMemo +// +// @doc: +// Check if a request reusing child contexts is pruned after one of its +// children. Optimizing the children checks cost bounds after each +// non-scalar child that is followed by another non-scalar child in +// optimization order; make the same checks with the reused contexts. +// +//--------------------------------------------------------------------------- +BOOL +CEngine::FSafeToPruneWithChildOptMemo(CGroupExpression *pgexpr, + COptimizationContext *poc, + COptimizationContextArray *pdrgpoc, + CCost *pcostLowerBound) +{ + CExpressionHandle exprhdl(m_mp); + exprhdl.Attach(pgexpr); + + ULONG ulPrevChildIndex = gpos::ulong_max; + ULONG child_index = exprhdl.UlFirstOptimizedChildIndex(); + do + { + if (gpos::ulong_max != ulPrevChildIndex && + !(*pgexpr)[ulPrevChildIndex]->FScalar() && + !(*pgexpr)[child_index]->FScalar()) + { + CCostContext *pccPrev = + (*pdrgpoc)[UlNonScalarChildPos(pgexpr, ulPrevChildIndex)] + ->PccBest(); + if (FSafeToPrune(pgexpr, poc->Prpp(), pccPrev, ulPrevChildIndex, + pcostLowerBound)) + { + return true; + } + } + ulPrevChildIndex = child_index; + } while (exprhdl.FNextChildIndex(&child_index)); + + return false; +} + + +//--------------------------------------------------------------------------- +// @function: +// CEngine::AddChildOptMemo +// +// @doc: +// Store the child contexts reached by a request, unless a child has no +// plan or the same entry exists +// +//--------------------------------------------------------------------------- +void +CEngine::AddChildOptMemo(CMemoryPool *mp, CGroupExpression *pgexpr, + COptimizationContext *poc, ULONG ulOptReq, + COptimizationContextArray *pdrgpoc) +{ + const ULONG ulChildren = pdrgpoc->Size(); + for (ULONG ul = 0; ul < ulChildren; ul++) + { + if (nullptr == (*pdrgpoc)[ul]->PccBest()) + { + return; + } + } + + ChildOptMemoArray *pdrgpcome = pgexpr->PdrgpcomeChildOptMemo(); + if (nullptr != pdrgpcome) + { + const ULONG size = pdrgpcome->Size(); + for (ULONG ul = 0; ul < size; ul++) + { + CChildOptMemoEntry *pcome = (*pdrgpcome)[ul]; + if (pcome->m_ulOptReq == ulOptReq && + pcome->m_ulSearchStage == poc->UlSearchStageIndex() && + FSameChildContexts(pcome->m_pdrgpoc, pdrgpoc) && + FEqualExceptDistribution(pcome->m_prpp, poc->Prpp())) + { + return; + } + } + } + + pgexpr->AddChildOptMemo(mp, ulOptReq, poc->UlSearchStageIndex(), + poc->Prpp(), pdrgpoc); + CountChildOptMemoEvent(EcomeInsert); +} + + +//--------------------------------------------------------------------------- +// @function: +// CEngine::VerifyChildOptMemo +// +// @doc: +// Compare the child contexts reached by optimizing the children of a +// request with the ones the memo offered, and report a difference +// +//--------------------------------------------------------------------------- +void +CEngine::VerifyChildOptMemo(CGroupExpression *pgexpr, + COptimizationContext *poc, ULONG ulOptReq, + COptimizationContextArray *pdrgpocMemo, + COptimizationContextArray *pdrgpoc, + BOOL fMemoPredictsPrune) +{ + if (!FSameChildContexts(pdrgpocMemo, pdrgpoc)) + { + ReportChildOptMemoDifference(EcomeVerifyMismatch, pgexpr, poc, + ulOptReq); + } + else if (fMemoPredictsPrune) + { + ReportChildOptMemoDifference(EcomeVerifyPruneDiff, pgexpr, poc, + ulOptReq); + } +} + + +//--------------------------------------------------------------------------- +// @function: +// CEngine::ReportChildOptMemoDifference +// +// @doc: +// Count and report a difference between reusing child contexts and +// optimizing the children, found in verify mode +// +//--------------------------------------------------------------------------- +void +CEngine::ReportChildOptMemoDifference(EChildOptMemoEvent ecome, + CGroupExpression *pgexpr, + COptimizationContext *poc, + ULONG ulOptReq) +{ + CountChildOptMemoEvent(ecome); + + const CHAR *szWhat = "other child contexts"; + if (EcomeVerifyPruneDiff == ecome) + { + szWhat = "other pruning"; + } + else if (EcomeVerifyIncomplete == ecome) + { + szWhat = "child optimization failed"; + } + + CAutoTrace at(m_mp); + at.Os() << "[OPT]: child optimization memo difference (" << szWhat + << "): " << pgexpr->Pop()->SzId() << " (group " + << pgexpr->Pgroup()->Id() << ", expression " << pgexpr->Id() + << ", request " << ulOptReq << ") under "; + poc->Prpp()->OsPrint(at.Os()); +} + + //--------------------------------------------------------------------------- // @function: // CEngine::PdrgpocChildren @@ -1570,6 +2017,21 @@ CEngine::PrintActivatedXforms(IOstream &os) const } os << "[OPT]: " << std::endl; + + os << "[OPT]: child optimization memo: " + << m_rgulChildOptMemoEvents[EcomeLookup] << " lookups, " + << m_rgulChildOptMemoEvents[EcomeHit] << " hits, " + << m_rgulChildOptMemoEvents[EcomePrunedHit] << " pruned hits, " + << m_rgulChildOptMemoEvents[EcomeDistMismatch] + << " distribution mismatches, " + << m_rgulChildOptMemoEvents[EcomeShadowed] << " shadowed, " + << m_rgulChildOptMemoEvents[EcomeInsert] << " entries, " + << m_rgulChildOptMemoEvents[EcomeVerifyMismatch] + << " verify mismatches, " + << m_rgulChildOptMemoEvents[EcomeVerifyPruneDiff] + << " verify pruning differences, " + << m_rgulChildOptMemoEvents[EcomeVerifyIncomplete] + << " verify incomplete" << std::endl; } } @@ -2248,6 +2710,31 @@ CEngine::FProhibited(CEnfdProp::EPropEnforcingType epetOrder, } +BOOL +CEngine::FRedundantPartPropagationRequest(CExpressionHandle &exprhdl, + CReqdPropPlan *prpp, ULONG ulOptReq) +{ + CPhysical *popPhysical = CPhysical::PopConvert(exprhdl.Pop()); + + // Hash and nested-loop joins can enumerate DPE and non-DPE requests. + // Without partition consumers or incoming propagation requirements, + // both produce empty specs for every child. The other property request + // numbers are unchanged, so keep only the DPE request for each tuple. + if (2 != popPhysical->UlPartPropagateRequests() || + !CUtils::FPhysicalJoin(popPhysical) || + !prpp->Pepp()->PppsRequired()->IsEmpty()) + { + return false; + } + + ULONG order_req, distribution_req, rewind_req, partition_req; + popPhysical->LookupRequest(ulOptReq, &order_req, &distribution_req, + &rewind_req, &partition_req); + return 1 == partition_req && + 0 == exprhdl.DerivePartitionInfo()->UlConsumers(); +} + + //--------------------------------------------------------------------------- // @function: // CEngine::FCheckReqdProps @@ -2289,6 +2776,10 @@ CEngine::FCheckReqdProps(CExpressionHandle &exprhdl, CReqdPropPlan *prpp, CPhysical *popPhysical = CPhysical::PopConvert(exprhdl.Pop()); COperator::EOperatorId op_id = popPhysical->Eopid(); + if (FRedundantPartPropagationRequest(exprhdl, prpp, ulOptReq)) + { + return false; + } // check if sort operator is passed an empty order spec; // this check is required to avoid self-deadlocks, i.e. diff --git a/src/backend/gporca/libgpopt/src/search/CGroup.cpp b/src/backend/gporca/libgpopt/src/search/CGroup.cpp index 02fd7fa9ccc..dfc8d85ba86 100644 --- a/src/backend/gporca/libgpopt/src/search/CGroup.cpp +++ b/src/backend/gporca/libgpopt/src/search/CGroup.cpp @@ -439,6 +439,26 @@ CGroup::PocInsert(COptimizationContext *poc) } +//--------------------------------------------------------------------------- +// @function: +// CGroup::FLookupFinds +// +// @doc: +// Check if looking up the requirements of a context of this group finds +// that context. If requirements change after their context was +// inserted, a group can hold several contexts with equal requirements, +// of which a lookup finds only one. +// +//--------------------------------------------------------------------------- +BOOL +CGroup::FLookupFinds(COptimizationContext *poc) +{ + ShtAcc shta(Sht(), *poc); + + return poc == shta.Find(); +} + + //--------------------------------------------------------------------------- // @function: // CGroup::PgexprBest diff --git a/src/backend/gporca/libgpopt/src/search/CGroupExpression.cpp b/src/backend/gporca/libgpopt/src/search/CGroupExpression.cpp index dffd10cb35f..c6fc40e3b5c 100644 --- a/src/backend/gporca/libgpopt/src/search/CGroupExpression.cpp +++ b/src/backend/gporca/libgpopt/src/search/CGroupExpression.cpp @@ -143,6 +143,35 @@ CGroupExpression::CleanupContexts() } } } + + CRefCount::SafeRelease(m_pdrgpcome); + m_pdrgpcome = nullptr; +} + + +//--------------------------------------------------------------------------- +// @function: +// CGroupExpression::AddChildOptMemo +// +// @doc: +// Store the child contexts reached by a request, together with the +// required plan properties of the context the request was made under +// +//--------------------------------------------------------------------------- +void +CGroupExpression::AddChildOptMemo(CMemoryPool *mp, ULONG ulOptReq, + ULONG ulSearchStage, CReqdPropPlan *prpp, + COptimizationContextArray *pdrgpoc) +{ + if (nullptr == m_pdrgpcome) + { + m_pdrgpcome = GPOS_NEW(mp) ChildOptMemoArray(mp); + } + + prpp->AddRef(); + pdrgpoc->AddRef(); + m_pdrgpcome->Append(GPOS_NEW(mp) CChildOptMemoEntry( + ulOptReq, ulSearchStage, prpp, pdrgpoc)); } diff --git a/src/backend/gporca/libgpopt/src/search/CJobGroupExpressionOptimization.cpp b/src/backend/gporca/libgpopt/src/search/CJobGroupExpressionOptimization.cpp index 404a772c1d3..534025337b0 100644 --- a/src/backend/gporca/libgpopt/src/search/CJobGroupExpressionOptimization.cpp +++ b/src/backend/gporca/libgpopt/src/search/CJobGroupExpressionOptimization.cpp @@ -211,6 +211,10 @@ CJobGroupExpressionOptimization::Init(CGroupExpression *pgexpr, m_pdrgpdp = nullptr; m_pexprhdlPlan = nullptr; m_pexprhdlRel = nullptr; + m_fChildOptMemoHit = false; + m_pdrgpocMemo = nullptr; + m_fMemoPredictsPrune = false; + m_fPrunedAfterChild = false; m_eceo = CPhysical::PopConvert(pgexpr->Pop())->Eceo(); m_ulArity = pgexpr->Arity(); m_ulChildIndex = gpos::ulong_max; @@ -246,6 +250,8 @@ CJobGroupExpressionOptimization::Cleanup() CRefCount::SafeRelease(m_pdrgpstatCurrentCtxt); CRefCount::SafeRelease(m_pdrgpdp); CRefCount::SafeRelease(m_prppCTEProducer); + CRefCount::SafeRelease(m_pdrgpocMemo); + m_pdrgpocMemo = nullptr; GPOS_DELETE(m_pexprhdlPlan); GPOS_DELETE(m_pexprhdlRel); } @@ -337,6 +343,48 @@ CJobGroupExpressionOptimization::EevtInitialize(CSchedulerContext *psc, return eevFinalized; } + // reuse the child contexts that the same request reached under another + // context, if the request would reach them under this context as well + CEngine *peng = psc->Peng(); + if (peng->FChildOptMemoApplicable(pjgeo->m_pgexpr, + pjgeo->m_fOptimizeCTESequence)) + { + COptimizationContextArray *pdrgpoc = peng->PdrgpocChildOptMemo( + psc->GetGlobalMemoryPool(), pjgeo->m_pgexpr, pjgeo->m_poc, + pjgeo->m_ulOptReq); + if (nullptr != pdrgpoc) + { + CCost costLowerBoundMemo(GPOPT_INVALID_COST); + BOOL fPruned = peng->FSafeToPruneWithChildOptMemo( + pjgeo->m_pgexpr, pjgeo->m_poc, pdrgpoc, &costLowerBoundMemo); + peng->CountChildOptMemoEvent(fPruned ? CEngine::EcomePrunedHit + : CEngine::EcomeHit); + if (GPOS_FTRACE(EopttraceVerifyChildOptMemo)) + { + // optimize the children anyway, and compare the contexts + // they reach with the reused ones + pjgeo->m_pdrgpocMemo = pdrgpoc; + pjgeo->m_fMemoPredictsPrune = fPruned; + } + else if (fPruned) + { + (void) pjgeo->m_pgexpr->PccComputeCost( + psc->GetGlobalMemoryPool(), pjgeo->m_poc, + pjgeo->m_ulOptReq, nullptr /*pdrgpoc*/, true /*fPruned*/, + costLowerBoundMemo); + pdrgpoc->Release(); + return eevFinalized; + } + else + { + pjgeo->m_pdrgpoc = pdrgpoc; + pjgeo->m_fChildOptMemoHit = true; + pjgeo->SetChildrenScheduled(); + return eevOptimizingChildren; + } + } + } + pjgeo->InitChildGroupsOptimization(psc); return eevOptimizingChildren; @@ -389,6 +437,7 @@ CJobGroupExpressionOptimization::DerivePrevChildProps(CSchedulerContext *psc) m_ulOptReq, nullptr /*pdrgpoc*/, true /*fPruned*/, costLowerBound); m_fChildOptimizationFailed = true; + m_fPrunedAfterChild = true; return; } @@ -544,6 +593,24 @@ CJobGroupExpressionOptimization::EevtOptimizeChildren(CSchedulerContext *psc, pjgeo->ScheduleChildGroupsJobs(psc); if (pjgeo->m_fChildOptimizationFailed) { + if (nullptr != pjgeo->m_pdrgpocMemo) + { + // verify mode: the reused child contexts would have let the + // request continue, or be pruned at another point + if (!pjgeo->m_fPrunedAfterChild) + { + psc->Peng()->ReportChildOptMemoDifference( + CEngine::EcomeVerifyIncomplete, pjgeo->m_pgexpr, + pjgeo->m_poc, pjgeo->m_ulOptReq); + } + else if (!pjgeo->m_fMemoPredictsPrune) + { + psc->Peng()->ReportChildOptMemoDifference( + CEngine::EcomeVerifyPruneDiff, pjgeo->m_pgexpr, + pjgeo->m_poc, pjgeo->m_ulOptReq); + } + } + // failed to optimize child, terminate job pjgeo->Cleanup(); return eevFinalized; @@ -571,10 +638,29 @@ CJobGroupExpressionOptimization::EevtAddEnforcers(CSchedulerContext *psc, // get a job pointer CJobGroupExpressionOptimization *pjgeo = PjConvert(pjOwner); - // build child contexts array - GPOS_ASSERT(nullptr == pjgeo->m_pdrgpoc); - pjgeo->m_pdrgpoc = psc->Peng()->PdrgpocChildren(psc->GetGlobalMemoryPool(), - *pjgeo->m_pexprhdlPlan); + // build child contexts array, unless reused from the child + // optimization memo + if (!pjgeo->m_fChildOptMemoHit) + { + GPOS_ASSERT(nullptr == pjgeo->m_pdrgpoc); + CEngine *peng = psc->Peng(); + pjgeo->m_pdrgpoc = peng->PdrgpocChildren(psc->GetGlobalMemoryPool(), + *pjgeo->m_pexprhdlPlan); + if (nullptr != pjgeo->m_pdrgpocMemo) + { + peng->VerifyChildOptMemo(pjgeo->m_pgexpr, pjgeo->m_poc, + pjgeo->m_ulOptReq, pjgeo->m_pdrgpocMemo, + pjgeo->m_pdrgpoc, + pjgeo->m_fMemoPredictsPrune); + } + if (peng->FChildOptMemoApplicable(pjgeo->m_pgexpr, + pjgeo->m_fOptimizeCTESequence)) + { + peng->AddChildOptMemo(psc->GetGlobalMemoryPool(), pjgeo->m_pgexpr, + pjgeo->m_poc, pjgeo->m_ulOptReq, + pjgeo->m_pdrgpoc); + } + } // enforce physical properties BOOL fCheckEnfdProps = psc->Peng()->FCheckEnfdProps( diff --git a/src/backend/gporca/libgpopt/src/xforms/CXformUtils.cpp b/src/backend/gporca/libgpopt/src/xforms/CXformUtils.cpp index b810b058f4d..83d8662bc51 100644 --- a/src/backend/gporca/libgpopt/src/xforms/CXformUtils.cpp +++ b/src/backend/gporca/libgpopt/src/xforms/CXformUtils.cpp @@ -1784,7 +1784,9 @@ CXformUtils::PstrErrorMessage(CMemoryPool *mp, ULONG major, ULONG minor, ...) // // @doc: // Return the array of columns from the given array of columns which appear -// in the index key columns +// in the index key columns. INCLUDE columns are not keys: the executor +// only accepts index quals on key attributes (see ExecIndexBuildScanKeys), +// so callers building index conditions must never see them here. // //--------------------------------------------------------------------------- CColRefArray * @@ -1792,7 +1794,7 @@ CXformUtils::PdrgpcrIndexKeys(CMemoryPool *mp, CColRefArray *colref_array, const IMDIndex *pmdindex, const IMDRelation *pmdrel) { - return PdrgpcrIndexColumns(mp, colref_array, pmdindex, pmdrel); + return PdrgpcrIndexColumns(mp, colref_array, pmdindex, pmdrel, EicKey); } //--------------------------------------------------------------------------- @@ -1808,7 +1810,7 @@ CColRefSet * CXformUtils::PcrsIndexKeys(CMemoryPool *mp, CColRefArray *colref_array, const IMDIndex *pmdindex, const IMDRelation *pmdrel) { - return PcrsIndexColumns(mp, colref_array, pmdindex, pmdrel); + return PcrsIndexColumns(mp, colref_array, pmdindex, pmdrel, EicKey); } //--------------------------------------------------------------------------- @@ -1817,16 +1819,16 @@ CXformUtils::PcrsIndexKeys(CMemoryPool *mp, CColRefArray *colref_array, // // @doc: // Return the set of columns from the given array of columns which appear -// in the index columns of the specified type (included / key) +// in the index columns of the specified type (key only / key + included) // //--------------------------------------------------------------------------- CColRefSet * CXformUtils::PcrsIndexColumns(CMemoryPool *mp, CColRefArray *colref_array, const IMDIndex *pmdindex, - const IMDRelation *pmdrel) + const IMDRelation *pmdrel, EIndexCols eic) { CColRefArray *pdrgpcrIndexColumns = - PdrgpcrIndexColumns(mp, colref_array, pmdindex, pmdrel); + PdrgpcrIndexColumns(mp, colref_array, pmdindex, pmdrel, eic); CColRefSet *pcrsCols = GPOS_NEW(mp) CColRefSet(mp, pdrgpcrIndexColumns); pdrgpcrIndexColumns->Release(); @@ -1836,11 +1838,11 @@ CXformUtils::PcrsIndexColumns(CMemoryPool *mp, CColRefArray *colref_array, //--------------------------------------------------------------------------- // @function: -// CXformUtils::PdrgpcrIndexColumns +// CXformUtils::PcrsIndexReturnableColumns // // @doc: // Return the set of columns from the given array of columns which are -// retunable through the index (to determine index-only scan capable) +// returnable through the index (to determine index-only scan capable) // //--------------------------------------------------------------------------- CColRefSet * @@ -1872,14 +1874,17 @@ CXformUtils::PcrsIndexReturnableColumns(CMemoryPool *mp, // // @doc: // Return the ordered list of columns from the given array of columns which -// appear in the index columns of the specified type (included / key) +// appear in the index columns of the specified type: key columns only +// (EicKey), or key columns followed by INCLUDE columns (EicKeyAndIncluded) // //--------------------------------------------------------------------------- CColRefArray * CXformUtils::PdrgpcrIndexColumns(CMemoryPool *mp, CColRefArray *colref_array, const IMDIndex *pmdindex, - const IMDRelation *pmdrel) + const IMDRelation *pmdrel, EIndexCols eic) { + GPOS_ASSERT(EicKey == eic || EicKeyAndIncluded == eic); + CColRefArray *pdrgpcrIndex = GPOS_NEW(mp) CColRefArray(mp); // key columns @@ -1894,6 +1899,11 @@ CXformUtils::PdrgpcrIndexColumns(CMemoryPool *mp, CColRefArray *colref_array, pdrgpcrIndex->Append(colref); } + if (EicKey == eic) + { + return pdrgpcrIndex; + } + // included columns for (ULONG ul = 0; ul < pmdindex->IncludedCols(); ul++) { @@ -3026,8 +3036,15 @@ CXformUtils::PexprBitmapSelectBestIndex( continue; } - CColRefArray *indexColumns = CXformUtils::PdrgpcrIndexKeys( - mp, pdrgpcrOutput, pmdindex, pmdrel); + // This array is used below for two things: to check the first key + // of the index, and to compare candidate indexes by width. The + // width comparison is a proxy for how big the index is on disk, so + // it must count the INCLUDE columns as well - they are stored in + // every index tuple. Key columns still come first, so looking at + // element 0 for the first key stays correct. + CColRefArray *indexColumns = CXformUtils::PdrgpcrIndexColumns( + mp, pdrgpcrOutput, pmdindex, pmdrel, + CXformUtils::EicKeyAndIncluded); // make sure the first key of index is included in the scalar predicate // (except for BRIN, which are symmetrical) diff --git a/src/backend/gporca/libnaucrates/include/naucrates/traceflags/traceflags.h b/src/backend/gporca/libnaucrates/include/naucrates/traceflags/traceflags.h index 54a72b082f0..981f4b2d090 100644 --- a/src/backend/gporca/libnaucrates/include/naucrates/traceflags/traceflags.h +++ b/src/backend/gporca/libnaucrates/include/naucrates/traceflags/traceflags.h @@ -261,6 +261,14 @@ enum EOptTraceFlag // so that they don't need to sort it themselves on the coordinator EopttraceDisablePresortedAgg = 103055, + // reuse the child contexts that the same request of a group expression + // reached under another context, instead of optimizing the children again + EopttraceChildOptMemo = 103070, + + // optimize the children of requests that could reuse child contexts, and + // report when the child contexts differ from the reused ones + EopttraceVerifyChildOptMemo = 103071, + /////////////////////////////////////////////////////// ///////////////////// statistics flags //////////////// ////////////////////////////////////////////////////// diff --git a/src/backend/lib/bloomfilter.c b/src/backend/lib/bloomfilter.c index bde46f5b6e6..c7f0ed3f0fd 100644 --- a/src/backend/lib/bloomfilter.c +++ b/src/backend/lib/bloomfilter.c @@ -293,6 +293,47 @@ mod_m(uint32 val, uint64 m) return val & (m - 1); } +Size +bloom_bitset_bytes(const bloom_filter *filter) +{ + return filter != NULL ? (Size) (filter->m / BITS_PER_BYTE) : 0; +} + +const unsigned char * +bloom_bitset_data(const bloom_filter *filter) +{ + return filter != NULL ? filter->bitset : NULL; +} + +/* + * Create a filter sized exactly as bloom_create(total_elems, work_mem, seed) and + * initialize its bitset from the supplied bytes in one step. Returns NULL if the + * supplied length is not the filter's bitset size, so a wrongly-sized bitset is + * rejected rather than partially loaded. This is the only supported way to + * populate a filter from serialized bytes; afterwards a filter is only ever grown + * by bloom_add_element (or a raw bitwise OR of two equal-sized serialized parts, + * as Anser's coordinator fold does). + */ +bloom_filter * +bloom_create_from_bitset(int64 total_elems, int bloom_work_mem, uint64 seed, + const unsigned char *bitset, Size bitset_len) +{ + bloom_filter *filter; + + if (bitset == NULL) + return NULL; + + filter = bloom_create(total_elems, bloom_work_mem, seed); + if (bitset_len != (Size) (filter->m / BITS_PER_BYTE)) + { + bloom_free(filter); + return NULL; + } + + memcpy(filter->bitset, bitset, bitset_len); + return filter; +} + double bloom_false_positive_rate(bloom_filter *filter) { diff --git a/src/backend/optimizer/util/relnode.c b/src/backend/optimizer/util/relnode.c index d32e48c5514..cce6e172f67 100644 --- a/src/backend/optimizer/util/relnode.c +++ b/src/backend/optimizer/util/relnode.c @@ -376,7 +376,8 @@ build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent) } if ((root->parse->commandType == CMD_UPDATE || - root->parse->commandType == CMD_DELETE) && + root->parse->commandType == CMD_DELETE || + root->parse->commandType == CMD_MERGE) && root->parse->resultRelation == relid && GpPolicyIsReplicated(rel->cdbpolicy)) { diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 806ccfcef85..394f8c9b39f 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -596,6 +596,11 @@ SocketBackend(StringInfo inBuf) doing_extended_query_message = false; break; + case GP_SIDEBAND_MESSAGE: /* Cloudberry QD -> QE sideband data */ + maxmsglen = PQ_LARGE_MESSAGE_LIMIT; + doing_extended_query_message = false; + break; + default: /* @@ -6437,6 +6442,18 @@ PostgresMain(const char *dbname, const char *username) */ break; + case GP_SIDEBAND_MESSAGE: /* Cloudberry QD -> QE sideband data */ + /* + * Accept but ignore this message. A QE reads sideband data + * explicitly at the point it expects it; reaching the command + * loop means nobody is waiting for it any more -- typically + * the waiter was interrupted (query cancel) after the QD had + * already written the message. Same reasoning as '?' above, + * but a wider window, since a sideband waiter may block for + * the lifetime of a query. + */ + break; + default: ereport(FATAL, (errcode(ERRCODE_PROTOCOL_VIOLATION), diff --git a/src/backend/utils/misc/guc_gp.c b/src/backend/utils/misc/guc_gp.c index aa3042ea938..11f5588b79c 100644 --- a/src/backend/utils/misc/guc_gp.c +++ b/src/backend/utils/misc/guc_gp.c @@ -153,6 +153,8 @@ bool enable_parallel_semi_join = true; bool enable_parallel_dedup_semi_join = true; bool enable_parallel_dedup_semi_reverse_join = true; bool parallel_query_use_streaming_hashagg = false; +bool optimizer_enable_child_opt_memo = true; +bool optimizer_verify_child_opt_memo = false; bool gp_use_streaming_hashagg = true; bool optimizer_use_streaming_hashagg = true; int gp_appendonly_insert_files = 0; @@ -3260,6 +3262,26 @@ struct config_bool ConfigureNamesBool_gp[] = true, NULL, NULL, NULL }, + { + {"optimizer_enable_child_opt_memo", PGC_USERSET, DEVELOPER_OPTIONS, + gettext_noop("Reuse the child contexts that the same request of a group expression reached under another context."), + NULL, + GUC_NO_SHOW_ALL | GUC_NOT_IN_SAMPLE + }, + &optimizer_enable_child_opt_memo, + true, NULL, NULL + }, + + { + {"optimizer_verify_child_opt_memo", PGC_USERSET, DEVELOPER_OPTIONS, + gettext_noop("Optimize the children of requests that could reuse child contexts and report differences."), + NULL, + GUC_NO_SHOW_ALL | GUC_NOT_IN_SAMPLE + }, + &optimizer_verify_child_opt_memo, + false, NULL, NULL + }, + { {"parallel_query_use_streaming_hashagg", PGC_USERSET, QUERY_TUNING_METHOD, gettext_noop("allow to use of streaming hashagg in parallel query for DISTINCT."), diff --git a/src/common/kmgr_utils.c b/src/common/kmgr_utils.c index ede043d1078..0d2204c4fd8 100644 --- a/src/common/kmgr_utils.c +++ b/src/common/kmgr_utils.c @@ -34,8 +34,6 @@ #include "crypto/kmgr.h" #include "lib/stringinfo.h" #include "storage/fd.h" -#include "postgres.h" -#include "utils/builtins.h" #ifndef FRONTEND #include "pgstat.h" diff --git a/src/common/percentrepl.c b/src/common/percentrepl.c index 53b717edda9..5feaf64ac6e 100644 --- a/src/common/percentrepl.c +++ b/src/common/percentrepl.c @@ -15,16 +15,16 @@ #ifndef FRONTEND #include "postgres.h" +#include "cdb/cdbvars.h" +#include "postmaster/postmaster.h" +#include "utils/builtins.h" #else #include "postgres_fe.h" #include "common/logging.h" #endif -#include "cdb/cdbvars.h" #include "common/percentrepl.h" #include "lib/stringinfo.h" -#include "postmaster/postmaster.h" -#include "utils/builtins.h" /* * replace_percent_placeholders diff --git a/src/include/cdb/cdbdisp.h b/src/include/cdb/cdbdisp.h index 9fac725c5fd..3c7179fffe6 100644 --- a/src/include/cdb/cdbdisp.h +++ b/src/include/cdb/cdbdisp.h @@ -22,11 +22,34 @@ #define CDB_MOTION_LOST_CONTACT_STRING "Interconnect error master lost contact with segment." struct CdbDispatchResults; /* #include "cdb/cdbdispatchresult.h" */ +struct CdbDispatchResult; /* #include "cdb/cdbdispatchresult.h" */ struct CdbPgResults; struct Gang; /* #include "cdb/cdbgang.h" */ struct ResourceOwnerData; +struct pgNotify; /* #include "libpq-fe.h" */ enum GangType; +/* + * Hook for an extension to consume a NOTIFY sent by a QE on its dispatch + * connection, alongside the sequence and endpoint-ack channels the dispatcher + * handles itself. Return true if the notify was consumed, false to let the + * dispatcher fall through to its own handling. + * + * 'dispatchResult' identifies the sending QE and, through its meleeResults, + * gives access to every connection of the current dispatch -- which is what + * lets a handler answer on other QEs' connections (see GP_SIDEBAND_MESSAGE). + * + * The hook runs inside the QD's dispatch-result processing, which is reached + * from the interconnect wait loop while the query is executing. It must + * therefore be quick, must not re-enter the dispatcher, and should not throw + * for conditions it can recover from: an error here propagates into the + * running query. The PGnotify belongs to the caller and is freed after the + * hook returns, so a handler must copy anything it wants to keep. + */ +typedef bool (*cdbdisp_notify_hook_type) (struct CdbDispatchResult *dispatchResult, + struct pgNotify *notify); +extern PGDLLIMPORT cdbdisp_notify_hook_type cdbdisp_notify_hook; + /* * Types of message to QE when we wait for it. */ diff --git a/src/include/cdb/cdbvars.h b/src/include/cdb/cdbvars.h index 5389e5a3979..efd05538a17 100644 --- a/src/include/cdb/cdbvars.h +++ b/src/include/cdb/cdbvars.h @@ -802,6 +802,19 @@ extern const char * lookup_autostats_mode_by_value(GpAutoStatsModeValue val); */ #define CDB_NOTIFY_ENDPOINT_ACK "ack_notify" +/* + * Message type for QD -> QE sideband data sent on the dispatch connection + * outside the normal query protocol, in the same spirit as the '?' sequence + * response. The QE reads it explicitly where it expects it; a message that + * arrives unexpectedly (typically because the QE was cancelled while waiting + * for one) is accepted and discarded by the command loop, so it cannot break + * message-boundary sync. + * + * Unlike '?', a sideband message may be large: it is read with + * PQ_LARGE_MESSAGE_LIMIT. + */ +#define GP_SIDEBAND_MESSAGE '!' + typedef enum WarehouseStatus { WAREHOUSE_STATUS_CREATING, diff --git a/src/include/commands/explain_gp.h b/src/include/commands/explain_gp.h index 016e5449543..c9210507e9f 100644 --- a/src/include/commands/explain_gp.h +++ b/src/include/commands/explain_gp.h @@ -34,6 +34,7 @@ typedef struct CdbExplain_StatInst double ntuples; /* Total tuples produced */ double ntuples2; double nloops; /* # of run cycles for this node */ + BufferUsage bufusage; /* Total buffer usage */ double nfiltered1; double nfiltered2; bool prf_work; diff --git a/src/include/executor/instrument.h b/src/include/executor/instrument.h index bce4b33c2f8..1fba5b70de3 100644 --- a/src/include/executor/instrument.h +++ b/src/include/executor/instrument.h @@ -64,7 +64,7 @@ typedef enum InstrumentOption { INSTRUMENT_NONE = 0, INSTRUMENT_TIMER = 1 << 0, /* needs timer (and row counts) */ - INSTRUMENT_BUFFERS = 1 << 1, /* needs buffer usage (not implemented yet) */ + INSTRUMENT_BUFFERS = 1 << 1, /* needs buffer usage */ INSTRUMENT_ROWS = 1 << 2, /* needs row count */ INSTRUMENT_WAL = 1 << 3, /* needs WAL usage */ INSTRUMENT_MEMORY_DETAIL = 0x20000000, /* needs detailed memory accounting */ @@ -135,6 +135,7 @@ extern void InstrAggNode(Instrumentation *dst, Instrumentation *add); extern void InstrStartParallelQuery(void); extern void InstrEndParallelQuery(BufferUsage *bufusage, WalUsage *walusage); extern void InstrAccumParallelQuery(BufferUsage *bufusage, WalUsage *walusage); +extern void BufferUsageAdd(BufferUsage *dst, const BufferUsage *add); extern void BufferUsageAccumDiff(BufferUsage *dst, const BufferUsage *add, const BufferUsage *sub); extern void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, diff --git a/src/include/lib/bloomfilter.h b/src/include/lib/bloomfilter.h index 05b736f566e..51afa223e03 100644 --- a/src/include/lib/bloomfilter.h +++ b/src/include/lib/bloomfilter.h @@ -27,5 +27,11 @@ extern double bloom_false_positive_rate(bloom_filter *filter); extern uint64 bloom_total_bits(bloom_filter *filter); extern bloom_filter *bloom_create_aggresive(int64 total_elems, int work_mem, uint64 seed); +extern Size bloom_bitset_bytes(const bloom_filter *filter); +extern const unsigned char *bloom_bitset_data(const bloom_filter *filter); +extern bloom_filter *bloom_create_from_bitset(int64 total_elems, + int bloom_work_mem, uint64 seed, + const unsigned char *bitset, + Size bitset_len); #endif /* BLOOMFILTER_H */ diff --git a/src/include/utils/guc.h b/src/include/utils/guc.h index 226beabaf89..980eb4340cc 100644 --- a/src/include/utils/guc.h +++ b/src/include/utils/guc.h @@ -300,6 +300,8 @@ extern bool enable_parallel_semi_join; extern bool enable_parallel_dedup_semi_join; extern bool enable_parallel_dedup_semi_reverse_join; extern bool parallel_query_use_streaming_hashagg; +extern bool optimizer_enable_child_opt_memo; +extern bool optimizer_verify_child_opt_memo; extern bool gp_use_streaming_hashagg; extern bool optimizer_use_streaming_hashagg; extern int gp_appendonly_insert_files; diff --git a/src/include/utils/unsync_guc_name.h b/src/include/utils/unsync_guc_name.h index a82fe9b7afd..e2fa575da4f 100644 --- a/src/include/utils/unsync_guc_name.h +++ b/src/include/utils/unsync_guc_name.h @@ -404,6 +404,7 @@ "optimizer_enable_associativity", "optimizer_enable_bitmapscan", "optimizer_enable_broadcast_nestloop_outer_child", + "optimizer_enable_child_opt_memo", "optimizer_enable_constant_expression_evaluation", "optimizer_enable_ctas", "optimizer_enable_derive_stats_all_groups", @@ -513,6 +514,7 @@ "optimizer_use_external_constant_expression_evaluation_for_ints", "optimizer_use_gpdb_allocators", "optimizer_use_streaming_hashagg", + "optimizer_verify_child_opt_memo", "optimizer_xform_bind_threshold", "parallel_leader_participation", "parallel_query_use_streaming_hashagg", diff --git a/src/test/modules/Makefile b/src/test/modules/Makefile index 77af107b55e..8b67ea905c7 100644 --- a/src/test/modules/Makefile +++ b/src/test/modules/Makefile @@ -10,6 +10,7 @@ SUBDIRS = \ delay_execution \ dummy_index_am \ dummy_seclabel \ + libpq_isbusy_notify \ libpq_pipeline \ plsample \ snapshot_too_old \ diff --git a/src/test/modules/libpq_isbusy_notify/.gitignore b/src/test/modules/libpq_isbusy_notify/.gitignore new file mode 100644 index 00000000000..5dd57c4b4f5 --- /dev/null +++ b/src/test/modules/libpq_isbusy_notify/.gitignore @@ -0,0 +1,5 @@ +# Generated subdirectories +/log/ +/results/ +/tmp_check/ +/libpq_isbusy_notify diff --git a/src/test/modules/libpq_isbusy_notify/Makefile b/src/test/modules/libpq_isbusy_notify/Makefile new file mode 100644 index 00000000000..7ea27d27d95 --- /dev/null +++ b/src/test/modules/libpq_isbusy_notify/Makefile @@ -0,0 +1,27 @@ +# src/test/modules/libpq_isbusy_notify/Makefile + +PGFILEDESC = "libpq_isbusy_notify - test program for PQisBusy and queued notifications" +PGAPPICON = win32 + +PROGRAM = libpq_isbusy_notify +OBJS = $(WIN32RES) libpq_isbusy_notify.o + +NO_INSTALL = 1 + +# libpq-int.h: the test builds a connection state by hand rather than reading +# one off a socket, which is the only way to reach the case without a server. +PG_CPPFLAGS = -I$(libpq_srcdir) +PG_LIBS_INTERNAL += $(libpq_pgport) + +TAP_TESTS = 1 + +ifdef USE_PGXS +PG_CONFIG = pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) +else +subdir = src/test/modules/libpq_isbusy_notify +top_builddir = ../../../.. +include $(top_builddir)/src/Makefile.global +include $(top_srcdir)/contrib/contrib-global.mk +endif diff --git a/src/test/modules/libpq_isbusy_notify/libpq_isbusy_notify.c b/src/test/modules/libpq_isbusy_notify/libpq_isbusy_notify.c new file mode 100644 index 00000000000..ec2da133e15 --- /dev/null +++ b/src/test/modules/libpq_isbusy_notify/libpq_isbusy_notify.c @@ -0,0 +1,161 @@ +/*------------------------------------------------------------------------- + * + * libpq_isbusy_notify.c + * PQisBusy() does not report a notification that is ready to collect. + * + * libpq's parser handles a NOTIFY by queueing it on the connection and + * carrying on: pqParseInput3's 'A' arm calls getNotify() and then breaks, + * never touching asyncStatus. PQisBusy() reports asyncStatus, so while the + * command that is in flight has not finished it keeps answering true -- even + * though a complete notification is sitting in conn->notifyHead with nothing + * left unread in the connection's buffer. + * + * Anything that treats !PQisBusy() as "nothing is ready for me" therefore has + * a blind spot, and the blind spot bites when the bytes reached the buffer + * without the socket staying readable. That is not exotic: a write that + * cannot complete in one go reads the peer's pending output to avoid + * deadlocking against it (pqSendSome -> pqReadData, fe-misc.c), so the + * notification can already be in our memory while poll() has nothing to + * report. The coordinator's dispatcher hit exactly this, which is why it now + * drains notifications before deciding whether to wait + * (checkDispatchResult, cdbdisp_async.c). + * + * This test pins the libpq behaviour that reasoning depends on. No server is + * involved and nothing is read from a socket: the input buffer is filled by + * hand with one complete NOTIFY message, which models the state after the + * bytes have already been absorbed. If libpq ever changes so that a queued + * notification stops PQisBusy() returning true, this fails -- and the drain in + * the dispatcher has become unnecessary. + * + * IDENTIFICATION + * src/test/modules/libpq_isbusy_notify/libpq_isbusy_notify.c + * + *------------------------------------------------------------------------- + */ +#include "postgres_fe.h" + +#include "libpq-fe.h" +#include "libpq-int.h" +#include "port/pg_bswap.h" + +/* A NOTIFY as it appears on the wire, minus the leading message type byte. */ +static const char notify_channel[] = "anser"; +static const char notify_payload[] = "part"; + +int +main(void) +{ + PGconn *conn; + uint32 netlen; + uint32 netpid; + int msglen; + int pos = 0; + int busy; + int unread; + PGnotify *notify; + int failures = 0; + + /* + * PQconnectStart() allocates the buffers and returns without waiting for + * the connection, which is all we need -- the path is deliberately one + * that cannot exist, and the resulting status is overwritten below. We + * never read or write the socket, so no server is required. + */ + conn = PQconnectStart("host=/nonexistent-libpq-isbusy-notify dbname=postgres"); + if (conn == NULL) + { + fprintf(stderr, "could not allocate a connection\n"); + return 2; + } + + /* 4-byte length, 4-byte sender pid, then two NUL-terminated strings. */ + msglen = 4 + 4 + sizeof(notify_channel) + sizeof(notify_payload); + if (conn->inBufSize < msglen + 1) + { + fprintf(stderr, "input buffer is too small for the test message\n"); + PQfinish(conn); + return 2; + } + + /* + * Put the connection in the state it would be in with a command still in + * flight and one complete notification already parsed off the socket. + */ + conn->status = CONNECTION_OK; + conn->asyncStatus = PGASYNC_BUSY; + conn->inStart = conn->inCursor = 0; + + conn->inBuffer[pos++] = 'A'; + netlen = pg_hton32(msglen); + memcpy(conn->inBuffer + pos, &netlen, 4); + pos += 4; + netpid = pg_hton32(123); + memcpy(conn->inBuffer + pos, &netpid, 4); + pos += 4; + memcpy(conn->inBuffer + pos, notify_channel, sizeof(notify_channel)); + pos += sizeof(notify_channel); + memcpy(conn->inBuffer + pos, notify_payload, sizeof(notify_payload)); + pos += sizeof(notify_payload); + conn->inEnd = pos; + + /* Parses the buffer; never touches the socket. */ + busy = PQisBusy(conn); + unread = conn->inEnd - conn->inStart; + + printf("PQisBusy=%d notify_queued=%d unread_bytes=%d\n", + busy, conn->notifyHead != NULL, unread); + + /* + * The three halves of the trap, each worth failing on separately: the + * notification is ready, the connection still reports itself busy, and + * there is nothing left that would make the socket readable again. + */ + if (conn->notifyHead == NULL) + { + fprintf(stderr, "FAIL: the notification was not queued\n"); + failures++; + } + if (!busy) + { + fprintf(stderr, + "FAIL: PQisBusy() reported the notification, so a caller " + "watching it alone would not miss one\n"); + failures++; + } + if (unread != 0) + { + fprintf(stderr, + "FAIL: %d byte(s) left unparsed, so the message was not " + "complete and the test proved nothing\n", unread); + failures++; + } + + /* And it really is collectable, by the call that does look for it. */ + notify = PQnotifies(conn); + if (notify == NULL) + { + fprintf(stderr, "FAIL: PQnotifies() returned nothing\n"); + failures++; + } + else + { + printf("PQnotifies: channel=%s payload=%s\n", + notify->relname, notify->extra); + if (strcmp(notify->relname, notify_channel) != 0 || + strcmp(notify->extra, notify_payload) != 0) + { + fprintf(stderr, "FAIL: the notification did not round-trip\n"); + failures++; + } + PQfreemem(notify); + } + + /* + * Nothing was ever connected, so stop PQfinish() from trying to send a + * terminate message down a socket we invented a state for. + */ + conn->status = CONNECTION_BAD; + PQfinish(conn); + + return failures == 0 ? 0 : 1; +} diff --git a/src/test/modules/libpq_isbusy_notify/meson.build b/src/test/modules/libpq_isbusy_notify/meson.build new file mode 100644 index 00000000000..c13e1dfcdbd --- /dev/null +++ b/src/test/modules/libpq_isbusy_notify/meson.build @@ -0,0 +1,47 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +libpq_isbusy_notify_sources = files( + 'libpq_isbusy_notify.c', +) + +if host_system == 'windows' + libpq_isbusy_notify_sources += rc_bin_gen.process(win32ver_rc, extra_args: [ + '--NAME', 'libpq_isbusy_notify', + '--FILEDESC', 'libpq_isbusy_notify - test program for PQisBusy and queued notifications',]) +endif + +libpq_isbusy_notify = executable('libpq_isbusy_notify', + libpq_isbusy_notify_sources, + dependencies: [frontend_code, libpq], + kwargs: default_bin_args + { + 'install': false, + }, +) +testprep_targets += libpq_isbusy_notify + +tests += { + 'name': 'libpq_isbusy_notify', + 'sd': meson.current_source_dir(), + 'bd': meson.current_build_dir(), + 'tap': { + 'tests': [ + 't/001_isbusy_notify.pl', + ], + 'deps': [libpq_isbusy_notify], + }, +} diff --git a/src/test/modules/libpq_isbusy_notify/t/001_isbusy_notify.pl b/src/test/modules/libpq_isbusy_notify/t/001_isbusy_notify.pl new file mode 100644 index 00000000000..1b29f773e0f --- /dev/null +++ b/src/test/modules/libpq_isbusy_notify/t/001_isbusy_notify.pl @@ -0,0 +1,33 @@ + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +use strict; +use warnings; + +use PostgreSQL::Test::Utils; +use Test::More; + +# No cluster is started. The program constructs the connection state it needs +# by hand and never reads or writes a socket, which is the point: the state +# under test is "the bytes are already in our buffer and the socket has gone +# quiet", awkward to provoke against a live server and trivial to build. +command_ok( + ['libpq_isbusy_notify'], + 'PQisBusy() stays true while a complete notification is queued'); + +done_testing(); diff --git a/src/test/modules/meson.build b/src/test/modules/meson.build index 9556bac4ad4..e2f8387fe33 100644 --- a/src/test/modules/meson.build +++ b/src/test/modules/meson.build @@ -6,6 +6,7 @@ subdir('delay_execution') subdir('dummy_index_am') subdir('dummy_seclabel') subdir('ldap_password_func') +subdir('libpq_isbusy_notify') subdir('libpq_pipeline') subdir('plsample') subdir('snapshot_too_old') diff --git a/src/test/regress/expected/gp_covering_index.out b/src/test/regress/expected/gp_covering_index.out index e67cd8d7ad1..f3bf8903654 100644 --- a/src/test/regress/expected/gp_covering_index.out +++ b/src/test/regress/expected/gp_covering_index.out @@ -837,5 +837,131 @@ SELECT b FROM test_combined_index_scan WHERE a < 42 OR b < 42; Optimizer: Postgres query optimizer (6 rows) +-- Test predicates on INCLUDE-only columns (https://github.com/apache/cloudberry/issues/1948) +-- +-- A predicate that only references INCLUDE ("payload") columns must never be +-- turned into an index condition: the executor rejects index quals on non-key +-- attributes ("bogus index qualification"). Boolean column references and +-- IS [NOT] NULL tests used to slip through ORCA's index predicate extraction, +-- so cover them explicitly. +CREATE TABLE test_include_col_pred(c0 boolean, c1 boolean, c2 int) DISTRIBUTED BY (c2); +CREATE INDEX i_test_include_col_pred ON test_include_col_pred(c0) INCLUDE (c1); +INSERT INTO test_include_col_pred VALUES (true, true, 1), (false, true, 2), (true, false, 3), (true, NULL, 4); +VACUUM ANALYZE test_include_col_pred; +-- KEYS: [c0] INCLUDED: [c1] +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE c1; + QUERY PLAN +------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Seq Scan on test_include_col_pred + Filter: c1 + Optimizer: Postgres query optimizer +(4 rows) + +SELECT * FROM test_include_col_pred WHERE c1 ORDER BY c2; + c0 | c1 | c2 +----+----+---- + t | t | 1 + f | t | 2 +(2 rows) + +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE NOT c1; + QUERY PLAN +------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Seq Scan on test_include_col_pred + Filter: (NOT c1) + Optimizer: Postgres query optimizer +(4 rows) + +SELECT * FROM test_include_col_pred WHERE NOT c1 ORDER BY c2; + c0 | c1 | c2 +----+----+---- + t | f | 3 +(1 row) + +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE c1 IS NULL; + QUERY PLAN +------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Seq Scan on test_include_col_pred + Filter: (c1 IS NULL) + Optimizer: Postgres query optimizer +(4 rows) + +SELECT * FROM test_include_col_pred WHERE c1 IS NULL ORDER BY c2; + c0 | c1 | c2 +----+----+---- + t | | 4 +(1 row) + +EXPLAIN (COSTS OFF) +SELECT c1 FROM test_include_col_pred WHERE c1 IS NOT NULL; + QUERY PLAN +------------------------------------------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Index Only Scan using i_test_include_col_pred on test_include_col_pred + Filter: (c1 IS NOT NULL) + Optimizer: Postgres query optimizer +(4 rows) + +SELECT c1 FROM test_include_col_pred WHERE c1 IS NOT NULL ORDER BY 1; + c1 +---- + f + t + t +(3 rows) + +-- key predicate combined with an INCLUDE-column predicate: only the key +-- predicate may become an index condition, the rest must stay a filter +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE c0 AND c1; + QUERY PLAN +------------------------------------------------------------------------- + Gather Motion 3:1 (slice1; segments: 3) + -> Index Scan using i_test_include_col_pred on test_include_col_pred + Index Cond: (c0 = true) + Filter: c1 + Optimizer: Postgres query optimizer +(5 rows) + +SELECT * FROM test_include_col_pred WHERE c0 AND c1 ORDER BY c2; + c0 | c1 | c2 +----+----+---- + t | t | 1 +(1 row) + +-- When two indexes have the same keys, the bitmap candidate with fewer stored +-- columns must still win: INCLUDE columns make the index wider on disk even +-- though they can never be used in an index condition. i_bmidx_wide is created +-- first on purpose, so a tie on width would hand it the plan. +CREATE TABLE test_bitmap_include_width(a int, b int, payload text) DISTRIBUTED BY (a); +CREATE INDEX i_bmidx_wide ON test_bitmap_include_width(a) INCLUDE (payload); +CREATE INDEX i_bmidx_narrow ON test_bitmap_include_width(a); +CREATE INDEX i_bmidx_b ON test_bitmap_include_width(b); +INSERT INTO test_bitmap_include_width SELECT i, i, repeat('x', 100) FROM generate_series(1, 2000) i; +VACUUM ANALYZE test_bitmap_include_width; +-- KEYS: [a] INCLUDED: [payload] +-- KEYS: [a] +-- KEYS: [b] +EXPLAIN (COSTS OFF) +SELECT * FROM test_bitmap_include_width WHERE a IN (1, 100, 1000) OR b = 17; + QUERY PLAN +--------------------------------------------------------------------------- + Gather Motion 3:1 (slice1; segments: 3) + -> Bitmap Heap Scan on test_bitmap_include_width + Recheck Cond: ((a = ANY ('{1,100,1000}'::integer[])) OR (b = 17)) + -> BitmapOr + -> Bitmap Index Scan on i_bmidx_narrow + Index Cond: (a = ANY ('{1,100,1000}'::integer[])) + -> Bitmap Index Scan on i_bmidx_b + Index Cond: (b = 17) + Optimizer: Postgres query optimizer +(9 rows) + reset optimizer_trace_fallback; reset enable_seqscan; diff --git a/src/test/regress/expected/gp_covering_index_optimizer.out b/src/test/regress/expected/gp_covering_index_optimizer.out index d709e659256..a27737cc2c3 100644 --- a/src/test/regress/expected/gp_covering_index_optimizer.out +++ b/src/test/regress/expected/gp_covering_index_optimizer.out @@ -795,5 +795,131 @@ SELECT b FROM test_combined_index_scan WHERE a < 42 OR b < 42; Optimizer: Pivotal Optimizer (GPORCA) (5 rows) +-- Test predicates on INCLUDE-only columns (https://github.com/apache/cloudberry/issues/1948) +-- +-- A predicate that only references INCLUDE ("payload") columns must never be +-- turned into an index condition: the executor rejects index quals on non-key +-- attributes ("bogus index qualification"). Boolean column references and +-- IS [NOT] NULL tests used to slip through ORCA's index predicate extraction, +-- so cover them explicitly. +CREATE TABLE test_include_col_pred(c0 boolean, c1 boolean, c2 int) DISTRIBUTED BY (c2); +CREATE INDEX i_test_include_col_pred ON test_include_col_pred(c0) INCLUDE (c1); +INSERT INTO test_include_col_pred VALUES (true, true, 1), (false, true, 2), (true, false, 3), (true, NULL, 4); +VACUUM ANALYZE test_include_col_pred; +-- KEYS: [c0] INCLUDED: [c1] +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE c1; + QUERY PLAN +------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Seq Scan on test_include_col_pred + Filter: c1 + Optimizer: GPORCA +(4 rows) + +SELECT * FROM test_include_col_pred WHERE c1 ORDER BY c2; + c0 | c1 | c2 +----+----+---- + t | t | 1 + f | t | 2 +(2 rows) + +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE NOT c1; + QUERY PLAN +------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Seq Scan on test_include_col_pred + Filter: (NOT c1) + Optimizer: GPORCA +(4 rows) + +SELECT * FROM test_include_col_pred WHERE NOT c1 ORDER BY c2; + c0 | c1 | c2 +----+----+---- + t | f | 3 +(1 row) + +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE c1 IS NULL; + QUERY PLAN +------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Seq Scan on test_include_col_pred + Filter: (c1 IS NULL) + Optimizer: GPORCA +(4 rows) + +SELECT * FROM test_include_col_pred WHERE c1 IS NULL ORDER BY c2; + c0 | c1 | c2 +----+----+---- + t | | 4 +(1 row) + +EXPLAIN (COSTS OFF) +SELECT c1 FROM test_include_col_pred WHERE c1 IS NOT NULL; + QUERY PLAN +------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Seq Scan on test_include_col_pred + Filter: (NOT (c1 IS NULL)) + Optimizer: GPORCA +(4 rows) + +SELECT c1 FROM test_include_col_pred WHERE c1 IS NOT NULL ORDER BY 1; + c1 +---- + f + t + t +(3 rows) + +-- key predicate combined with an INCLUDE-column predicate: only the key +-- predicate may become an index condition, the rest must stay a filter +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE c0 AND c1; + QUERY PLAN +------------------------------------------------------------------------- + Gather Motion 3:1 (slice1; segments: 3) + -> Index Scan using i_test_include_col_pred on test_include_col_pred + Index Cond: (c0 = true) + Filter: c1 + Optimizer: GPORCA +(5 rows) + +SELECT * FROM test_include_col_pred WHERE c0 AND c1 ORDER BY c2; + c0 | c1 | c2 +----+----+---- + t | t | 1 +(1 row) + +-- When two indexes have the same keys, the bitmap candidate with fewer stored +-- columns must still win: INCLUDE columns make the index wider on disk even +-- though they can never be used in an index condition. i_bmidx_wide is created +-- first on purpose, so a tie on width would hand it the plan. +CREATE TABLE test_bitmap_include_width(a int, b int, payload text) DISTRIBUTED BY (a); +CREATE INDEX i_bmidx_wide ON test_bitmap_include_width(a) INCLUDE (payload); +CREATE INDEX i_bmidx_narrow ON test_bitmap_include_width(a); +CREATE INDEX i_bmidx_b ON test_bitmap_include_width(b); +INSERT INTO test_bitmap_include_width SELECT i, i, repeat('x', 100) FROM generate_series(1, 2000) i; +VACUUM ANALYZE test_bitmap_include_width; +-- KEYS: [a] INCLUDED: [payload] +-- KEYS: [a] +-- KEYS: [b] +EXPLAIN (COSTS OFF) +SELECT * FROM test_bitmap_include_width WHERE a IN (1, 100, 1000) OR b = 17; + QUERY PLAN +--------------------------------------------------------------------------- + Gather Motion 3:1 (slice1; segments: 3) + -> Bitmap Heap Scan on test_bitmap_include_width + Recheck Cond: ((a = ANY ('{1,100,1000}'::integer[])) OR (b = 17)) + -> BitmapOr + -> Bitmap Index Scan on i_bmidx_narrow + Index Cond: (a = ANY ('{1,100,1000}'::integer[])) + -> Bitmap Index Scan on i_bmidx_b + Index Cond: (b = 17) + Optimizer: GPORCA +(9 rows) + reset optimizer_trace_fallback; reset enable_seqscan; diff --git a/src/test/regress/expected/gp_explain.out b/src/test/regress/expected/gp_explain.out index 6fc3b1ef844..3e02c273089 100644 --- a/src/test/regress/expected/gp_explain.out +++ b/src/test/regress/expected/gp_explain.out @@ -41,6 +41,29 @@ begin end loop; end; $$ language plpgsql; +-- Run EXPLAIN ANALYZE BUFFERS, preserving the positions of buffer statistics +create or replace function explain_buffers_filter(explain_query text) returns setof text as +$$ +declare + explainrow text; +begin + for explainrow in execute + 'EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF, SUMMARY OFF) ' || explain_query + loop + if explainrow ~ '^\s*Buffers:' then + explainrow := regexp_replace(explainrow, 'Buffers:.*$', 'Buffers: N'); + else + explainrow := regexp_replace(explainrow, + '\m\d+([kK]B?)\M', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '\m\d+(x)', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '-?\m\d+\M', 'N', 'g'); + end if; + return next explainrow; + end loop; +end; +$$ language plpgsql; -- Test explain wal option CREATE TABLE explainwal (id int4); NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Apache Cloudberry data distribution key for this table. @@ -104,6 +127,23 @@ NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' a HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew. INSERT INTO explaintest SELECT generate_series(1, 10); ANALYZE explaintest; +-- Compare normalized buffer placement +-- explain_processing_off +SELECT plan_line +FROM explain_buffers_filter($$ + SELECT * FROM explaintest; +$$) WITH ORDINALITY AS plan(plan_line, line_number) +ORDER BY line_number; + plan_line +------------------------------------------------------------------ + Gather Motion N:N (slice1; segments: N) (actual rows=N loops=N) + -> Seq Scan on explaintest (actual rows=N loops=N) + Buffers: N + Planning: + Buffers: N + Optimizer: Postgres query optimizer +(6 rows) + EXPLAIN ANALYZE SELECT * FROM explaintest; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------- diff --git a/src/test/regress/expected/gp_explain_optimizer.out b/src/test/regress/expected/gp_explain_optimizer.out index 4244ab91437..8c500ee69e0 100644 --- a/src/test/regress/expected/gp_explain_optimizer.out +++ b/src/test/regress/expected/gp_explain_optimizer.out @@ -41,6 +41,29 @@ begin end loop; end; $$ language plpgsql; +-- Run EXPLAIN ANALYZE BUFFERS, preserving the positions of buffer statistics +create or replace function explain_buffers_filter(explain_query text) returns setof text as +$$ +declare + explainrow text; +begin + for explainrow in execute + 'EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF, SUMMARY OFF) ' || explain_query + loop + if explainrow ~ '^\s*Buffers:' then + explainrow := regexp_replace(explainrow, 'Buffers:.*$', 'Buffers: N'); + else + explainrow := regexp_replace(explainrow, + '\m\d+([kK]B?)\M', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '\m\d+(x)', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '-?\m\d+\M', 'N', 'g'); + end if; + return next explainrow; + end loop; +end; +$$ language plpgsql; -- Test explain wal option CREATE TABLE explainwal (id int4); NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Apache Cloudberry data distribution key for this table. @@ -104,6 +127,23 @@ NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' a HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew. INSERT INTO explaintest SELECT generate_series(1, 10); ANALYZE explaintest; +-- Compare normalized buffer placement +-- explain_processing_off +SELECT plan_line +FROM explain_buffers_filter($$ + SELECT * FROM explaintest; +$$) WITH ORDINALITY AS plan(plan_line, line_number) +ORDER BY line_number; + plan_line +------------------------------------------------------------------ + Gather Motion N:N (slice1; segments: N) (actual rows=N loops=N) + -> Seq Scan on explaintest (actual rows=N loops=N) + Buffers: N + Planning: + Buffers: N + Optimizer: GPORCA +(6 rows) + EXPLAIN ANALYZE SELECT * FROM explaintest; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------- diff --git a/src/test/regress/expected/memoize.out b/src/test/regress/expected/memoize.out index 96e6737be27..afebea20104 100644 --- a/src/test/regress/expected/memoize.out +++ b/src/test/regress/expected/memoize.out @@ -118,6 +118,10 @@ SELECT d1::numeric, d1::text FROM ( INSERT INTO expr_key SELECT * FROM expr_key; CREATE INDEX expr_key_idx_x_t ON expr_key (x, t); VACUUM ANALYZE expr_key; +-- ORCA may pick either an Index Scan or an Index Only Scan for the inner +-- side depending on how much of expr_key the VACUUM above could mark +-- all-visible, which varies with concurrently running tests. Pin the plan. +SET optimizer_enable_indexonlyscan TO off; -- Ensure we get we get a cache miss and hit for each of the 20 distinct values SELECT explain_memoize(' SELECT * FROM expr_key t1 INNER JOIN expr_key t2 @@ -141,6 +145,7 @@ ON t1.x = t2.t::numeric AND t1.t::numeric = t2.x;', false); Optimizer: Postgres query optimizer (15 rows) +RESET optimizer_enable_indexonlyscan; DROP TABLE expr_key; -- Reduce work_mem and hash_mem_multiplier so that we see some cache evictions SET work_mem TO '64kB'; diff --git a/src/test/regress/expected/memoize_optimizer.out b/src/test/regress/expected/memoize_optimizer.out index 08ac4f997ef..6dd4bc6ce5c 100644 --- a/src/test/regress/expected/memoize_optimizer.out +++ b/src/test/regress/expected/memoize_optimizer.out @@ -115,6 +115,10 @@ SELECT d1::numeric, d1::text FROM ( INSERT INTO expr_key SELECT * FROM expr_key; CREATE INDEX expr_key_idx_x_t ON expr_key (x, t); VACUUM ANALYZE expr_key; +-- ORCA may pick either an Index Scan or an Index Only Scan for the inner +-- side depending on how much of expr_key the VACUUM above could mark +-- all-visible, which varies with concurrently running tests. Pin the plan. +SET optimizer_enable_indexonlyscan TO off; -- Ensure we get we get a cache miss and hit for each of the 20 distinct values SELECT explain_memoize(' SELECT * FROM expr_key t1 INNER JOIN expr_key t2 @@ -133,6 +137,7 @@ ON t1.x = t2.t::numeric AND t1.t::numeric = t2.x;', false); Optimizer: GPORCA (10 rows) +RESET optimizer_enable_indexonlyscan; DROP TABLE expr_key; -- Reduce work_mem and hash_mem_multiplier so that we see some cache evictions SET work_mem TO '64kB'; diff --git a/src/test/regress/expected/rpt.out b/src/test/regress/expected/rpt.out index 103d426a8c3..3b63bf4be7e 100644 --- a/src/test/regress/expected/rpt.out +++ b/src/test/regress/expected/rpt.out @@ -541,6 +541,49 @@ select * from foo; ---+--- (0 rows) +-- MERGE into a replicated table must broadcast the source to every segment, +-- like UPDATE/DELETE, otherwise each replica only sees the source rows stored +-- on its own segment and the replicas diverge. +create table rpt_merge_tgt(k int, v int) distributed replicated; +create table rpt_merge_src(k int, v int) distributed by (k); +insert into rpt_merge_tgt select g, 0 from generate_series(1, 10) g; +insert into rpt_merge_src select g, 1 from generate_series(6, 15) g; +explain (costs off) +merge into rpt_merge_tgt t using rpt_merge_src s on t.k = s.k + when matched then update set v = s.v + when not matched then insert values (s.k, s.v); + QUERY PLAN +--------------------------------------------------------- + Merge on rpt_merge_tgt t + -> Hash Left Join + Hash Cond: (s.k = t.k) + -> Broadcast Motion 3:3 (slice1; segments: 3) + -> Seq Scan on rpt_merge_src s + -> Hash + -> Seq Scan on rpt_merge_tgt t + Optimizer: Postgres query optimizer +(8 rows) + +merge into rpt_merge_tgt t using rpt_merge_src s on t.k = s.k + when matched then update set v = s.v + when not matched then insert values (s.k, s.v); +select count(*), count(*) filter (where v = 1) as v_is_1 from rpt_merge_tgt; + count | v_is_1 +-------+-------- + 15 | 10 +(1 row) + +-- every replica must hold the same 15 rows +select gp_execution_segment() as seg, count(*), count(*) filter (where v = 1) as v_is_1 +from gp_dist_random('rpt_merge_tgt') group by 1 order by 1; + seg | count | v_is_1 +-----+-------+-------- + 0 | 15 | 10 + 1 | 15 | 10 + 2 | 15 | 10 +(3 rows) + +drop table rpt_merge_tgt, rpt_merge_src; -- Test replicate table within init plan insert into foo values (1, 1), (2, 1); select * from bar where exists (select * from foo); @@ -973,6 +1016,14 @@ explain (costs off) delete from t_replicate_volatile using t_replicate_volatile ERROR: could not devise a plan (cdbpath.c:2578) explain (costs off) update t_replicate_volatile set a = random(); ERROR: could not devise a plan. (cdbpath.c:2440) +-- merge: volatile functions in MERGE actions are evaluated by ModifyTable, +-- not in the subplan targetlist, but must be rejected all the same +explain (costs off) merge into t_replicate_volatile using t_hashdist x on x.a = t_replicate_volatile.a when matched then update set b = random(); +ERROR: could not devise a plan. (cdbpath.c:2862) +explain (costs off) merge into t_replicate_volatile using t_hashdist x on x.a = t_replicate_volatile.a when not matched then insert values (x.a, random()); +ERROR: could not devise a plan. (cdbpath.c:2862) +explain (costs off) merge into t_replicate_volatile using t_hashdist x on x.a = t_replicate_volatile.a when matched and random() < 0.5 then delete; +ERROR: could not devise a plan. (cdbpath.c:2862) -- limit explain (costs off) insert into t_replicate_volatile select * from t_replicate_volatile limit random(); QUERY PLAN diff --git a/src/test/regress/expected/rpt_optimizer.out b/src/test/regress/expected/rpt_optimizer.out index 5612de4e95f..ca8621bcf94 100644 --- a/src/test/regress/expected/rpt_optimizer.out +++ b/src/test/regress/expected/rpt_optimizer.out @@ -541,6 +541,49 @@ select * from foo; ---+--- (0 rows) +-- MERGE into a replicated table must broadcast the source to every segment, +-- like UPDATE/DELETE, otherwise each replica only sees the source rows stored +-- on its own segment and the replicas diverge. +create table rpt_merge_tgt(k int, v int) distributed replicated; +create table rpt_merge_src(k int, v int) distributed by (k); +insert into rpt_merge_tgt select g, 0 from generate_series(1, 10) g; +insert into rpt_merge_src select g, 1 from generate_series(6, 15) g; +explain (costs off) +merge into rpt_merge_tgt t using rpt_merge_src s on t.k = s.k + when matched then update set v = s.v + when not matched then insert values (s.k, s.v); + QUERY PLAN +--------------------------------------------------------- + Merge on rpt_merge_tgt t + -> Hash Left Join + Hash Cond: (s.k = t.k) + -> Broadcast Motion 3:3 (slice1; segments: 3) + -> Seq Scan on rpt_merge_src s + -> Hash + -> Seq Scan on rpt_merge_tgt t + Optimizer: Postgres query optimizer +(8 rows) + +merge into rpt_merge_tgt t using rpt_merge_src s on t.k = s.k + when matched then update set v = s.v + when not matched then insert values (s.k, s.v); +select count(*), count(*) filter (where v = 1) as v_is_1 from rpt_merge_tgt; + count | v_is_1 +-------+-------- + 15 | 10 +(1 row) + +-- every replica must hold the same 15 rows +select gp_execution_segment() as seg, count(*), count(*) filter (where v = 1) as v_is_1 +from gp_dist_random('rpt_merge_tgt') group by 1 order by 1; + seg | count | v_is_1 +-----+-------+-------- + 0 | 15 | 10 + 1 | 15 | 10 + 2 | 15 | 10 +(3 rows) + +drop table rpt_merge_tgt, rpt_merge_src; -- Test replicate table within init plan insert into foo values (1, 1), (2, 1); select * from bar where exists (select * from foo); @@ -965,6 +1008,14 @@ explain (costs off) delete from t_replicate_volatile using t_replicate_volatile ERROR: could not devise a plan (cdbpath.c:2578) explain (costs off) update t_replicate_volatile set a = random(); ERROR: could not devise a plan. (cdbpath.c:2441) +-- merge: volatile functions in MERGE actions are evaluated by ModifyTable, +-- not in the subplan targetlist, but must be rejected all the same +explain (costs off) merge into t_replicate_volatile using t_hashdist x on x.a = t_replicate_volatile.a when matched then update set b = random(); +ERROR: could not devise a plan. (cdbpath.c:2862) +explain (costs off) merge into t_replicate_volatile using t_hashdist x on x.a = t_replicate_volatile.a when not matched then insert values (x.a, random()); +ERROR: could not devise a plan. (cdbpath.c:2862) +explain (costs off) merge into t_replicate_volatile using t_hashdist x on x.a = t_replicate_volatile.a when matched and random() < 0.5 then delete; +ERROR: could not devise a plan. (cdbpath.c:2862) -- limit explain (costs off) insert into t_replicate_volatile select * from t_replicate_volatile limit random(); QUERY PLAN diff --git a/src/test/regress/extended_protocol_resqueue.c b/src/test/regress/extended_protocol_resqueue.c index 1932f28138e..c1e4b84d058 100644 --- a/src/test/regress/extended_protocol_resqueue.c +++ b/src/test/regress/extended_protocol_resqueue.c @@ -31,7 +31,6 @@ main(int argc, char **argv) const char *execrole; PGconn *conn; PGresult *res; - const char *paramValues[1]; char buf[80]; conninfo = argv[1]; diff --git a/src/test/regress/sql/gp_covering_index.sql b/src/test/regress/sql/gp_covering_index.sql index defdbb2a12d..b927da0e022 100644 --- a/src/test/regress/sql/gp_covering_index.sql +++ b/src/test/regress/sql/gp_covering_index.sql @@ -443,5 +443,58 @@ EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT b FROM test_combined_index_scan WHERE a < 42 OR b < 42; +-- Test predicates on INCLUDE-only columns (https://github.com/apache/cloudberry/issues/1948) +-- +-- A predicate that only references INCLUDE ("payload") columns must never be +-- turned into an index condition: the executor rejects index quals on non-key +-- attributes ("bogus index qualification"). Boolean column references and +-- IS [NOT] NULL tests used to slip through ORCA's index predicate extraction, +-- so cover them explicitly. +CREATE TABLE test_include_col_pred(c0 boolean, c1 boolean, c2 int) DISTRIBUTED BY (c2); +CREATE INDEX i_test_include_col_pred ON test_include_col_pred(c0) INCLUDE (c1); +INSERT INTO test_include_col_pred VALUES (true, true, 1), (false, true, 2), (true, false, 3), (true, NULL, 4); +VACUUM ANALYZE test_include_col_pred; + +-- KEYS: [c0] INCLUDED: [c1] +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE c1; +SELECT * FROM test_include_col_pred WHERE c1 ORDER BY c2; + +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE NOT c1; +SELECT * FROM test_include_col_pred WHERE NOT c1 ORDER BY c2; + +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE c1 IS NULL; +SELECT * FROM test_include_col_pred WHERE c1 IS NULL ORDER BY c2; + +EXPLAIN (COSTS OFF) +SELECT c1 FROM test_include_col_pred WHERE c1 IS NOT NULL; +SELECT c1 FROM test_include_col_pred WHERE c1 IS NOT NULL ORDER BY 1; + +-- key predicate combined with an INCLUDE-column predicate: only the key +-- predicate may become an index condition, the rest must stay a filter +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE c0 AND c1; +SELECT * FROM test_include_col_pred WHERE c0 AND c1 ORDER BY c2; + +-- When two indexes have the same keys, the bitmap candidate with fewer stored +-- columns must still win: INCLUDE columns make the index wider on disk even +-- though they can never be used in an index condition. i_bmidx_wide is created +-- first on purpose, so a tie on width would hand it the plan. +CREATE TABLE test_bitmap_include_width(a int, b int, payload text) DISTRIBUTED BY (a); +CREATE INDEX i_bmidx_wide ON test_bitmap_include_width(a) INCLUDE (payload); +CREATE INDEX i_bmidx_narrow ON test_bitmap_include_width(a); +CREATE INDEX i_bmidx_b ON test_bitmap_include_width(b); +INSERT INTO test_bitmap_include_width SELECT i, i, repeat('x', 100) FROM generate_series(1, 2000) i; +VACUUM ANALYZE test_bitmap_include_width; + +-- KEYS: [a] INCLUDED: [payload] +-- KEYS: [a] +-- KEYS: [b] +EXPLAIN (COSTS OFF) +SELECT * FROM test_bitmap_include_width WHERE a IN (1, 100, 1000) OR b = 17; + + reset optimizer_trace_fallback; reset enable_seqscan; diff --git a/src/test/regress/sql/gp_explain.sql b/src/test/regress/sql/gp_explain.sql index 2b5aaa0c722..0e014ac234b 100644 --- a/src/test/regress/sql/gp_explain.sql +++ b/src/test/regress/sql/gp_explain.sql @@ -45,6 +45,30 @@ begin end; $$ language plpgsql; +-- Run EXPLAIN ANALYZE BUFFERS, preserving the positions of buffer statistics +create or replace function explain_buffers_filter(explain_query text) returns setof text as +$$ +declare + explainrow text; +begin + for explainrow in execute + 'EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF, SUMMARY OFF) ' || explain_query + loop + if explainrow ~ '^\s*Buffers:' then + explainrow := regexp_replace(explainrow, 'Buffers:.*$', 'Buffers: N'); + else + explainrow := regexp_replace(explainrow, + '\m\d+([kK]B?)\M', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '\m\d+(x)', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '-?\m\d+\M', 'N', 'g'); + end if; + return next explainrow; + end loop; +end; +$$ language plpgsql; + -- Test explain wal option CREATE TABLE explainwal (id int4); @@ -94,6 +118,14 @@ CREATE TABLE explaintest (id int4); INSERT INTO explaintest SELECT generate_series(1, 10); ANALYZE explaintest; +-- Compare normalized buffer placement +-- explain_processing_off +SELECT plan_line +FROM explain_buffers_filter($$ + SELECT * FROM explaintest; +$$) WITH ORDINALITY AS plan(plan_line, line_number) +ORDER BY line_number; + EXPLAIN ANALYZE SELECT * FROM explaintest; set explain_memory_verbosity='summary'; diff --git a/src/test/regress/sql/memoize.sql b/src/test/regress/sql/memoize.sql index 683889d8814..2829a7b52f4 100644 --- a/src/test/regress/sql/memoize.sql +++ b/src/test/regress/sql/memoize.sql @@ -79,11 +79,16 @@ INSERT INTO expr_key SELECT * FROM expr_key; CREATE INDEX expr_key_idx_x_t ON expr_key (x, t); VACUUM ANALYZE expr_key; +-- ORCA may pick either an Index Scan or an Index Only Scan for the inner +-- side depending on how much of expr_key the VACUUM above could mark +-- all-visible, which varies with concurrently running tests. Pin the plan. +SET optimizer_enable_indexonlyscan TO off; -- Ensure we get we get a cache miss and hit for each of the 20 distinct values SELECT explain_memoize(' SELECT * FROM expr_key t1 INNER JOIN expr_key t2 ON t1.x = t2.t::numeric AND t1.t::numeric = t2.x;', false); +RESET optimizer_enable_indexonlyscan; DROP TABLE expr_key; -- Reduce work_mem and hash_mem_multiplier so that we see some cache evictions diff --git a/src/test/regress/sql/rpt.sql b/src/test/regress/sql/rpt.sql index 79a4a3db649..2375688920d 100644 --- a/src/test/regress/sql/rpt.sql +++ b/src/test/regress/sql/rpt.sql @@ -273,6 +273,26 @@ select * from foo; delete from foo where y = 1; select * from foo; +-- MERGE into a replicated table must broadcast the source to every segment, +-- like UPDATE/DELETE, otherwise each replica only sees the source rows stored +-- on its own segment and the replicas diverge. +create table rpt_merge_tgt(k int, v int) distributed replicated; +create table rpt_merge_src(k int, v int) distributed by (k); +insert into rpt_merge_tgt select g, 0 from generate_series(1, 10) g; +insert into rpt_merge_src select g, 1 from generate_series(6, 15) g; +explain (costs off) +merge into rpt_merge_tgt t using rpt_merge_src s on t.k = s.k + when matched then update set v = s.v + when not matched then insert values (s.k, s.v); +merge into rpt_merge_tgt t using rpt_merge_src s on t.k = s.k + when matched then update set v = s.v + when not matched then insert values (s.k, s.v); +select count(*), count(*) filter (where v = 1) as v_is_1 from rpt_merge_tgt; +-- every replica must hold the same 15 rows +select gp_execution_segment() as seg, count(*), count(*) filter (where v = 1) as v_is_1 +from gp_dist_random('rpt_merge_tgt') group by 1 order by 1; +drop table rpt_merge_tgt, rpt_merge_src; + -- Test replicate table within init plan insert into foo values (1, 1), (2, 1); select * from bar where exists (select * from foo); @@ -435,6 +455,12 @@ explain (costs off) delete from t_replicate_volatile where a < random(); explain (costs off) delete from t_replicate_volatile using t_replicate_volatile x where t_replicate_volatile.a + x.b < random(); explain (costs off) update t_replicate_volatile set a = random(); +-- merge: volatile functions in MERGE actions are evaluated by ModifyTable, +-- not in the subplan targetlist, but must be rejected all the same +explain (costs off) merge into t_replicate_volatile using t_hashdist x on x.a = t_replicate_volatile.a when matched then update set b = random(); +explain (costs off) merge into t_replicate_volatile using t_hashdist x on x.a = t_replicate_volatile.a when not matched then insert values (x.a, random()); +explain (costs off) merge into t_replicate_volatile using t_hashdist x on x.a = t_replicate_volatile.a when matched and random() < 0.5 then delete; + -- limit explain (costs off) insert into t_replicate_volatile select * from t_replicate_volatile limit random(); explain (costs off) select * from t_hashdist cross join (select * from t_replicate_volatile limit random()) x;