Fix Sampling reduction to avoid RandomTids problems

The fixed cases will not be common but do occur in tests.
This is an interim fix which should be reverted if CDB_randomTids changes.
This commit is contained in:
Javier Goizueta 2015-12-28 19:41:14 +01:00
parent 2ff686de27
commit 6a6a5bc96a

View File

@ -176,22 +176,27 @@ RETURNS REGCLASS
AS $$ AS $$
DECLARE DECLARE
overview_rel TEXT; overview_rel TEXT;
reduction FLOAT8; fraction FLOAT8;
base_name TEXT; base_name TEXT;
num_rows FLOAT8; class_info RECORD;
num_samples INTEGER; num_samples INTEGER;
BEGIN BEGIN
overview_rel := _CDB_Overview_Name(reloid, ref_z, overview_z); overview_rel := _CDB_Overview_Name(reloid, ref_z, overview_z);
reduction := power(2, 2*(overview_z - ref_z)); fraction := power(2, 2*(overview_z - ref_z));
EXECUTE Format('DROP TABLE IF EXISTS %s CASCADE;', overview_rel); EXECUTE Format('DROP TABLE IF EXISTS %s CASCADE;', overview_rel);
-- Estimate number of rows -- Estimate number of rows
SELECT reltuples FROM pg_class INTO STRICT num_rows SELECT reltuples, relpages FROM pg_class INTO STRICT class_info
WHERE oid = reloid::oid; WHERE oid = reloid::oid;
num_samples := ceil(num_rows*reduction); IF class_info.relpages < 2 OR fraction > 0.5 THEN
-- We'll avoid possible CDB_RandomTids problems
EXECUTE Format('
CREATE TABLE %s AS SELECT * FROM %s WHERE random() < %s;
', overview_rel, reloid, fraction);
ELSE
num_samples := ceil(class_info.reltuples*fraction);
EXECUTE Format(' EXECUTE Format('
CREATE TABLE %1$s AS SELECT * FROM %2$s CREATE TABLE %1$s AS SELECT * FROM %2$s
WHERE ctid = ANY ( WHERE ctid = ANY (
@ -200,6 +205,7 @@ AS $$
] ]
); );
', overview_rel, reloid, num_samples); ', overview_rel, reloid, num_samples);
END IF;
RETURN overview_rel; RETURN overview_rel;
END; END;
$$ LANGUAGE PLPGSQL; $$ LANGUAGE PLPGSQL;