From 39a96512b3ed72de7b24b2667d9575d7e9fcb326 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Wed, 11 Jul 2018 18:47:31 -0400 Subject: [PATCH] Mark built-in btree comparison functions as leakproof where it's safe. Generally, if the comparison operators for a datatype or pair of datatypes are leakproof, the corresponding btree comparison support function can be considered so as well. But we had not originally worried about marking support functions as leakproof, reasoning that they'd not likely be used in queries so the marking wouldn't matter. It turns out there's at least one place where it does matter: calc_arraycontsel() finds the target datatype's default btree comparison function and tries to use that to estimate selectivity, but it will be blocked in some cases if the function isn't leakproof. This leads to unnecessarily poor selectivity estimates and bad plans, as seen in bug #15251. Hence, run around and apply proleakproof markings where the corresponding btree comparison operators are leakproof. (I did eyeball each function to verify that it wasn't doing anything surprising, too.) This isn't a full solution to bug #15251, and it's not back-patchable because of the need for a catversion bump. A more useful response probably is to consider whether we can check permissions on the parent table instead of the child. However, this change will help in some cases where that won't, and it's easy enough to do in HEAD, so let's do so. Discussion: https://postgr.es/m/3876.1531261875@sss.pgh.pa.us --- src/include/catalog/catversion.h | 2 +- src/include/catalog/pg_proc.dat | 116 +++++++++++------------ src/test/regress/expected/opr_sanity.out | 35 +++++++ 3 files changed, 94 insertions(+), 59 deletions(-) diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h index 505e32109a..66f91a29fc 100644 --- a/src/include/catalog/catversion.h +++ b/src/include/catalog/catversion.h @@ -53,6 +53,6 @@ */ /* yyyymmddN */ -#define CATALOG_VERSION_NO 201806231 +#define CATALOG_VERSION_NO 201807111 #endif diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 40d54ed030..00b59fd6a9 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -1004,38 +1004,38 @@ prosrc => 'poly_out' }, { oid => '350', descr => 'less-equal-greater', - proname => 'btint2cmp', prorettype => 'int4', proargtypes => 'int2 int2', - prosrc => 'btint2cmp' }, + proname => 'btint2cmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'int2 int2', prosrc => 'btint2cmp' }, { oid => '3129', descr => 'sort support', proname => 'btint2sortsupport', prorettype => 'void', proargtypes => 'internal', prosrc => 'btint2sortsupport' }, { oid => '351', descr => 'less-equal-greater', - proname => 'btint4cmp', prorettype => 'int4', proargtypes => 'int4 int4', - prosrc => 'btint4cmp' }, + proname => 'btint4cmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'int4 int4', prosrc => 'btint4cmp' }, { oid => '3130', descr => 'sort support', proname => 'btint4sortsupport', prorettype => 'void', proargtypes => 'internal', prosrc => 'btint4sortsupport' }, { oid => '842', descr => 'less-equal-greater', - proname => 'btint8cmp', prorettype => 'int4', proargtypes => 'int8 int8', - prosrc => 'btint8cmp' }, + proname => 'btint8cmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'int8 int8', prosrc => 'btint8cmp' }, { oid => '3131', descr => 'sort support', proname => 'btint8sortsupport', prorettype => 'void', proargtypes => 'internal', prosrc => 'btint8sortsupport' }, { oid => '354', descr => 'less-equal-greater', - proname => 'btfloat4cmp', prorettype => 'int4', + proname => 'btfloat4cmp', proleakproof => 't', prorettype => 'int4', proargtypes => 'float4 float4', prosrc => 'btfloat4cmp' }, { oid => '3132', descr => 'sort support', proname => 'btfloat4sortsupport', prorettype => 'void', proargtypes => 'internal', prosrc => 'btfloat4sortsupport' }, { oid => '355', descr => 'less-equal-greater', - proname => 'btfloat8cmp', prorettype => 'int4', + proname => 'btfloat8cmp', proleakproof => 't', prorettype => 'int4', proargtypes => 'float8 float8', prosrc => 'btfloat8cmp' }, { oid => '3133', descr => 'sort support', proname => 'btfloat8sortsupport', prorettype => 'void', proargtypes => 'internal', prosrc => 'btfloat8sortsupport' }, { oid => '356', descr => 'less-equal-greater', - proname => 'btoidcmp', prorettype => 'int4', proargtypes => 'oid oid', - prosrc => 'btoidcmp' }, + proname => 'btoidcmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'oid oid', prosrc => 'btoidcmp' }, { oid => '3134', descr => 'sort support', proname => 'btoidsortsupport', prorettype => 'void', proargtypes => 'internal', prosrc => 'btoidsortsupport' }, @@ -1043,14 +1043,14 @@ proname => 'btoidvectorcmp', prorettype => 'int4', proargtypes => 'oidvector oidvector', prosrc => 'btoidvectorcmp' }, { oid => '357', descr => 'less-equal-greater', - proname => 'btabstimecmp', prorettype => 'int4', + proname => 'btabstimecmp', proleakproof => 't', prorettype => 'int4', proargtypes => 'abstime abstime', prosrc => 'btabstimecmp' }, { oid => '358', descr => 'less-equal-greater', - proname => 'btcharcmp', prorettype => 'int4', proargtypes => 'char char', - prosrc => 'btcharcmp' }, + proname => 'btcharcmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'char char', prosrc => 'btcharcmp' }, { oid => '359', descr => 'less-equal-greater', - proname => 'btnamecmp', prorettype => 'int4', proargtypes => 'name name', - prosrc => 'btnamecmp' }, + proname => 'btnamecmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'name name', prosrc => 'btnamecmp' }, { oid => '3135', descr => 'sort support', proname => 'btnamesortsupport', prorettype => 'void', proargtypes => 'internal', prosrc => 'btnamesortsupport' }, @@ -1061,13 +1061,13 @@ proname => 'bttextsortsupport', prorettype => 'void', proargtypes => 'internal', prosrc => 'bttextsortsupport' }, { oid => '377', descr => 'less-equal-greater', - proname => 'cash_cmp', prorettype => 'int4', proargtypes => 'money money', - prosrc => 'cash_cmp' }, + proname => 'cash_cmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'money money', prosrc => 'cash_cmp' }, { oid => '380', descr => 'less-equal-greater', - proname => 'btreltimecmp', prorettype => 'int4', + proname => 'btreltimecmp', proleakproof => 't', prorettype => 'int4', proargtypes => 'reltime reltime', prosrc => 'btreltimecmp' }, { oid => '381', descr => 'less-equal-greater', - proname => 'bttintervalcmp', prorettype => 'int4', + proname => 'bttintervalcmp', proleakproof => 't', prorettype => 'int4', proargtypes => 'tinterval tinterval', prosrc => 'bttintervalcmp' }, { oid => '382', descr => 'less-equal-greater', proname => 'btarraycmp', prorettype => 'int4', @@ -2172,8 +2172,8 @@ proname => 'date_ne', proleakproof => 't', prorettype => 'bool', proargtypes => 'date date', prosrc => 'date_ne' }, { oid => '1092', descr => 'less-equal-greater', - proname => 'date_cmp', prorettype => 'int4', proargtypes => 'date date', - prosrc => 'date_cmp' }, + proname => 'date_cmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'date date', prosrc => 'date_cmp' }, { oid => '3136', descr => 'sort support', proname => 'date_sortsupport', prorettype => 'void', proargtypes => 'internal', prosrc => 'date_sortsupport' }, @@ -2200,8 +2200,8 @@ proname => 'time_ne', proleakproof => 't', prorettype => 'bool', proargtypes => 'time time', prosrc => 'time_ne' }, { oid => '1107', descr => 'less-equal-greater', - proname => 'time_cmp', prorettype => 'int4', proargtypes => 'time time', - prosrc => 'time_cmp' }, + proname => 'time_cmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'time time', prosrc => 'time_cmp' }, { oid => '1138', descr => 'larger of two', proname => 'date_larger', prorettype => 'date', proargtypes => 'date date', prosrc => 'date_larger' }, @@ -2576,8 +2576,8 @@ proname => 'tidle', proleakproof => 't', prorettype => 'bool', proargtypes => 'tid tid', prosrc => 'tidle' }, { oid => '2794', descr => 'less-equal-greater', - proname => 'bttidcmp', prorettype => 'int4', proargtypes => 'tid tid', - prosrc => 'bttidcmp' }, + proname => 'bttidcmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'tid tid', prosrc => 'bttidcmp' }, { oid => '2795', descr => 'larger of two', proname => 'tidlarger', prorettype => 'tid', proargtypes => 'tid tid', prosrc => 'tidlarger' }, @@ -2678,10 +2678,10 @@ proname => 'timestamptypmodout', prorettype => 'cstring', proargtypes => 'int4', prosrc => 'timestamptypmodout' }, { oid => '1314', descr => 'less-equal-greater', - proname => 'timestamptz_cmp', prorettype => 'int4', + proname => 'timestamptz_cmp', proleakproof => 't', prorettype => 'int4', proargtypes => 'timestamptz timestamptz', prosrc => 'timestamp_cmp' }, { oid => '1315', descr => 'less-equal-greater', - proname => 'interval_cmp', prorettype => 'int4', + proname => 'interval_cmp', proleakproof => 't', prorettype => 'int4', proargtypes => 'interval interval', prosrc => 'interval_cmp' }, { oid => '1316', descr => 'convert timestamp to time', proname => 'time', prorettype => 'time', proargtypes => 'timestamp', @@ -2778,8 +2778,8 @@ proname => 'timetz_gt', proleakproof => 't', prorettype => 'bool', proargtypes => 'timetz timetz', prosrc => 'timetz_gt' }, { oid => '1358', descr => 'less-equal-greater', - proname => 'timetz_cmp', prorettype => 'int4', proargtypes => 'timetz timetz', - prosrc => 'timetz_cmp' }, + proname => 'timetz_cmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'timetz timetz', prosrc => 'timetz_cmp' }, { oid => '1359', descr => 'convert date and time with time zone to timestamp with time zone', proname => 'timestamptz', prorettype => 'timestamptz', @@ -3317,8 +3317,8 @@ proname => 'bitlt', proleakproof => 't', prorettype => 'bool', proargtypes => 'bit bit', prosrc => 'bitlt' }, { oid => '1596', descr => 'less-equal-greater', - proname => 'bitcmp', prorettype => 'int4', proargtypes => 'bit bit', - prosrc => 'bitcmp' }, + proname => 'bitcmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'bit bit', prosrc => 'bitcmp' }, { oid => '1598', descr => 'random value', proname => 'random', provolatile => 'v', proparallel => 'r', @@ -3774,8 +3774,8 @@ proname => 'varbitlt', proleakproof => 't', prorettype => 'bool', proargtypes => 'varbit varbit', prosrc => 'bitlt' }, { oid => '1672', descr => 'less-equal-greater', - proname => 'varbitcmp', prorettype => 'int4', proargtypes => 'varbit varbit', - prosrc => 'bitcmp' }, + proname => 'varbitcmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'varbit varbit', prosrc => 'bitcmp' }, # avoid the C names bitand and bitor, since they are C++ keywords { oid => '1673', @@ -3877,7 +3877,7 @@ proname => 'macaddr_ne', proleakproof => 't', prorettype => 'bool', proargtypes => 'macaddr macaddr', prosrc => 'macaddr_ne' }, { oid => '836', descr => 'less-equal-greater', - proname => 'macaddr_cmp', prorettype => 'int4', + proname => 'macaddr_cmp', proleakproof => 't', prorettype => 'int4', proargtypes => 'macaddr macaddr', prosrc => 'macaddr_cmp' }, { oid => '3144', proname => 'macaddr_not', prorettype => 'macaddr', proargtypes => 'macaddr', @@ -3923,7 +3923,7 @@ proname => 'macaddr8_ne', proleakproof => 't', prorettype => 'bool', proargtypes => 'macaddr8 macaddr8', prosrc => 'macaddr8_ne' }, { oid => '4119', descr => 'less-equal-greater', - proname => 'macaddr8_cmp', prorettype => 'int4', + proname => 'macaddr8_cmp', proleakproof => 't', prorettype => 'int4', proargtypes => 'macaddr8 macaddr8', prosrc => 'macaddr8_cmp' }, { oid => '4120', proname => 'macaddr8_not', prorettype => 'macaddr8', @@ -3986,8 +3986,8 @@ proname => 'network_smaller', prorettype => 'inet', proargtypes => 'inet inet', prosrc => 'network_smaller' }, { oid => '926', descr => 'less-equal-greater', - proname => 'network_cmp', prorettype => 'int4', proargtypes => 'inet inet', - prosrc => 'network_cmp' }, + proname => 'network_cmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'inet inet', prosrc => 'network_cmp' }, { oid => '927', proname => 'network_sub', prorettype => 'bool', proargtypes => 'inet inet', prosrc => 'network_sub' }, @@ -4150,8 +4150,8 @@ proname => 'boolge', proleakproof => 't', prorettype => 'bool', proargtypes => 'bool bool', prosrc => 'boolge' }, { oid => '1693', descr => 'less-equal-greater', - proname => 'btboolcmp', prorettype => 'int4', proargtypes => 'bool bool', - prosrc => 'btboolcmp' }, + proname => 'btboolcmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'bool bool', prosrc => 'btboolcmp' }, { oid => '1688', descr => 'hash', proname => 'time_hash', prorettype => 'int4', proargtypes => 'time', @@ -5547,8 +5547,8 @@ proname => 'byteane', proleakproof => 't', prorettype => 'bool', proargtypes => 'bytea bytea', prosrc => 'byteane' }, { oid => '1954', descr => 'less-equal-greater', - proname => 'byteacmp', prorettype => 'int4', proargtypes => 'bytea bytea', - prosrc => 'byteacmp' }, + proname => 'byteacmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'bytea bytea', prosrc => 'byteacmp' }, { oid => '3331', descr => 'sort support', proname => 'bytea_sortsupport', prorettype => 'void', proargtypes => 'internal', prosrc => 'bytea_sortsupport' }, @@ -5706,7 +5706,7 @@ prorettype => 'bool', proargtypes => 'timestamp interval timestamp timestamp', prosrc => 'select ($1, ($1 + $2)) overlaps ($3, $4)' }, { oid => '2045', descr => 'less-equal-greater', - proname => 'timestamp_cmp', prorettype => 'int4', + proname => 'timestamp_cmp', proleakproof => 't', prorettype => 'int4', proargtypes => 'timestamp timestamp', prosrc => 'timestamp_cmp' }, { oid => '3137', descr => 'sort support', proname => 'timestamp_sortsupport', prorettype => 'void', @@ -6588,28 +6588,28 @@ proargtypes => 'internal', prosrc => 'btbpchar_pattern_sortsupport' }, { oid => '2188', descr => 'less-equal-greater', - proname => 'btint48cmp', prorettype => 'int4', proargtypes => 'int4 int8', - prosrc => 'btint48cmp' }, + proname => 'btint48cmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'int4 int8', prosrc => 'btint48cmp' }, { oid => '2189', descr => 'less-equal-greater', - proname => 'btint84cmp', prorettype => 'int4', proargtypes => 'int8 int4', - prosrc => 'btint84cmp' }, + proname => 'btint84cmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'int8 int4', prosrc => 'btint84cmp' }, { oid => '2190', descr => 'less-equal-greater', - proname => 'btint24cmp', prorettype => 'int4', proargtypes => 'int2 int4', - prosrc => 'btint24cmp' }, + proname => 'btint24cmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'int2 int4', prosrc => 'btint24cmp' }, { oid => '2191', descr => 'less-equal-greater', - proname => 'btint42cmp', prorettype => 'int4', proargtypes => 'int4 int2', - prosrc => 'btint42cmp' }, + proname => 'btint42cmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'int4 int2', prosrc => 'btint42cmp' }, { oid => '2192', descr => 'less-equal-greater', - proname => 'btint28cmp', prorettype => 'int4', proargtypes => 'int2 int8', - prosrc => 'btint28cmp' }, + proname => 'btint28cmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'int2 int8', prosrc => 'btint28cmp' }, { oid => '2193', descr => 'less-equal-greater', - proname => 'btint82cmp', prorettype => 'int4', proargtypes => 'int8 int2', - prosrc => 'btint82cmp' }, + proname => 'btint82cmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'int8 int2', prosrc => 'btint82cmp' }, { oid => '2194', descr => 'less-equal-greater', - proname => 'btfloat48cmp', prorettype => 'int4', + proname => 'btfloat48cmp', proleakproof => 't', prorettype => 'int4', proargtypes => 'float4 float8', prosrc => 'btfloat48cmp' }, { oid => '2195', descr => 'less-equal-greater', - proname => 'btfloat84cmp', prorettype => 'int4', + proname => 'btfloat84cmp', proleakproof => 't', prorettype => 'int4', proargtypes => 'float8 float4', prosrc => 'btfloat84cmp' }, { oid => '2212', descr => 'I/O', @@ -8374,8 +8374,8 @@ proname => 'uuid_ne', proleakproof => 't', prorettype => 'bool', proargtypes => 'uuid uuid', prosrc => 'uuid_ne' }, { oid => '2960', descr => 'less-equal-greater', - proname => 'uuid_cmp', prorettype => 'int4', proargtypes => 'uuid uuid', - prosrc => 'uuid_cmp' }, + proname => 'uuid_cmp', proleakproof => 't', prorettype => 'int4', + proargtypes => 'uuid uuid', prosrc => 'uuid_cmp' }, { oid => '3300', descr => 'sort support', proname => 'uuid_sortsupport', prorettype => 'void', proargtypes => 'internal', prosrc => 'uuid_sortsupport' }, diff --git a/src/test/regress/expected/opr_sanity.out b/src/test/regress/expected/opr_sanity.out index a1e18a6ceb..3c6d853ffb 100644 --- a/src/test/regress/expected/opr_sanity.out +++ b/src/test/regress/expected/opr_sanity.out @@ -563,6 +563,17 @@ float84lt(double precision,real) float84le(double precision,real) float84gt(double precision,real) float84ge(double precision,real) +btint2cmp(smallint,smallint) +btint4cmp(integer,integer) +btfloat4cmp(real,real) +btfloat8cmp(double precision,double precision) +btoidcmp(oid,oid) +btabstimecmp(abstime,abstime) +btcharcmp("char","char") +btnamecmp(name,name) +cash_cmp(money,money) +btreltimecmp(reltime,reltime) +bttintervalcmp(tinterval,tinterval) int8eq(bigint,bigint) int8ne(bigint,bigint) int8lt(bigint,bigint) @@ -594,6 +605,8 @@ macaddr_le(macaddr,macaddr) macaddr_gt(macaddr,macaddr) macaddr_ge(macaddr,macaddr) macaddr_ne(macaddr,macaddr) +macaddr_cmp(macaddr,macaddr) +btint8cmp(bigint,bigint) int48eq(integer,bigint) int48ne(integer,bigint) int48lt(integer,bigint) @@ -612,6 +625,7 @@ network_le(inet,inet) network_gt(inet,inet) network_ge(inet,inet) network_ne(inet,inet) +network_cmp(inet,inet) lseg_eq(lseg,lseg) bpchareq(character,character) bpcharne(character,character) @@ -621,11 +635,13 @@ date_le(date,date) date_gt(date,date) date_ge(date,date) date_ne(date,date) +date_cmp(date,date) time_lt(time without time zone,time without time zone) time_le(time without time zone,time without time zone) time_gt(time without time zone,time without time zone) time_ge(time without time zone,time without time zone) time_ne(time without time zone,time without time zone) +time_cmp(time without time zone,time without time zone) time_eq(time without time zone,time without time zone) timestamptz_eq(timestamp with time zone,timestamp with time zone) timestamptz_ne(timestamp with time zone,timestamp with time zone) @@ -642,6 +658,8 @@ interval_gt(interval,interval) charlt("char","char") tidne(tid,tid) tideq(tid,tid) +timestamptz_cmp(timestamp with time zone,timestamp with time zone) +interval_cmp(interval,interval) xideqint4(xid,integer) timetz_eq(time with time zone,time with time zone) timetz_ne(time with time zone,time with time zone) @@ -649,6 +667,7 @@ timetz_lt(time with time zone,time with time zone) timetz_le(time with time zone,time with time zone) timetz_ge(time with time zone,time with time zone) timetz_gt(time with time zone,time with time zone) +timetz_cmp(time with time zone,time with time zone) circle_eq(circle,circle) circle_ne(circle,circle) circle_lt(circle,circle) @@ -666,6 +685,7 @@ bitge(bit,bit) bitgt(bit,bit) bitle(bit,bit) bitlt(bit,bit) +bitcmp(bit,bit) oidgt(oid,oid) oidge(oid,oid) varbiteq(bit varying,bit varying) @@ -674,8 +694,10 @@ varbitge(bit varying,bit varying) varbitgt(bit varying,bit varying) varbitle(bit varying,bit varying) varbitlt(bit varying,bit varying) +varbitcmp(bit varying,bit varying) boolle(boolean,boolean) boolge(boolean,boolean) +btboolcmp(boolean,boolean) int28eq(smallint,bigint) int28ne(smallint,bigint) int28lt(smallint,bigint) @@ -694,24 +716,36 @@ byteale(bytea,bytea) byteagt(bytea,bytea) byteage(bytea,bytea) byteane(bytea,bytea) +byteacmp(bytea,bytea) +timestamp_cmp(timestamp without time zone,timestamp without time zone) timestamp_eq(timestamp without time zone,timestamp without time zone) timestamp_ne(timestamp without time zone,timestamp without time zone) timestamp_lt(timestamp without time zone,timestamp without time zone) timestamp_le(timestamp without time zone,timestamp without time zone) timestamp_ge(timestamp without time zone,timestamp without time zone) timestamp_gt(timestamp without time zone,timestamp without time zone) +btint48cmp(integer,bigint) +btint84cmp(bigint,integer) +btint24cmp(smallint,integer) +btint42cmp(integer,smallint) +btint28cmp(smallint,bigint) +btint82cmp(bigint,smallint) +btfloat48cmp(real,double precision) +btfloat84cmp(double precision,real) md5(text) md5(bytea) tidgt(tid,tid) tidlt(tid,tid) tidge(tid,tid) tidle(tid,tid) +bttidcmp(tid,tid) uuid_lt(uuid,uuid) uuid_le(uuid,uuid) uuid_eq(uuid,uuid) uuid_ge(uuid,uuid) uuid_gt(uuid,uuid) uuid_ne(uuid,uuid) +uuid_cmp(uuid,uuid) xidneq(xid,xid) xidneqint4(xid,integer) sha224(bytea) @@ -725,6 +759,7 @@ macaddr8_le(macaddr8,macaddr8) macaddr8_gt(macaddr8,macaddr8) macaddr8_ge(macaddr8,macaddr8) macaddr8_ne(macaddr8,macaddr8) +macaddr8_cmp(macaddr8,macaddr8) -- restore normal output mode \a\t -- List of functions used by libpq's fe-lobj.c -- 2.40.0