Re: Functions used in index definitions shouldn't be changed

Lists: pgsql-hackers
From: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Functions used in index definitions shouldn't be changed
Date: 2014-11-19 14:38:05
Message-ID: A737B7A37273E048B164557ADEF4A58B17D9FF61@ntex2010a.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Currently it is possible to change the behaviour of a function with
CREATE OR REPLACE FUNCTION even if the function is part of an index definition.

I think that should be forbidden, because it is very likely to corrupt
the index. I expect the objection that this would break valid use cases
where people know exactly what they are doing, but I believe that this
is a footgun for inexperienced users that should be disarmed.

I'd also opt for forbidding behaviour changing modifications with ALTER FUNCTION
for functions used in index definitions, specifically altering strictness.

Attached is a patch implementing a fix.

Yours,
Laurenz Albe

Attachment Content-Type Size
alter_index_function.patch application/octet-stream 4.0 KB

From: Marko Tiikkaja <marko(at)joh(dot)to>
To: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Functions used in index definitions shouldn't be changed
Date: 2014-11-19 14:46:13
Message-ID: 546CAD35.7030503@joh.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 11/19/14 3:38 PM, Albe Laurenz wrote:
> I think that should be forbidden, because it is very likely to corrupt
> the index. I expect the objection that this would break valid use cases
> where people know exactly what they are doing, but I believe that this
> is a footgun for inexperienced users that should be disarmed.

Yes, I believe that objection to be completely valid. I can't begin to
describe how frustrating it is to be in the position where my hammer is
made out of plastic because someone might hurt themself if it was a real
one.

I'd at least like to see a workaround for the "less inexperienced" users.

.marko


From: Antonin Houska <ah(at)cybertec(dot)at>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Functions used in index definitions shouldn't be changed
Date: 2014-11-19 15:21:29
Message-ID: 20449.1416410489@localhost
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at> wrote:
> Currently it is possible to change the behaviour of a function with
> CREATE OR REPLACE FUNCTION even if the function is part of an index definition.
>
> I think that should be forbidden, because it is very likely to corrupt
> the index. I expect the objection that this would break valid use cases
> where people know exactly what they are doing, but I believe that this
> is a footgun for inexperienced users that should be disarmed.
>
> I'd also opt for forbidding behaviour changing modifications with ALTER FUNCTION
> for functions used in index definitions, specifically altering strictness.
>
> Attached is a patch implementing a fix.

Instead of adding extra check, shouldn't you just ensure that
DEPENDENCY_INTERNAL is the dependency type and let the existing logic do the work?

--
Antonin Houska
Cybertec Schönig & Schönig GmbH
Gröhrmühlgasse 26
A-2700 Wiener Neustadt
Web: http://www.postgresql-support.de, http://www.cybertec.at


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Antonin Houska <ah(at)cybertec(dot)at>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Functions used in index definitions shouldn't be changed
Date: 2014-11-19 16:23:47
Message-ID: 27727.1416414227@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Antonin Houska <ah(at)cybertec(dot)at> writes:
> Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at> wrote:
>> Currently it is possible to change the behaviour of a function with
>> CREATE OR REPLACE FUNCTION even if the function is part of an index definition.
>>
>> I think that should be forbidden, because it is very likely to corrupt
>> the index. I expect the objection that this would break valid use cases
>> where people know exactly what they are doing, but I believe that this
>> is a footgun for inexperienced users that should be disarmed.
>>
>> I'd also opt for forbidding behaviour changing modifications with ALTER FUNCTION
>> for functions used in index definitions, specifically altering strictness.
>>
>> Attached is a patch implementing a fix.

> Instead of adding extra check, shouldn't you just ensure that
> DEPENDENCY_INTERNAL is the dependency type and let the existing logic do the work?

The dependency logic only prohibits DROP, not ALTER, so changing the
dependency type wouldn't be enough to accomplish that. But I dislike the
entire concept, so implementation details are not that interesting.

A comparable issue is that alteration of text search configurations may
partially invalidate precomputed tsvector columns and indexes based on
tsvector data. We discussed whether we should prohibit that somehow
and explicitly decided that it would be a bad idea. In the text search
case, changes like adding stop words are common and don't meaningfully
invalidate indexes. In some cases you may decide that you need to
recompute the tsvector data, but that decision should be left to the
user.

