Re: are there any method that "Update" command not affect other unrelated indices?

Lists: pgsql-general
From: sunpeng <bluevaley(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: are there any method that "Update" command not affect other unrelated indices?
Date: 2010-10-12 22:03:26
Message-ID: AANLkTi=zhUeL=2N_m=E-dR+hzAmVK6PcvxwgR7Y1rf3+@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hi, I have the following table:
CREATE TABLE A
(
a1 integer not null,
a2 integer,
a3 integer,
a4 integer
)
and have the following four indices:
create index ind_a1 on A USING gist(a1);
create index ind_a2 on A USING gist(a2);
create index ind_a3 on A USING gist(a3);
create index ind_a4 on A USING gist(a4);

now we have 10,000 update command executions using spi_exeplan():
SPI_prepare(); // prepare the plan for "update A set a4 = $1;"
for(i=0;i<10000;i++ ){
SPI_execute_plan();// update A set a4 = i;
}

the question is why all four indices updated in the execution of
SPI_execute_plan()?
I think there should only one index, that is ind_a4 be updated, how to avoid
other three indices updated?
thanks!


From: Ben Carbery <ben(dot)carbery(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: are there any method that "Update" command not affect other unrelated indices?
Date: 2010-10-12 22:38:15
Message-ID: AANLkTik7=1g+US-e1HxHxjWyD1urTRE4p8Hs14GtP-2f@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Well, the objects indices 1,2,3 point to changed when you changed column a4,
but I don't know if that's the reason. I would guess that the indices are
structured as pointers of some kind though.

On Wed, Oct 13, 2010 at 9:03 AM, sunpeng <bluevaley(at)gmail(dot)com> wrote:

>
> the question is why all four indices updated in the execution of
> SPI_execute_plan()?
> I think there should only one index, that is ind_a4 be updated, how to
> avoid other three indices updated?
> thanks!
>
>


From: sunpeng <bluevaley(at)gmail(dot)com>
To: Ben Carbery <ben(dot)carbery(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: are there any method that "Update" command not affect other unrelated indices?
Date: 2010-10-12 22:51:17
Message-ID: AANLkTikkkcH44Vp1=v_fUUQ450+BfKJJNp5JmZ_0SqK3@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Thanks. I could give more clues.
The call stack of the function most consumed time is:
Thread [1] (Suspended)
34 ExecInsertIndexTuples()
/home/postgres/develop/postgresql-snapshot/src/backend/executor/execUtils.c:1046
0x08201e66
33 ExecUpdate()
/home/postgres/develop/postgresql-snapshot/src/backend/executor/execMain.c:2135
0x081f3b13
32 ExecutePlan()
/home/postgres/develop/postgresql-snapshot/src/backend/executor/execMain.c:1681
0x081f31c6
31 standard_ExecutorRun()
/home/postgres/develop/postgresql-snapshot/src/backend/executor/execMain.c:309
0x081f0f4b
30 ExecutorRun()
/home/postgres/develop/postgresql-snapshot/src/backend/executor/execMain.c:258
0x081f0e04
29 _SPI_pquery()
/home/postgres/develop/postgresql-snapshot/src/backend/executor/spi.c:2009
0x0821fe8c
28 _SPI_execute_plan()
/home/postgres/develop/postgresql-snapshot/src/backend/executor/spi.c:1831
0x0821facd
27 SPI_execute_plan()
/home/postgres/develop/postgresql-snapshot/src/backend/executor/spi.c:392
0x0821d201

in execMain.c, the call of ExecInsertIndexTuples() is as following:
if (resultRelInfo->ri_NumIndices > 0 && !HeapTupleIsHeapOnly(tuple))
ExecInsertIndexTuples(slot, &(tuple->t_self), estate, false);

2010/10/12 Ben Carbery <ben(dot)carbery(at)gmail(dot)com>

> Well, the objects indices 1,2,3 point to changed when you changed column
> a4, but I don't know if that's the reason. I would guess that the indices
> are structured as pointers of some kind though.
>
>
> On Wed, Oct 13, 2010 at 9:03 AM, sunpeng <bluevaley(at)gmail(dot)com> wrote:
>
>>
>> the question is why all four indices updated in the execution of
>> SPI_execute_plan()?
>> I think there should only one index, that is ind_a4 be updated, how to
>> avoid other three indices updated?
>> thanks!
>>
>>


From: Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>
To: sunpeng <bluevaley(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: are there any method that "Update" command not affect other unrelated indices?
Date: 2010-10-13 06:38:19
Message-ID: AF861344-9919-43A3-9581-39B4469679EA@solfertje.student.utwente.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On 13 Oct 2010, at 24:03, sunpeng wrote:

> Hi, I have the following table:
> CREATE TABLE A
> (
> a1 integer not null,
> a2 integer,
> a3 integer,
> a4 integer
> )
> and have the following four indices:
> create index ind_a1 on A USING gist(a1);
> create index ind_a2 on A USING gist(a2);
> create index ind_a3 on A USING gist(a3);
> create index ind_a4 on A USING gist(a4);
>
> now we have 10,000 update command executions using spi_exeplan():
> SPI_prepare(); // prepare the plan for "update A set a4 = $1;"
> for(i=0;i<10000;i++ ){
> SPI_execute_plan();// update A set a4 = i;
> }
>
> the question is why all four indices updated in the execution of SPI_execute_plan()?
> I think there should only one index, that is ind_a4 be updated, how to avoid other three indices updated?

Obviously the indices need to point to the new version of the row as well, or you won't be able to find the new version using the other indices.

Alban Hertroys

--
Screwing up is an excellent way to attach something to the ceiling.

!DSPAM:737,4cb55404678308231573016!


From: Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>
To: sunpeng <bluevaley(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: are there any method that "Update" command not affect other unrelated indices?
Date: 2010-10-13 06:52:33
Message-ID: FA1370A2-6C7A-4FCB-A1C8-F2715E5568C0@solfertje.student.utwente.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On 13 Oct 2010, at 24:03, sunpeng wrote:

> Hi, I have the following table:
> CREATE TABLE A
> (
> a1 integer not null,
> a2 integer,
> a3 integer,
> a4 integer
> )
> and have the following four indices:
> create index ind_a1 on A USING gist(a1);
> create index ind_a2 on A USING gist(a2);
> create index ind_a3 on A USING gist(a3);
> create index ind_a4 on A USING gist(a4);

Is there any reason you're using gist indices on integer fields? Those are primarily used with full-text searching, I wouldn't expect them to be particularly efficient for scalar values. A (default) btree would probably perform better.

Alban Hertroys

--
If you can't see the forest for the trees,
cut the trees and you'll see there is no forest.

!DSPAM:737,4cb55736678302085914008!


From: "Igor Neyman" <ineyman(at)perceptron(dot)com>
To: "Alban Hertroys" <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>, "sunpeng" <bluevaley(at)gmail(dot)com>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: are there any method that "Update" command not affect other unrelated indices?
Date: 2010-10-13 14:31:03
Message-ID: F4C27E77F7A33E4CA98C19A9DC6722A2069F29D7@EXCHANGE.corp.perceptron.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

> -----Original Message-----
> From: Alban Hertroys [mailto:dalroi(at)solfertje(dot)student(dot)utwente(dot)nl]
> Sent: Wednesday, October 13, 2010 2:38 AM
> To: sunpeng
> Cc: pgsql-general(at)postgresql(dot)org
> Subject: Re: are there any method that "Update" command not
> affect other unrelated indices?
>
> On 13 Oct 2010, at 24:03, sunpeng wrote:
>
> > Hi, I have the following table:
> > CREATE TABLE A
> > (
> > a1 integer not null,
> > a2 integer,
> > a3 integer,
> > a4 integer
> > )
> > and have the following four indices:
> > create index ind_a1 on A USING gist(a1); create index ind_a2 on A
> > USING gist(a2); create index ind_a3 on A USING gist(a3);
> create index
> > ind_a4 on A USING gist(a4);
> >
> > now we have 10,000 update command executions using spi_exeplan():
> > SPI_prepare(); // prepare the plan for "update A set a4 = $1;"
> > for(i=0;i<10000;i++ ){
> > SPI_execute_plan();// update A set a4 = i;
> > }
> >
> > the question is why all four indices updated in the
> execution of SPI_execute_plan()?
> > I think there should only one index, that is ind_a4 be
> updated, how to avoid other three indices updated?
>
> Obviously the indices need to point to the new version of the
> row as well, or you won't be able to find the new version
> using the other indices.
>
> Alban Hertroys
>
> --
> Screwing up is an excellent way to attach something to the ceiling.
>
>
> !DSPAM:737,4cb55404678308231573016!
>

Just to make clear what Alban said, because it looks like OP is not
familiar with Postgres MVCC model.

All indices need to be updated, because Postgres does not do "upgrade in
place", like some other databases do.
When any column is updated, new version of the row created and the old
one marked as deleted.

Regards,
Igor Neyman


From: Vick Khera <vivek(at)khera(dot)org>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: are there any method that "Update" command not affect other unrelated indices?
Date: 2010-10-13 15:23:49
Message-ID: AANLkTik0J1ZHOmvDf5hw-WoDdqoVZ+vsFp5fzJ6EeYu6@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Wed, Oct 13, 2010 at 10:31 AM, Igor Neyman <ineyman(at)perceptron(dot)com> wrote:
> All indices need to be updated, because Postgres does not do "upgrade in
> place", like some other databases do.
> When any column is updated, new version of the row created and the old
> one marked as deleted.
>

If you qualify for a HOT update the indexes do not need updating.