pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Unable to Create or Drop Index Concurrently
4+ messages / 3 participants
[nested] [flat]

* Unable to Create or Drop Index Concurrently
@ 2022-08-18 05:57 Abdul Qoyyuum <aqoyyuum@cardaccess.com.au>
  2022-08-18 06:05 ` Re: Unable to Create or Drop Index Concurrently Christophe Pettus <xof@thebuild.com>
  2022-08-18 09:00 ` Re: Unable to Create or Drop Index Concurrently hubert depesz lubaczewski <depesz@depesz.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Abdul Qoyyuum @ 2022-08-18 05:57 UTC (permalink / raw)
  To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Hi list,

We have a running Master-Slave High Availability set up. Naturally, we
can't run any changes on read-only databases on slave, so we have to do it
on the master node.

When trying to run the following command:

create index concurrently idx_cash_deposit_channel_id_batch_id on
cash_deposit (channel_id, batch_id);


Waiting for a long time, and my connection dropped. When checking the
table, we get the index as INVALID

Indexes:
    "pk_cash_deposit" PRIMARY KEY, btree (id)
    "idx_cash_deposit_channel_id_batch_id" btree (channel_id, batch_id)
INVALID

And when dropping the invalid index, also takes a long time, my connection
timed out, then when logging back in and check the table, it hasn't dropped.

Question is, do we have to shutdown traffic and close all existing open
connections in order to drop and properly recreate the index? Any advice
appreciated.

-- 
Abdul Qoyyuum Bin Haji Abdul Kadir
HP No: +673 720 8043

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Unable to Create or Drop Index Concurrently
  2022-08-18 05:57 Unable to Create or Drop Index Concurrently Abdul Qoyyuum <aqoyyuum@cardaccess.com.au>
@ 2022-08-18 06:05 ` Christophe Pettus <xof@thebuild.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Christophe Pettus @ 2022-08-18 06:05 UTC (permalink / raw)
  To: Abdul Qoyyuum <aqoyyuum@cardaccess.com.au>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>



> On Aug 17, 2022, at 22:57, Abdul Qoyyuum <aqoyyuum@cardaccess.com.au> wrote:
> Question is, do we have to shutdown traffic and close all existing open connections in order to drop and properly recreate the index?

No, you don't.

On the CREATE INDEX CONCURRENTLY command, what is likely going on is that when the connection drops, the session terminates, which will terminate the CREATE INDEX CONCURRENTLY command and leave the index in an INVALID state.  The problem to solve is preventing the session from disconnecting, either by finding a way to avoid a timeout, connecting via screen or tmux, etc.

On the DROP INDEX, what is likely going on is that the DROP INDEX is waiting for other transactions which are accessing that table to finish, since it needs to take an exclusive lock on the table.  If the session drops, the command isn't run, so the index hasn't been dropped.  The solution is the same as above.  If you are on a version that supports it,  you can use the DROP INDEX CONCURRENTLY command to avoid locking issues with the table, since even before the DROP INDEX happens, new transactions attempting to access that table will queue up behind the DROP INDEX.




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Unable to Create or Drop Index Concurrently
  2022-08-18 05:57 Unable to Create or Drop Index Concurrently Abdul Qoyyuum <aqoyyuum@cardaccess.com.au>
@ 2022-08-18 09:00 ` hubert depesz lubaczewski <depesz@depesz.com>
  2022-08-19 02:46   ` Re: Unable to Create or Drop Index Concurrently Abdul Qoyyuum <aqoyyuum@cardaccess.com.au>
  1 sibling, 1 reply; 4+ messages in thread

From: hubert depesz lubaczewski @ 2022-08-18 09:00 UTC (permalink / raw)
  To: Abdul Qoyyuum <aqoyyuum@cardaccess.com.au>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Thu, Aug 18, 2022 at 01:57:48PM +0800, Abdul Qoyyuum wrote:
> Hi list,
> 
> We have a running Master-Slave High Availability set up. Naturally, we
> can't run any changes on read-only databases on slave, so we have to do it
> on the master node.
> 
> When trying to run the following command:
> 
> create index concurrently idx_cash_deposit_channel_id_batch_id on
> cash_deposit (channel_id, batch_id);
> 
> 
> Waiting for a long time, and my connection dropped. When checking the
> table, we get the index as INVALID
> 
> Indexes:
>     "pk_cash_deposit" PRIMARY KEY, btree (id)
>     "idx_cash_deposit_channel_id_batch_id" btree (channel_id, batch_id)
> INVALID
> 
> And when dropping the invalid index, also takes a long time, my connection
> timed out, then when logging back in and check the table, it hasn't dropped.

This means that you have some very long transactions.

To make/drop index concurrently, all transactions that have started
before you started create/drop, have to finish.

You can see your oldest transactions by doing:

select * from pg_stat_activity where xact_start is not null order by xact_start

Best regards,

depesz






^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Unable to Create or Drop Index Concurrently
  2022-08-18 05:57 Unable to Create or Drop Index Concurrently Abdul Qoyyuum <aqoyyuum@cardaccess.com.au>
  2022-08-18 09:00 ` Re: Unable to Create or Drop Index Concurrently hubert depesz lubaczewski <depesz@depesz.com>
@ 2022-08-19 02:46   ` Abdul Qoyyuum <aqoyyuum@cardaccess.com.au>
  0 siblings, 0 replies; 4+ messages in thread

From: Abdul Qoyyuum @ 2022-08-19 02:46 UTC (permalink / raw)
  To: depesz@depesz.com; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Apparently just leaving it alone until tomorrow managed to finish
creating/dropping the index. Thank you all very much.

On Thu, Aug 18, 2022 at 5:00 PM hubert depesz lubaczewski <depesz@depesz.com>
wrote:

> On Thu, Aug 18, 2022 at 01:57:48PM +0800, Abdul Qoyyuum wrote:
> > Hi list,
> >
> > We have a running Master-Slave High Availability set up. Naturally, we
> > can't run any changes on read-only databases on slave, so we have to do
> it
> > on the master node.
> >
> > When trying to run the following command:
> >
> > create index concurrently idx_cash_deposit_channel_id_batch_id on
> > cash_deposit (channel_id, batch_id);
> >
> >
> > Waiting for a long time, and my connection dropped. When checking the
> > table, we get the index as INVALID
> >
> > Indexes:
> >     "pk_cash_deposit" PRIMARY KEY, btree (id)
> >     "idx_cash_deposit_channel_id_batch_id" btree (channel_id, batch_id)
> > INVALID
> >
> > And when dropping the invalid index, also takes a long time, my
> connection
> > timed out, then when logging back in and check the table, it hasn't
> dropped.
>
> This means that you have some very long transactions.
>
> To make/drop index concurrently, all transactions that have started
> before you started create/drop, have to finish.
>
> You can see your oldest transactions by doing:
>
> select * from pg_stat_activity where xact_start is not null order by
> xact_start
>
> Best regards,
>
> depesz
>
>

-- 
Abdul Qoyyuum Bin Haji Abdul Kadir
HP No: +673 720 8043

^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2022-08-19 02:46 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-08-18 05:57 Unable to Create or Drop Index Concurrently Abdul Qoyyuum <aqoyyuum@cardaccess.com.au>
2022-08-18 06:05 ` Christophe Pettus <xof@thebuild.com>
2022-08-18 09:00 ` hubert depesz lubaczewski <depesz@depesz.com>
2022-08-19 02:46   ` Abdul Qoyyuum <aqoyyuum@cardaccess.com.au>

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