I think that pretty much the same thing applies to functions used in
indexes. There are too many use-cases where alterations don't
meaningfully impact the stored data for us to get away with a blanket
prohibition. (I'm pretty sure that this has been discussed in the
past, too. If Albe really wants to push the point he should look
up the previous discussions and explain why the decision was wrong.)

regards, tom lane


From: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: "Tom Lane *EXTERN*" <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Antonin Houska <ah(at)cybertec(dot)at>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Functions used in index definitions shouldn't be changed
Date: 2014-11-20 18:56:58
Message-ID: A737B7A37273E048B164557ADEF4A58B17DA24CD@ntex2010a.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Tom Lane wrote:
> Antonin Houska <ah(at)cybertec(dot)at> writes:
>> Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at> wrote:
>>> Currently it is possible to change the behaviour of a function with
>>> CREATE OR REPLACE FUNCTION even if the function is part of an index definition.
>>>
>>> I think that should be forbidden, because it is very likely to corrupt
>>> the index. I expect the objection that this would break valid use cases
>>> where people know exactly what they are doing, but I believe that this
>>> is a footgun for inexperienced users that should be disarmed.
>>>
>>> I'd also opt for forbidding behaviour changing modifications with ALTER FUNCTION
>>> for functions used in index definitions, specifically altering strictness.
>>>
>>> Attached is a patch implementing a fix.

> A comparable issue is that alteration of text search configurations may
> partially invalidate precomputed tsvector columns and indexes based on
> tsvector data. We discussed whether we should prohibit that somehow
> and explicitly decided that it would be a bad idea. In the text search
> case, changes like adding stop words are common and don't meaningfully
> invalidate indexes. In some cases you may decide that you need to
> recompute the tsvector data, but that decision should be left to the
> user.
>
> I think that pretty much the same thing applies to functions used in
> indexes. There are too many use-cases where alterations don't
> meaningfully impact the stored data for us to get away with a blanket
> prohibition. (I'm pretty sure that this has been discussed in the
> past, too. If Albe really wants to push the point he should look
> up the previous discussions and explain why the decision was wrong.)

I don't think that there is a universally compelling right or wrong to
questions like this, it is more a matter of taste. Is it more important to protect
the casual DBA from hurting himself or herself, or is it more important to
provide a well honed scalpel for the experienced surgeon?

It seems that more people lean in the latter direction, so I'll cease and desist.

Yours,
Laurenz Albe


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>
Cc: "Tom Lane *EXTERN*" <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Antonin Houska <ah(at)cybertec(dot)at>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Functions used in index definitions shouldn't be changed
Date: 2014-11-20 19:11:31
Message-ID: CA+TgmoaGXzNeh08SOR13A3=sNyR135rREPmxXB-wStcjnCi6hQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Nov 20, 2014 at 1:56 PM, Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at> wrote:
> I don't think that there is a universally compelling right or wrong to
> questions like this, it is more a matter of taste. Is it more important to protect
> the casual DBA from hurting himself or herself, or is it more important to
> provide a well honed scalpel for the experienced surgeon?

+1.

I think if we had an already-existing prohibition here and you
proposed relaxing it, the howls would be equally loud. We're not
entirely consistent about how picky we are.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>, Antonin Houska <ah(at)cybertec(dot)at>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Functions used in index definitions shouldn't be changed
Date: 2014-11-20 19:28:35
Message-ID: 18706.1416511715@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> On Thu, Nov 20, 2014 at 1:56 PM, Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at> wrote:
>> I don't think that there is a universally compelling right or wrong to
>> questions like this, it is more a matter of taste. Is it more important to protect
>> the casual DBA from hurting himself or herself, or is it more important to
>> provide a well honed scalpel for the experienced surgeon?

> +1.

> I think if we had an already-existing prohibition here and you
> proposed relaxing it, the howls would be equally loud. We're not
> entirely consistent about how picky we are.

How's that quote about foolish consistency go? In many cases, the reason
why we enforce some things and not others is practical utility.

