Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ng3vS-0005UZ-BX for pgsql-general@arkaria.postgresql.org; Sun, 17 Apr 2022 12:22:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ng3vQ-0002jf-NF for pgsql-general@arkaria.postgresql.org; Sun, 17 Apr 2022 12:22:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ng3vQ-0002jV-AA for pgsql-general@lists.postgresql.org; Sun, 17 Apr 2022 12:22:44 +0000 Received: from dog.elm.relay.mailchannels.net ([23.83.212.48]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ng3vO-0007if-Jp for pgsql-general@postgresql.org; Sun, 17 Apr 2022 12:22:43 +0000 X-Sender-Id: a2hosting|x-authuser|pedz+easesoftware.com@mi3-ss4.a2hosting.com Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id DDEB21219EF for ; Sun, 17 Apr 2022 12:22:39 +0000 (UTC) Received: from mi3-ss4.a2hosting.com (unknown [127.0.0.6]) (Authenticated sender: a2hosting) by relay.mailchannels.net (Postfix) with ESMTPA id 2DF25121A1B for ; Sun, 17 Apr 2022 12:22:39 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1650198159; a=rsa-sha256; cv=none; b=XePFc0Shmj4+OV1WgUkNwytaTHHHQbcVqche5kMuN+EjxRfANqHhM0P/rWadVW/BKIeecn w5L912dGRwPcvNMQTts07quyuaNgZQrz/xIbmMvJOAwG1ZInKFLf/yXa08/5HjaEQSJaFC zPqExt8NLcEEJCSfOI1XFZmU3g+LEuUAI8OUk2dbnEaXY8QN8GfIVbE97zAiJYY7EbOja9 1EfeuOH9Ox/QiKKpY/Op14hvAf4K5eos8pDyR4ra2eYa8/BaEoM/gqlJheVaVJPVchpfvL iaYuHaFk8+3h1tQ3grq2OmBu3Oeh5nmNpMrUv4mWK7DcIxbCyjLwBTGL5Q9Yig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1650198159; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hqEweavEH9AG3QktBSlZ/3EgWaWteXhJfRaE653easY=; b=yzQWUqPzdAgueKITSHTwnKBOiqthKlBU0vvcombesubj+8s/bG8RwFWfFfTlCqosf3D121 t7G4pPPAT8BnjnUj4dttcKyu/kG/5vtlIcqbyHKvDg3DKyYngxcPyjLUP4tYFEgfIsQTXW Pn8Nad46QV92pW9I48gGyIQylvvKhj16u24MbSXrp899dJc6zX3X+DbW4141fdcFxgf2v/ ZeqxpzcfQ7MAndvSrdyEVTZ5LJ5kXUr50LlYPhALrJqEt/2wbQ95eHJeY4/1E6YBKObieW 07NU0BOkIFTiSBb0r1PV1HrLjB7hMksIgjrPMgHnQC9BeiUy5OKYSYJ9xCnM+Q== ARC-Authentication-Results: i=1; rspamd-5f678fb567-qbtch; auth=pass smtp.auth=a2hosting smtp.mailfrom=pedz@easesoftware.com X-Sender-Id: a2hosting|x-authuser|pedz+easesoftware.com@mi3-ss4.a2hosting.com Received: from mi3-ss4.a2hosting.com (mi3-ss4.a2hosting.com [68.66.200.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.107.255.147 (trex/6.7.1); Sun, 17 Apr 2022 12:22:39 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: a2hosting|x-authuser|pedz+easesoftware.com@mi3-ss4.a2hosting.com X-MailChannels-Auth-Id: a2hosting X-Tasty-Shoe: 035b8cc2176d1cc7_1650198159743_3852251252 X-MC-Loop-Signature: 1650198159743:1472082742 X-MC-Ingress-Time: 1650198159743 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=easesoftware.com; s=default; h=Message-Id:In-Reply-To:To:References:Date: Subject:Mime-Version:Content-Type:From:Sender:Reply-To:Cc: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=hqEweavEH9AG3QktBSlZ/3EgWaWteXhJfRaE653easY=; b=et2aCiKP54h2/4YF5vJx+Ae0gw 5HMe37W0J0r/oAYlWvZqZzQO8tmCwqzPLXozvjAZ0HkB76uZsIHPUQ8OFcxHn6Tu61OWLf1vGZgnm AL38zOOhdLYNVOuBV2zKuVBSiUk6yHTRs+Pu99SsaPZ0Qg6w/TIYXZjsgUFfMftU6icRcZlypkoqP K34eMMX5dqPVQrGCpBelFJIsuMS2o7D1rdhDMiKoChB+ixpR1tAMSkPpkD5EiSNp9/mC/DViticSW gbe1yVRk1oAqYiap4Iy4bOdx+f9DeeninGrFSQJaD7FzdMXc6ntIxFux2N0bRqGvEE2FS3mxfv/V2 YpR6A/MQ==; Received: from cpe-70-94-128-193.satx.res.rr.com ([70.94.128.193]:57554 helo=smtpclient.apple) by mi3-ss4.a2hosting.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ng3vK-0003Be-4K for pgsql-general@postgresql.org; Sun, 17 Apr 2022 08:22:38 -0400 From: Perry Smith Content-Type: multipart/signed; boundary="Apple-Mail=_FEC52042-15DA-43A1-BB76-6FB82370C5E4"; protocol="application/pgp-signature"; micalg=pgp-sha256 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.80.82.1.1\)) Subject: Re: Help with large delete Date: Sun, 17 Apr 2022 07:22:37 -0500 References: <3823FD54-2910-41E5-B908-869DA917DE82@easesoftware.com> <1939729.1650123237@sss.pgh.pa.us> <57F41293-F307-4579-9459-79A0787E4644@easesoftware.com> To: "pgsql-general@postgresql.org" In-Reply-To: <57F41293-F307-4579-9459-79A0787E4644@easesoftware.com> Message-Id: <91F344A7-8485-474E-BC0A-020A27B653BA@easesoftware.com> X-Mailer: Apple Mail (2.3696.80.82.1.1) X-AuthUser: pedz+easesoftware.com@mi3-ss4.a2hosting.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_FEC52042-15DA-43A1-BB76-6FB82370C5E4 Content-Type: multipart/alternative; boundary="Apple-Mail=_88AB7A60-56FC-4BC2-A2CC-7A1A60238575" --Apple-Mail=_88AB7A60-56FC-4BC2-A2CC-7A1A60238575 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Thank you TOM!!! So=E2=80=A6 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 wrote: >=20 > I=E2=80=99m sending this again. I don=E2=80=99t see that it made it = to the list but there is also new info here. >=20 >> On Apr 16, 2022, at 10:33, Tom Lane > wrote: >>=20 >> Perry Smith > = 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=E2=80=99ve started a delete of a root entry with about 300,000 = descendants. The table currently has about 22M entries and I=E2=80=99m = 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. >>=20 >> 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. >=20 > To try to reply to Peter=E2=80=99s question, I jstarted: >=20 > psql -c "explain analyze delete from dateien where basename =3D = '/mnt/pedz/Visual_Media'=E2=80=9D find_dups >=20 > I did this last night at 10 p.m. and killed it just now at 6:30 = without any response. >=20 > This is inside a BSD =E2=80=9Cjail=E2=80=9D on a NAS. I don=E2=80=99t = know how much CPU the jail is given. >=20 > For Tom=E2=80=99s question, here is the description of the table: >=20 > 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 =3D '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 >=20 > To do a simple delete of a node that has no children takes about 11 = seconds: >=20 > time psql -c "delete from dateien where id =3D 13498939;" find_dups > DELETE 1 > psql -c "delete from dateien where id =3D 13498939;" find_dups 0.00s = user 0.01s system 0% cpu 11.282 total >=20 > I=E2=80=99m 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. >=20 > Thank you for your time, > Perry >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 --Apple-Mail=_88AB7A60-56FC-4BC2-A2CC-7A1A60238575 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 Thank= you TOM!!!

So=E2=80=A6= 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=E2=80=99m sending = this again.  I don=E2=80=99t 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> wrote:

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=E2=80=99ve started a delete of a root = entry with about 300,000 descendants.  The table currently has = about 22M entries and I=E2=80=99m 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=E2=80=99s question, I jstarted:

psql -c "explain analyze = delete from dateien where basename =3D '/mnt/pedz/Visual_Media'=E2=80=9D = 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 =E2=80=9Cjail=E2=80=9D on a NAS.  I = don=E2=80=99t know how much CPU the jail is given.

For Tom=E2=80=99s = 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 =3D '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 =3D 13498939;" find_dups
DELETE 1
psql -c "delete from dateien where id =3D 13498939;" = find_dups  0.00s user 0.01s system 0% cpu 11.282 = total

I=E2=80=99m 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
















= --Apple-Mail=_88AB7A60-56FC-4BC2-A2CC-7A1A60238575-- --Apple-Mail=_FEC52042-15DA-43A1-BB76-6FB82370C5E4 Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=signature.asc Content-Type: application/pgp-signature; name=signature.asc Content-Description: Message signed with OpenPGP -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEE5yOa/gCtQpb3oCpljxzk9yzE+MAFAmJcBo0ACgkQjxzk9yzE +MDiSg//Sv8qQY7k4hvCgsC9iEtuKbzXyiqGcG+I7T1Nhr+4Z5IBX38u79su++Zm Xs8qYxY7yY8DVya2ylGEUDE+gI1rOIO+Xyj+uPOS35bGTSOh4Ssmacq8iucAJkxo tsJYEcu5peuIX/C4JSHKPYCDBb0oCVH0GFPXr/ENiGFjH9tUm8VXxbP7iNr9pWvt CSQlBtZJTGU4m6sGh3nMCa8BDF+n6XdPvfInD+dOo/IE8Bkd8kITl+Gf9rgw1BHL TUP7EF02MCQnNNbZuI3yBcq1rnUgRIThQIzoh25QYLiO2pgb4fq7vRpyuP+kj84u frQ9nN+r8jrIGzoYjRb006S27eWmuixffUd23viL+J9ZryOGkjnCZyt5MHGE7a3c PNmeACcGZ9S6ZIl5iJp9yOFKePq1K8KaWbb/fVYIT4nhd5oOPILfhHr9I7HRz1+9 oYLjOBwP5wrxkDcL+iIU3eBgMlyE/LaApAGlHJGFsGob/TgXXfysCKGznK31hAIa a8tHCk78GK4HE9Bw5yViw8RQNhzFGTx7YTsxLa+vVe5DSh8XEjFJUHCdmrxx5HnK iitaaXI+CgwMdx75itSaOiKC1+HQg/JL+mosTIr9qaxUmlrgmIC/3YBkWvuulMia CF+YsAVAISA+ZiiSyu20jV4GhIv8U496OAN8P1o7s21GhgsZ09o= =VOw6 -----END PGP SIGNATURE----- --Apple-Mail=_FEC52042-15DA-43A1-BB76-6FB82370C5E4--