pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Help with large delete
10+ messages / 5 participants
[nested] [flat]

* Help with large delete
@ 2022-04-16 13:25 Perry Smith <pedz@easesoftware.com>
  2022-04-16 13:50 ` Re: Help with large delete Rob Sargent <robjsargent@gmail.com>
  2022-04-16 14:47 ` Re: Help with large delete Peter J. Holzer <hjp-pgsql@hjp.at>
  2022-04-16 15:33 ` Re: Help with large delete Tom Lane <tgl@sss.pgh.pa.us>
  2022-04-16 17:57 ` Re: Help with large delete Jan Wieck <jan@wi3ck.info>
  0 siblings, 4 replies; 10+ messages in thread

From: Perry Smith @ 2022-04-16 13:25 UTC (permalink / raw)
  To: pgsql-general

Currently I have one table that mimics a file system.  Each entry has a parent_id and a base name where parent_id is an id in the table that must exist in the table or be null with cascade on delete.

I’ve started a delete of a root entry with about 300,000 descendants.  The table currently has about 22M entries and I’m adding about 1600 entries per minute still.  Eventually there will not be massive amounts of entries being added and the table will be mostly static.

I started the delete before from a terminal that got detached.  So I killed that process and started it up again from a terminal less likely to get detached.˘

My question is basically how can I make life easier for Postgres?  I believe (hope) the deletes will be few and far between but they will happen from time to time.  In this case, Dropbox — its a long story that isn’t really pertinent.  The point is that @#$% happens.

“What can I do” includes starting completely over if necessary.  I’ve only got about a week invested in this and its just machine time at zero cost.  I could stop the other processes that are adding entries and let the delete finish if that would help.  etc.

Thank you for your time,
Perry

Attachments:

  signature.asc (application/pgp-signature, 832B) download

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

* Re: Help with large delete
  2022-04-16 13:25 Help with large delete Perry Smith <pedz@easesoftware.com>
@ 2022-04-16 13:50 ` Rob Sargent <robjsargent@gmail.com>
  3 siblings, 0 replies; 10+ messages in thread

From: Rob Sargent @ 2022-04-16 13:50 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 4/16/22 07:25, Perry Smith wrote:
> Currently I have one table that mimics a file system.  Each entry has a parent_id and a base name where parent_id is an id in the table that must exist in the table or be null with cascade on delete.
>
> I’ve started a delete of a root entry with about 300,000 descendants.  The table currently has about 22M entries and I’m adding about 1600 entries per minute still.  Eventually there will not be massive amounts of entries being added and the table will be mostly static.
>
> I started the delete before from a terminal that got detached.  So I killed that process and started it up again from a terminal less likely to get detached.˘
>
> My question is basically how can I make life easier for Postgres?  I believe (hope) the deletes will be few and far between but they will happen from time to time.  In this case, Dropbox — its a long story that isn’t really pertinent.  The point is that @#$% happens.
>
> “What can I do” includes starting completely over if necessary.  I’ve only got about a week invested in this and its just machine time at zero cost.  I could stop the other processes that are adding entries and let the delete finish if that would help.  etc.
>
> Thank you for your time,
> Perry
>
I would try 1) find any nodes with disproportionately more nodes, deal 
with them separately.  You my have a gut feel for where these nodes are? 
2) Start at least one step down: run a transaction for each entry in 
root node.  Maybe go two levels down.

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

* Re: Help with large delete
  2022-04-16 13:25 Help with large delete Perry Smith <pedz@easesoftware.com>
@ 2022-04-16 14:47 ` Peter J. Holzer <hjp-pgsql@hjp.at>
  3 siblings, 0 replies; 10+ messages in thread

From: Peter J. Holzer @ 2022-04-16 14:47 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 2022-04-16 08:25:56 -0500, Perry Smith wrote:
> Currently I have one table that mimics a file system.  Each entry has
> a parent_id and a base name where parent_id is an id in the table that
> must exist in the table or be null with cascade on delete.
> 
> I’ve started a delete of a root entry with about 300,000 descendants.
> The table currently has about 22M entries and I’m adding about 1600
> entries per minute still.  Eventually there will not be massive
> amounts of entries being added and the table will be mostly static.
> 
> I started the delete before from a terminal that got detached.  So I
> killed that process and started it up again from a terminal less
> likely to get detached.˘
> 
> My question is basically how can I make life easier for Postgres?

Deleting 300k rows doesn't sound that bad. Neither does recursively
finding those 300k rows, although if you have a very biased distribution
(many nodes with only a few children, but some with hundreds of
thousands or even millions of children), PostgreSQL may not find a good
plan.

