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 1ng3Yg-0003ld-Ov for pgsql-general@arkaria.postgresql.org; Sun, 17 Apr 2022 11:59:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ng3Ye-00073J-OS for pgsql-general@arkaria.postgresql.org; Sun, 17 Apr 2022 11:59:12 +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 1ng3Yd-00073A-Uq for pgsql-general@lists.postgresql.org; Sun, 17 Apr 2022 11:59:12 +0000 Received: from olivedrab.birch.relay.mailchannels.net ([23.83.209.135]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ng3YW-0007RH-He for pgsql-general@postgresql.org; Sun, 17 Apr 2022 11:59:11 +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 41382800C54 for ; Sun, 17 Apr 2022 11:59:01 +0000 (UTC) Received: from mi3-ss4.a2hosting.com (unknown [127.0.0.6]) (Authenticated sender: a2hosting) by relay.mailchannels.net (Postfix) with ESMTPA id 8D018800A56 for ; Sun, 17 Apr 2022 11:59:00 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1650196740; a=rsa-sha256; cv=none; b=eTuLBVh6jsSFUTpO66xnIBup4Ppb2NpVgoQ0S336LqooMz13uOoptZAcbjtlX4lWlk7xjR oiUmVW2CutbeCimj8WWrof7TRlhXlSTP6sSKrc2/2oCFm6/oLuFAMD+2aDUqsDSRtbOON+ GaIjmTYSJsQfAQ+VMseV1U5BDNN12JJh1HCWyV6LIwfR2+hHrXoY7SxfTfrTcXAxRFziq1 0KlkSw5i15LdoOg8PVDblTD8aEttMPfHLXhCQeMJSmAoJvO7vGF39lAWuU93XcvJDCBc1E SuQN+wZMoXp6bjPe6Up9nC3xWHvnDFOLwHIg73H5Kj0N4WR+CUweWZ33IOl7Xw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1650196740; 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=BQjVGXyB6juo6alIlOVPclrqWoseUPvgadfGrCuG/Fs=; b=pG6DHg5acXdCfl2DB9MuS2a2Myr1m+kskrtEB7P2pkRqtRkwkaMp2FxSHqV1d1s4mIlyKF k+0w1M4i29FnZxO/2IWOT7vUNF2ZhvYypkgqxestQjB56xvsy9xPDT8lzI/aHzei7aJXL+ vhmSVQB2bPWKfvYIzxNknXiuZFpvPyBjikoM3/dRKbJH8JS3pUEGAx+xHVwC4+G3LO7Wfo vPyxakRDEaYaFDgFsDPo5EwQsw+0hy+hjlV+5RreLygvPBw4MNgEqgfRYVKo3kPvzPvPP/ 8zKm5Ha+pwUTVxlUqDntlSJORMwosqZ3hyDoH+3UZiG6AXIZ2bSv55vuF3ht+w== ARC-Authentication-Results: i=1; rspamd-5f678fb567-vwqvc; 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.96.96.16 (trex/6.7.1); Sun, 17 Apr 2022 11:59:01 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: a2hosting|x-authuser|pedz+easesoftware.com@mi3-ss4.a2hosting.com X-MailChannels-Auth-Id: a2hosting X-Print-Keen: 60633a754bb6954a_1650196741018_101508219 X-MC-Loop-Signature: 1650196741017:1731555606 X-MC-Ingress-Time: 1650196741017 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=BQjVGXyB6juo6alIlOVPclrqWoseUPvgadfGrCuG/Fs=; b=CUlCnJPlVF9xIhOePIYdOaZUbr 4ATnA8QHUPRq8byY3xeDIFxu2dZkocHVkLk/ljDT1noOMvIN6SMeVWqTjnxHnY3Df1GyumWyDqZ5K CRUdvFVDIRwXUgnK64NuoggOdwZ1cEyqL/zc/KJv7GhiVVpvCIqBgNZR0d+0XK/94n4Tx6Pi9QqOb BCWYd3YBvLUmfRIzz7rUTDpt284yJ5XJRqDem/GDRdX2YkC+CEA65TR2vKHJIUFHgHdG1zUOWcypm kQgWZj2eWQwPep+npe4KoLyBapFmhcGgWpFbi4jDhgFzWKACmI22TDfJBtTn8bSLTRKHUO2CaKHpW Ve8lTGdw==; Received: from cpe-70-94-128-193.satx.res.rr.com ([70.94.128.193]:57508 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 1ng3YQ-0002gz-IB for pgsql-general@postgresql.org; Sun, 17 Apr 2022 07:58:58 -0400 From: Perry Smith Content-Type: multipart/signed; boundary="Apple-Mail=_F934A118-CD35-47B5-8DEA-085B194FF7BA"; 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 06:58:57 -0500 References: <3823FD54-2910-41E5-B908-869DA917DE82@easesoftware.com> <1939729.1650123237@sss.pgh.pa.us> To: "pgsql-general@postgresql.org" In-Reply-To: <1939729.1650123237@sss.pgh.pa.us> Message-Id: <57F41293-F307-4579-9459-79A0787E4644@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=_F934A118-CD35-47B5-8DEA-085B194FF7BA Content-Type: multipart/alternative; boundary="Apple-Mail=_3175AC95-7AD2-4BDD-9993-0C181DA029B7" --Apple-Mail=_3175AC95-7AD2-4BDD-9993-0C181DA029B7 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 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 > 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. 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=_3175AC95-7AD2-4BDD-9993-0C181DA029B7 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
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=_3175AC95-7AD2-4BDD-9993-0C181DA029B7-- --Apple-Mail=_F934A118-CD35-47B5-8DEA-085B194FF7BA 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+MAFAmJcAQEACgkQjxzk9yzE +MDWOxAA53N2coBqk0v7wBpIFJKYPUGA0eLzC+dLZjmfVQYvrq6a8ZYWuVPgHSB9 mKRGrQ9a9QYEe0KYDT19T813dA1djzaY3Xq49jxVgD9nZuToEZkvRxri3Xk6qOPd j662KzeX065Y2/MaxuJHHHBS3Li6RdmjR3R8pAi40KBzt5Gsn9RBl1hfrLVgD5fv O2oh/vmM/HPVUUAEqY5195Ypz7Nucx1CjlHxhEPrgzmHgZtWzaZMsb/INDVnTm3H Rnpctj3HUg3cAgOxbES0U7AYmhlnTeZO6UFYennB0dTVeXwW1Cq2udu0qu4IeUlU 0hAurK+tugBOmziGG31x6HekfbZ8a3QO88QZ2yPnmZPyRat+pOvNum/2p3k6zljL w3FWK6t0KSoyIBXtwFVB/IUa8zTomP9SOSY2mqTX+7phi1P/7B8i1J1BSWw2ksRj CR7jK+meKMer7CNwBdKau5qy66Lywfjhr9L+fBGiwVWOPWVAqNtSHIk3yDxWjwkP 3KN1ygWYwmIKyG2RNmKhMji/6OTgy42t1DX1bkNlEggdpQufrkEaPkjUZxISB2z0 IpEYw9wHzEULt5zU9Sadj1C3myzTXPTxidx4OB19o/fjF/Pqhxz6lgSnlya63v2E QAbqd0472pFhd9yEq7VeWorguqXrGqLfUj8MyMQa0CfLJk9E8ek= =sfSk -----END PGP SIGNATURE----- --Apple-Mail=_F934A118-CD35-47B5-8DEA-085B194FF7BA--