regards, tom lane


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>, Antonin Houska <ah(at)cybertec(dot)at>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Functions used in index definitions shouldn't be changed
Date: 2014-11-20 19:36:17
Message-ID: 546E42B1.3010108@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On 11/20/2014 02:28 PM, Tom Lane wrote:
> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
>> On Thu, Nov 20, 2014 at 1:56 PM, Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at> wrote:
>>> I don't think that there is a universally compelling right or wrong to
>>> questions like this, it is more a matter of taste. Is it more important to protect
>>> the casual DBA from hurting himself or herself, or is it more important to
>>> provide a well honed scalpel for the experienced surgeon?
>> +1.
>> I think if we had an already-existing prohibition here and you
>> proposed relaxing it, the howls would be equally loud. We're not
>> entirely consistent about how picky we are.
> How's that quote about foolish consistency go? In many cases, the reason
> why we enforce some things and not others is practical utility.

Right.

(FTR, the quote from Emerson goes "A foolish consistency is the
hobgoblin of little minds, adored by little statesmen and philosophers
and divines.")

cheers

andrew


From: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: "Robert Haas *EXTERN*" <robertmhaas(at)gmail(dot)com>
Cc: Tom Lane *EXTERN* <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Antonin Houska <ah(at)cybertec(dot)at>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Functions used in index definitions shouldn't be changed
Date: 2014-11-21 08:53:06
Message-ID: A737B7A37273E048B164557ADEF4A58B17DA379F@ntex2010a.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas wrote:
> On Thu, Nov 20, 2014 at 1:56 PM, Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at> wrote:
> > I don't think that there is a universally compelling right or wrong to
> > questions like this, it is more a matter of taste. Is it more important to protect
> > the casual DBA from hurting himself or herself, or is it more important to
> > provide a well honed scalpel for the experienced surgeon?
>
> +1.
>
> I think if we had an already-existing prohibition here and you
> proposed relaxing it, the howls would be equally loud. We're not
> entirely consistent about how picky we are.

There is also the possibility to add syntax like this:

CREATE OR REPLACE [FORCE] FUNCTION ...

What do you think about that? It would protect the casual user but allow
the expert to do it anyway.

Another thing I thought about is changing function volatility:
If you change the volatility of a function used in an index to anything
other than IMMUTABLE, your database will continue to work as expected, but
a dump will fail to restore with
ERROR: functions in index expression must be marked IMMUTABLE

Is that something worth checking for?

Yours,
Laurenz Albe


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>
Cc: "Robert Haas *EXTERN*" <robertmhaas(at)gmail(dot)com>, Antonin Houska <ah(at)cybertec(dot)at>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Functions used in index definitions shouldn't be changed
Date: 2014-11-21 15:06:04
Message-ID: 5770.1416582364@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at> writes:
> There is also the possibility to add syntax like this:
> CREATE OR REPLACE [FORCE] FUNCTION ...
> What do you think about that? It would protect the casual user but allow
> the expert to do it anyway.

I don't see any great attraction to that.

regards, tom lane


From: Jim Nasby <Jim(dot)Nasby(at)BlueTreble(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>
Cc: Robert Haas *EXTERN* <robertmhaas(at)gmail(dot)com>, Antonin Houska <ah(at)cybertec(dot)at>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Functions used in index definitions shouldn't be changed
Date: 2014-11-21 22:34:45
Message-ID: 546FBE05.7070602@BlueTreble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 11/21/14, 9:06 AM, Tom Lane wrote:
> Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at> writes:
>> There is also the possibility to add syntax like this:
>> CREATE OR REPLACE [FORCE] FUNCTION ...
>> What do you think about that? It would protect the casual user but allow
>> the expert to do it anyway.
>
> I don't see any great attraction to that.

Given what this would do to someone's data, and our general stance on not hurting data, I'm a bit surprised that we don't want to do something here. Especially since we did go down this route with disallowing indexes on timestamptz casted to date, which seriously impacts a lot of reporting scenarios.

I fully agree that it's impractical to completely restrict this case, but something akin to FORCE seems reasonable. If nothing else, I'd think we should at least issue a warning if someone does something that might affect index viability.
--
Jim Nasby, Data Architect, Blue Treble Consulting
Data in Trouble? Get it in Treble! http://BlueTreble.com