So as almost always when performance is an issue:

* What exactly are you doing?
* What is the execution plan?
* How long does it take?

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachments:

  signature.asc (application/pgp-signature, 832B) download

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

* Re: Help with large delete
  2022-04-16 13:25 Help with large delete Perry Smith <pedz@easesoftware.com>
@ 2022-04-16 15:33 ` Tom Lane <tgl@sss.pgh.pa.us>
  2022-04-17 11:58   ` Re: Help with large delete Perry Smith <pedz@easesoftware.com>
  3 siblings, 1 reply; 10+ messages in thread

From: Tom Lane @ 2022-04-16 15:33 UTC (permalink / raw)
  To: Perry Smith <pedz@easesoftware.com>; +Cc: pgsql-general

Perry Smith <pedz@easesoftware.com> writes:
> Currently I have one table that mimics a file system.  Each entry has a parent_id and a base name where parent_id is an id in the table that must exist in the table or be null with cascade on delete.
> I’ve started a delete of a root entry with about 300,000 descendants.  The table currently has about 22M entries and I’m adding about 1600 entries per minute still.  Eventually there will not be massive amounts of entries being added and the table will be mostly static.

The most obvious question is do you have an index on the referencing
column.  PG doesn't require one to exist to create an FK; but if you
don't, deletes of referenced rows had better be uninteresting to you
performance-wise, because each one will cause a seqscan.

			regards, tom lane





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

* Re: Help with large delete
  2022-04-16 13:25 Help with large delete Perry Smith <pedz@easesoftware.com>
  2022-04-16 15:33 ` Re: Help with large delete Tom Lane <tgl@sss.pgh.pa.us>
@ 2022-04-17 11:58   ` Perry Smith <pedz@easesoftware.com>
  2022-04-17 12:22     ` Re: Help with large delete Perry Smith <pedz@easesoftware.com>
  0 siblings, 1 reply; 10+ messages in thread

From: Perry Smith @ 2022-04-17 11:58 UTC (permalink / raw)
  To: pgsql-general

I’m sending this again.  I don’t see that it made it to the list but there is also new info here.

> On Apr 16, 2022, at 10:33, Tom Lane <tgl@sss.pgh.pa.us <mailto:tgl@sss.pgh.pa.us>> wrote:
> 
> Perry Smith <pedz@easesoftware.com <mailto:pedz@easesoftware.com>> writes:
>> Currently I have one table that mimics a file system.  Each entry has a parent_id and a base name where parent_id is an id in the table that must exist in the table or be null with cascade on delete.
>> I’ve started a delete of a root entry with about 300,000 descendants.  The table currently has about 22M entries and I’m adding about 1600 entries per minute still.  Eventually there will not be massive amounts of entries being added and the table will be mostly static.
> 
> The most obvious question is do you have an index on the referencing
> column.  PG doesn't require one to exist to create an FK; but if you
> don't, deletes of referenced rows had better be uninteresting to you
> performance-wise, because each one will cause a seqscan.

To try to reply to Peter’s question, I jstarted:

psql -c "explain analyze delete from dateien where basename = '/mnt/pedz/Visual_Media'” find_dups

I did this last night at 10 p.m. and killed it just now at 6:30 without any response.

This is inside a BSD “jail” on a NAS.  I don’t know how much CPU the jail is given.

For Tom’s question, here is the description of the table:

psql -c '\d dateien' find_dups
                                          Table "public.dateien"
   Column   |              Type              | Collation | Nullable |               Default
------------+--------------------------------+-----------+----------+-------------------------------------
 id         | bigint                         |           | not null | nextval('dateien_id_seq'::regclass)
 basename   | character varying              |           | not null |
 parent_id  | bigint                         |           |          |
 dev        | bigint                         |           | not null |
 ftype      | character varying              |           | not null |
 uid        | bigint                         |           | not null |
 gid        | bigint                         |           | not null |
 ino        | bigint                         |           | not null |
 mode       | bigint                         |           | not null |
 mtime      | timestamp without time zone    |           | not null |
 nlink      | bigint                         |           | not null |
 size       | bigint                         |           | not null |
 sha1       | character varying              |           |          |
 created_at | timestamp(6) without time zone |           | not null |
 updated_at | timestamp(6) without time zone |           | not null |
Indexes:
    "dateien_pkey" PRIMARY KEY, btree (id)
    "unique_dev_ino_for_dirs" UNIQUE, btree (dev, ino) WHERE ftype::text = 'directory'::text
    "unique_parent_basename" UNIQUE, btree (COALESCE(parent_id, '-1'::integer::bigint), basename)
Foreign-key constraints:
    "fk_rails_c01ebbd0bf" FOREIGN KEY (parent_id) REFERENCES dateien(id) ON DELETE CASCADE
Referenced by:
    TABLE "dateien" CONSTRAINT "fk_rails_c01ebbd0bf" FOREIGN KEY (parent_id) REFERENCES dateien(id) ON DELETE CASCADE

To do a simple delete of a node that has no children takes about 11 seconds:

time psql -c "delete from dateien where id = 13498939;" find_dups
DELETE 1
psql -c "delete from dateien where id = 13498939;" find_dups  0.00s user 0.01s system 0% cpu 11.282 total

I’m implementing the suggestion that I do the recession myself but at this rate it will take about 38 days to delete 300K entries.  I must be doing something horribly wrong.  I hope you guys can enlighten me.

Thank you for your time,
Perry

Attachments:

  signature.asc (application/pgp-signature, 832B) download

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

* Re: Help with large delete
  2022-04-16 13:25 Help with large delete Perry Smith <pedz@easesoftware.com>
  2022-04-16 15:33 ` Re: Help with large delete Tom Lane <tgl@sss.pgh.pa.us>
  2022-04-17 11:58   ` Re: Help with large delete Perry Smith <pedz@easesoftware.com>
@ 2022-04-17 12:22     ` Perry Smith <pedz@easesoftware.com>
  0 siblings, 0 replies; 10+ messages in thread

