pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedIndexes on expressions that include user-defined functions are not reindexed when the function definition is changed
4+ messages / 3 participants
[nested] [flat]
* Indexes on expressions that include user-defined functions are not reindexed when the function definition is changed
@ 2019-05-29 17:00 Dmytry Strikha <dm.strikha@gmail.com>
2019-05-29 17:25 ` Re: Indexes on expressions that include user-defined functions are not reindexed when the function definition is changed Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 4+ messages in thread
From: Dmytry Strikha @ 2019-05-29 17:00 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org
If the index on expression is defined so, that the indexed value
depends on a user-defined function, it doesn't get reindexed when the
function's definition changes, therefore completely wrong values can
be displayed when the query is executed using that index.
MWE:
psql (11.3)
Type "help" for help.
postgres=# create database bugtest;
CREATE DATABASE
postgres=# \c bugtest
You are now connected to database "bugtest" as user "postgres".
bugtest=# create function f(x int) returns int as select($1 * 2) LANGUAGE SQL;
ERROR: syntax error at or near "select"
LINE 1: create function f(x int) returns int as select($1 * 2) LANGU...
^
bugtest=# create function f(x int) returns int as $$select($1 * 2)$$
LANGUAGE SQL;
CREATE FUNCTION
bugtest=# create table tbl (id serial primary key, n int);
CREATE TABLE
bugtest=# create index idx on tbl(f(n));
CREATE INDEX
bugtest=# insert into tbl(n) (select * from generate_series(1,1000));
INSERT 0 1000
bugtest=# create or replace function f(x int) returns int as
$$select($1 * 3)$$ LANGUAGE SQL;
CREATE FUNCTION
bugtest=# select * from tbl where f(n) = 12;
id | n
----+---
6 | 6
(1 row)
bugtest=# select id,n,f(n) from tbl where f(n) = 12;
id | n | f
----+---+----
6 | 6 | 18
(1 row)
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Indexes on expressions that include user-defined functions are not reindexed when the function definition is changed
2019-05-29 17:00 Indexes on expressions that include user-defined functions are not reindexed when the function definition is changed Dmytry Strikha <dm.strikha@gmail.com>
@ 2019-05-29 17:25 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-05-29 17:36 ` Re: Indexes on expressions that include user-defined functions are not reindexed when the function definition is changed Dmytry Strikha <dm.strikha@gmail.com>
0 siblings, 1 reply; 4+ messages in thread
From: Tom Lane @ 2019-05-29 17:25 UTC (permalink / raw)
To: Dmytry Strikha <dm.strikha@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org
Dmytry Strikha <dm.strikha@gmail.com> writes:
> If the index on expression is defined so, that the indexed value
> depends on a user-defined function, it doesn't get reindexed when the
> function's definition changes, therefore completely wrong values can
> be displayed when the query is executed using that index.
If you declare the function immutable, as you must to use it in an
index, then redefining the function in a way that changes its results
means *you* broke the contract. I don't think it's unreasonable of
us to insist that it's on your head to issue any necessary reindexing.
Certainly, neither of the plausible alternatives are acceptable:
* Reindex any time the function is changed at all. This is putting
an undue burden on people who did follow the contract.
* Detecting whether the redefinition changed the results.
See halting problem.
regards, tom lane
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Indexes on expressions that include user-defined functions are not reindexed when the function definition is changed
2019-05-29 17:00 Indexes on expressions that include user-defined functions are not reindexed when the function definition is changed Dmytry Strikha <dm.strikha@gmail.com>
2019-05-29 17:25 ` Re: Indexes on expressions that include user-defined functions are not reindexed when the function definition is changed Tom Lane <tgl@sss.pgh.pa.us>
@ 2019-05-29 17:36 ` Dmytry Strikha <dm.strikha@gmail.com>
2019-06-05 18:36 ` Re: Indexes on expressions that include user-defined functions are not reindexed when the function definition is changed Alvaro Herrera <alvherre@2ndquadrant.com>
0 siblings, 1 reply; 4+ messages in thread
From: Dmytry Strikha @ 2019-05-29 17:36 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Dmytry Strikha <dm.strikha@gmail.com>; pgsql-bugs@lists.postgresql.org
Could this get a special mention in the documentation for Indexes on
expressions? Most unfamiliar users will look there to figure out the
correct way to create the index and will be at least warned.
On Wed, May 29, 2019 at 7:25 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> Dmytry Strikha <dm.strikha@gmail.com> writes:
> > If the index on expression is defined so, that the indexed value
> > depends on a user-defined function, it doesn't get reindexed when the
> > function's definition changes, therefore completely wrong values can
> > be displayed when the query is executed using that index.
>
> If you declare the function immutable, as you must to use it in an
> index, then redefining the function in a way that changes its results
> means *you* broke the contract. I don't think it's unreasonable of
> us to insist that it's on your head to issue any necessary reindexing.
> Certainly, neither of the plausible alternatives are acceptable:
>
> * Reindex any time the function is changed at all. This is putting
> an undue burden on people who did follow the contract.
>
> * Detecting whether the redefinition changed the results.
> See halting problem.
>
> regards, tom lane
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Indexes on expressions that include user-defined functions are not reindexed when the function definition is changed
2019-05-29 17:00 Indexes on expressions that include user-defined functions are not reindexed when the function definition is changed Dmytry Strikha <dm.strikha@gmail.com>
2019-05-29 17:25 ` Re: Indexes on expressions that include user-defined functions are not reindexed when the function definition is changed Tom Lane <tgl@sss.pgh.pa.us>
2019-05-29 17:36 ` Re: Indexes on expressions that include user-defined functions are not reindexed when the function definition is changed Dmytry Strikha <dm.strikha@gmail.com>
@ 2019-06-05 18:36 ` Alvaro Herrera <alvherre@2ndquadrant.com>
0 siblings, 0 replies; 4+ messages in thread
From: Alvaro Herrera @ 2019-06-05 18:36 UTC (permalink / raw)
To: Dmytry Strikha <dm.strikha@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-bugs@lists.postgresql.org
On 2019-May-29, Dmytry Strikha wrote:
> Could this get a special mention in the documentation for Indexes on
> expressions? Most unfamiliar users will look there to figure out the
> correct way to create the index and will be at least warned.
That sounds reasonable. Care to submit a patch?
--
Álvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2019-06-05 18:36 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-05-29 17:00 Indexes on expressions that include user-defined functions are not reindexed when the function definition is changed Dmytry Strikha <dm.strikha@gmail.com>
2019-05-29 17:25 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-05-29 17:36 ` Dmytry Strikha <dm.strikha@gmail.com>
2019-06-05 18:36 ` Alvaro Herrera <alvherre@2ndquadrant.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox