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 1nfm1y-0007Sj-S8 for pgsql-general@arkaria.postgresql.org; Sat, 16 Apr 2022 17:16:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nfm1x-00021a-NV for pgsql-general@arkaria.postgresql.org; Sat, 16 Apr 2022 17:16:17 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nfm1x-00021O-Cz for pgsql-general@lists.postgresql.org; Sat, 16 Apr 2022 17:16:17 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nfm1v-0001u9-6V for pgsql-general@postgresql.org; Sat, 16 Apr 2022 17:16:16 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 23GHFXrn1958275; Sat, 16 Apr 2022 13:15:33 -0400 From: Tom Lane To: Perry Smith cc: "pgsql-general@postgresql.org" Subject: Re: ***SPAM*** Re: Help with large delete In-reply-to: References: <3823FD54-2910-41E5-B908-869DA917DE82@easesoftware.com> <1939729.1650123237@sss.pgh.pa.us> Comments: In-reply-to Perry Smith message dated "Sat, 16 Apr 2022 11:54:32 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <1958273.1650129333.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sat, 16 Apr 2022 13:15:33 -0400 Message-ID: <1958274.1650129333@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Perry Smith writes: > On Apr 16, 2022, at 10:33, Tom Lane wrote: >> 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. > 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 | ne= xtval('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'::in= teger::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 Yeah. So if you want to make deletes on this table not be unpleasantly slow, you need an index on the parent_id column, and you don't have one. (unique_parent_basename doesn't help, because with that definition it's useless for looking up rows by parent_id.) regards, tom lane