From: Perry Smith @ 2022-04-17 12:22 UTC (permalink / raw)
  To: pgsql-general

Thank you TOM!!!

So… I did:

create index parent_id_index on dateien(parent_id);

And now things are going much faster.  As you can see, I had an index kinda sorta on the parent id but I guess the way I did it prevented Postgres from using it.

> On Apr 17, 2022, at 06:58, Perry Smith <pedz@easesoftware.com> wrote:
> 
> I’m sending this again.  I don’t see that it made it to the list but there is also new info here.
> 
>> On Apr 16, 2022, at 10:33, Tom Lane <tgl@sss.pgh.pa.us <mailto:tgl@sss.pgh.pa.us>> wrote:
>> 
>> Perry Smith <pedz@easesoftware.com <mailto:pedz@easesoftware.com>> writes:
>>> Currently I have one table that mimics a file system.  Each entry has a parent_id and a base name where parent_id is an id in the table that must exist in the table or be null with cascade on delete.
>>> I’ve started a delete of a root entry with about 300,000 descendants.  The table currently has about 22M entries and I’m adding about 1600 entries per minute still.  Eventually there will not be massive amounts of entries being added and the table will be mostly static.
>> 
>> The most obvious question is do you have an index on the referencing
>> column.  PG doesn't require one to exist to create an FK; but if you
>> don't, deletes of referenced rows had better be uninteresting to you
>> performance-wise, because each one will cause a seqscan.
> 
> To try to reply to Peter’s question, I jstarted:
> 
> psql -c "explain analyze delete from dateien where basename = '/mnt/pedz/Visual_Media'” find_dups
> 
> I did this last night at 10 p.m. and killed it just now at 6:30 without any response.
> 
> This is inside a BSD “jail” on a NAS.  I don’t know how much CPU the jail is given.
> 
> For Tom’s question, here is the description of the table:
> 
> psql -c '\d dateien' find_dups
>                                           Table "public.dateien"
>    Column   |              Type              | Collation | Nullable |               Default
> ------------+--------------------------------+-----------+----------+-------------------------------------
>  id         | bigint                         |           | not null | nextval('dateien_id_seq'::regclass)
>  basename   | character varying              |           | not null |
>  parent_id  | bigint                         |           |          |
>  dev        | bigint                         |           | not null |
>  ftype      | character varying              |           | not null |
>  uid        | bigint                         |           | not null |
>  gid        | bigint                         |           | not null |
>  ino        | bigint                         |           | not null |
>  mode       | bigint                         |           | not null |
>  mtime      | timestamp without time zone    |           | not null |
>  nlink      | bigint                         |           | not null |
>  size       | bigint                         |           | not null |
>  sha1       | character varying              |           |          |
>  created_at | timestamp(6) without time zone |           | not null |
>  updated_at | timestamp(6) without time zone |           | not null |
> Indexes:
>     "dateien_pkey" PRIMARY KEY, btree (id)
>     "unique_dev_ino_for_dirs" UNIQUE, btree (dev, ino) WHERE ftype::text = 'directory'::text
>     "unique_parent_basename" UNIQUE, btree (COALESCE(parent_id, '-1'::integer::bigint), basename)
> Foreign-key constraints:
>     "fk_rails_c01ebbd0bf" FOREIGN KEY (parent_id) REFERENCES dateien(id) ON DELETE CASCADE
> Referenced by:
>     TABLE "dateien" CONSTRAINT "fk_rails_c01ebbd0bf" FOREIGN KEY (parent_id) REFERENCES dateien(id) ON DELETE CASCADE
> 
> To do a simple delete of a node that has no children takes about 11 seconds:
> 
> time psql -c "delete from dateien where id = 13498939;" find_dups
> DELETE 1
> psql -c "delete from dateien where id = 13498939;" find_dups  0.00s user 0.01s system 0% cpu 11.282 total
> 
> I’m implementing the suggestion that I do the recession myself but at this rate it will take about 38 days to delete 300K entries.  I must be doing something horribly wrong.  I hope you guys can enlighten me.
> 
> Thank you for your time,
> Perry
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 

Attachments:

  signature.asc (application/pgp-signature, 832B) download

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

* Re: Help with large delete
  2022-04-16 13:25 Help with large delete Perry Smith <pedz@easesoftware.com>
@ 2022-04-16 17:57 ` Jan Wieck <jan@wi3ck.info>
  2022-04-16 18:24   ` Re: Help with large delete Perry Smith <pedz@easesoftware.com>
  3 siblings, 1 reply; 10+ messages in thread

From: Jan Wieck @ 2022-04-16 17:57 UTC (permalink / raw)
  To: Perry Smith <pedz@easesoftware.com>; +Cc: pgsql-general

Make your connection immune to disconnects by using something like the
screen utility.


Regards, Jan

On Sat, Apr 16, 2022, 09:26 Perry Smith <pedz@easesoftware.com> wrote:

> Currently I have one table that mimics a file system.  Each entry has a
> parent_id and a base name where parent_id is an id in the table that must
> exist in the table or be null with cascade on delete.
>
> I’ve started a delete of a root entry with about 300,000 descendants.  The
> table currently has about 22M entries and I’m adding about 1600 entries per
> minute still.  Eventually there will not be massive amounts of entries
> being added and the table will be mostly static.
>
> I started the delete before from a terminal that got detached.  So I
> killed that process and started it up again from a terminal less likely to
> get detached.˘
>
> My question is basically how can I make life easier for Postgres?  I
> believe (hope) the deletes will be few and far between but they will happen
> from time to time.  In this case, Dropbox — its a long story that isn’t
> really pertinent.  The point is that @#$% happens.
>
> “What can I do” includes starting completely over if necessary.  I’ve only
> got about a week invested in this and its just machine time at zero cost.
> I could stop the other processes that are adding entries and let the delete
> finish if that would help.  etc.
>
> Thank you for your time,
> Perry
>
>

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

* Re: Help with large delete
  2022-04-16 13:25 Help with large delete Perry Smith <pedz@easesoftware.com>
  2022-04-16 17:57 ` Re: Help with large delete Jan Wieck <jan@wi3ck.info>
@ 2022-04-16 18:24   ` Perry Smith <pedz@easesoftware.com>
  2022-04-16 18:56     ` Re: Help with large delete Rob Sargent <robjsargent@gmail.com>
  0 siblings, 1 reply; 10+ messages in thread

From: Perry Smith @ 2022-04-16 18:24 UTC (permalink / raw)
  To: pgsql-general



> On Apr 16, 2022, at 12:57, Jan Wieck <jan@wi3ck.info> wrote:
> 
> Make your connection immune to disconnects by using something like the screen utility.

Exactly… I’m using emacs in a server (daemon) mode so it stays alive.  Then I do “shell” within it.


> On Sat, Apr 16, 2022, 09:26 Perry Smith <pedz@easesoftware.com <mailto:pedz@easesoftware.com>> wrote:
> Currently I have one table that mimics a file system.  Each entry has a parent_id and a base name where parent_id is an id in the table that must exist in the table or be null with cascade on delete.
> 
> I’ve started a delete of a root entry with about 300,000 descendants.  The table currently has about 22M entries and I’m adding about 1600 entries per minute still.  Eventually there will not be massive amounts of entries being added and the table will be mostly static.
> 
> I started the delete before from a terminal that got detached.  So I killed that process and started it up again from a terminal less likely to get detached.˘
> 
> My question is basically how can I make life easier for Postgres?  I believe (hope) the deletes will be few and far between but they will happen from time to time.  In this case, Dropbox — its a long story that isn’t really pertinent.  The point is that @#$% happens.
> 
> “What can I do” includes starting completely over if necessary.  I’ve only got about a week invested in this and its just machine time at zero cost.  I could stop the other processes that are adding entries and let the delete finish if that would help.  etc.
> 
> Thank you for your time,
> Perry
> 

Attachments:

  signature.asc (application/pgp-signature, 832B) download

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

* Re: Help with large delete
  2022-04-16 13:25 Help with large delete Perry Smith <pedz@easesoftware.com>
  2022-04-16 17:57 ` Re: Help with large delete Jan Wieck <jan@wi3ck.info>
  2022-04-16 18:24   ` Re: Help with large delete Perry Smith <pedz@easesoftware.com>
@ 2022-04-16 18:56     ` Rob Sargent <robjsargent@gmail.com>
  2022-04-16 19:06       ` Re: Help with large delete Perry Smith <pedz@easesoftware.com>
  0 siblings, 1 reply; 10+ messages in thread

From: Rob Sargent @ 2022-04-16 18:56 UTC (permalink / raw)
  To: Perry Smith <pedz@easesoftware.com>; +Cc: pgsql-general



> On Apr 16, 2022, at 12:24 PM, Perry Smith <pedz@easesoftware.com> wrote:
> 
> 
> 
>>> On Apr 16, 2022, at 12:57, Jan Wieck <jan@wi3ck.info> wrote:
>>> 
>>> Make your connection immune to disconnects by using something like the screen utility.
>> 
>> Exactly… I’m using emacs in a server (daemon) mode so it stays alive.  Then I do “shell” within it.
I use emacs a lot.  It doesn’t keep the terminal alive in my experience. Perhaps nohup?
>>> 
> 

Attachments:

  signature.asc (application/octet-stream, 832B) download

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

* Re: Help with large delete
  2022-04-16 13:25 Help with large delete Perry Smith <pedz@easesoftware.com>
  2022-04-16 17:57 ` Re: Help with large delete Jan Wieck <jan@wi3ck.info>
  2022-04-16 18:24   ` Re: Help with large delete Perry Smith <pedz@easesoftware.com>
  2022-04-16 18:56     ` Re: Help with large delete Rob Sargent <robjsargent@gmail.com>
@ 2022-04-16 19:06       ` Perry Smith <pedz@easesoftware.com>
  0 siblings, 0 replies; 10+ messages in thread

From: Perry Smith @ 2022-04-16 19:06 UTC (permalink / raw)
  To: pgsql-general



> On Apr 16, 2022, at 13:56, Rob Sargent <robjsargent@gmail.com> wrote:
> 
> 
> 
>> On Apr 16, 2022, at 12:24 PM, Perry Smith <pedz@easesoftware.com> wrote:
>> 
>> 
>> 
>>> On Apr 16, 2022, at 12:57, Jan Wieck <jan@wi3ck.info <mailto:jan@wi3ck.info>> wrote:
>>> 
>>> Make your connection immune to disconnects by using something like the screen utility.
>> 
>> Exactly… I’m using emacs in a server (daemon) mode so it stays alive.  Then I do “shell” within it.
> I use emacs a lot.  It doesn’t keep the terminal alive in my experience. Perhaps nohup?

https://www.emacswiki.org/emacs/EmacsAsDaemon

Doing: emacs —daemon

You will see a couple of messages about loading up the customized file and then it detaches and you get back to the prompt.

Attachments:

  signature.asc (application/pgp-signature, 832B) download

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


end of thread, other threads:[~2022-04-17 12:22 UTC | newest]

Thread overview: 10+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-04-16 13:25 Help with large delete Perry Smith <pedz@easesoftware.com>
2022-04-16 13:50 ` Rob Sargent <robjsargent@gmail.com>
2022-04-16 14:47 ` Peter J. Holzer <hjp-pgsql@hjp.at>
2022-04-16 15:33 ` Tom Lane <tgl@sss.pgh.pa.us>
2022-04-17 11:58   ` Perry Smith <pedz@easesoftware.com>
2022-04-17 12:22     ` Perry Smith <pedz@easesoftware.com>
2022-04-16 17:57 ` Jan Wieck <jan@wi3ck.info>
2022-04-16 18:24   ` Perry Smith <pedz@easesoftware.com>
2022-04-16 18:56     ` Rob Sargent <robjsargent@gmail.com>
2022-04-16 19:06       ` Perry Smith <pedz@easesoftware.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