Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id DED8C1337B6E for ; Thu, 31 Mar 2011 11:54:44 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 41588-09 for ; Thu, 31 Mar 2011 14:54:26 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from web39707.mail.mud.yahoo.com (web39707.mail.mud.yahoo.com [209.191.106.53]) by mail.postgresql.org (Postfix) with SMTP id BC9891337B2F for ; Thu, 31 Mar 2011 11:54:25 -0300 (ADT) Received: (qmail 88616 invoked by uid 60001); 31 Mar 2011 14:54:25 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1301583265; bh=+pb318u1Nf8xpMUHcjNFkCIwhdkcOzXeabXMKOz48i0=; h=Message-ID:X-YMail-OSG:Received:X-Mailer:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=ynERHQ62IyaMhhP+cFOdMJvWsyAiERJ2XauSm82rwN8/0Kr629sYOjI08skJniAhdspfCGCq2Z2Fey84LRElksWWzRSCj6pvb/QwO983y81BD7ThFpqHjVj1h+aVQ8B5nMX0Cp7Nygkv2EDEFrFWF8qEeljrGKvgibwSOCcK4L8= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:X-YMail-OSG:Received:X-Mailer:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=NIFTmc3FP3r/4iW0vsvlmEw0V0goIyu3uFvrw+v9GNvTccetV18dtutEdQa38ChFeVzGwP4StYa8tgGGyMC6n6bo0kq7EqJyixPjRCTCYIzUvYhTJxKrbu+n9A474+Ymq5Di5S6gkwRsxcTiYtVmpMaWrMQWNoWKvnpze/s0qog=; Message-ID: <699909.75581.qm@web39707.mail.mud.yahoo.com> X-YMail-OSG: INUKrE8VM1l1xix1Onvvk_YwqURqi_mp.3P5.MaMBz77niT yQj5kgDGBs0hrdiI9OsNY8aixW2bm.Y_c6nITA18QsMeHC8EXStQ5PZDoRd. I3rEiU__.oZvo68b0cVGN.b2Gc2o7FyZe2b5sJdcSMS_srU3oA5nq9rYailP mMDDsVNEXevsiAO9Ca6f1yvpzStdu5BR.h9WPGB112xffE3_eyIafn6gonfg MbIoDoMDy.3j.cmiqBM3xhOE1Ee_loE84XWG6JdSwbqLllHOppH7Vch8Ko87 q7muQ4wqs_ekQ5dC4xfWIFU5OXZicUC1NtT8CESh5ckE4WfuUKKI873ISs41 fVXsycSrCb9GOeLwSAmT7pBx11yQDvGc4YDc1A8HLKPdGGTexRR3iFljEt9c - Received: from [64.126.29.122] by web39707.mail.mud.yahoo.com via HTTP; Thu, 31 Mar 2011 07:54:25 PDT X-Mailer: YahooMailClassic/12.0.2 YahooMailWebService/0.8.109.295617 Date: Thu, 31 Mar 2011 07:54:25 -0700 (PDT) From: Bob Lunney Subject: Re: Slow deleting tables with foreign keys To: "pgsql-performance@postgresql.org" , Jeremy Palmer In-Reply-To: <666FB8D75E95AE42965A0E76A5E5337E06DCBABE90@prdlsmmsg01.ad.linz.govt.nz> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.908 tagged_above=-10 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RFC_ABUSE_POST=0.001, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201103/377 X-Sequence-Number: 43056 Jeremy,=0A=0ADoes table_revision have a unique index on id? Also, I doubt = these two indexes ever get used:=0A=0ACREATE INDEX idx_crs_action_expired_c= reated=0A ON table_version.bde_crs_action_revision=0A USING btree=0A (_r= evision_expired, _revision_created);=0A=0ACREATE INDEX idx_crs_action_expir= ed_key=0A ON table_version.bde_crs_action_revision=0A USING btree=0A (_r= evision_expired, audit_id);=0A=0ABob Lunney=0A=0A--- On Wed, 3/30/11, Jerem= y Palmer wrote:=0A=0A> From: Jeremy Palmer =0A> Subject: [PERFORM] Slow deleting tables with foreign keys= =0A> To: "pgsql-performance@postgresql.org" =0A> Date: Wednesday, March 30, 2011, 10:16 PM=0A> Hi All,=0A> =0A> I'm = trying to delete one row from a table and it's taking=0A> an extremely long= time. This parent table is referenced by=0A> other table's foreign keys, b= ut the particular row I'm=0A> trying to delete is not referenced any other = rows in the=0A> associative tables. This table has the following structure:= =0A> =0A> CREATE TABLE revision=0A> (=0A> =A0 id serial NOT NULL,=0A> =A0 r= evision_time timestamp without time zone NOT NULL=0A> DEFAULT now(),=0A> = =A0 start_time timestamp without time zone NOT NULL=0A> DEFAULT clock_times= tamp(),=0A> =A0 schema_change boolean NOT NULL,=0A> =A0 "comment" text,=0A>= =A0 CONSTRAINT revision_pkey PRIMARY KEY (id)=0A> )=0A> WITH (=0A> =A0 OID= S=3DFALSE=0A> );=0A> =0A> This table is referenced from foreign key by 130 = odd other=0A> tables. The total number of rows from these referencing=0A> t= ables goes into the hundreds of millions. Each of these=0A> tables has been= automatically created by script and has the=0A> same _revision_created, _r= evision_expired fields, foreign=0A> keys and indexes. Here is an example of= one:=0A> =0A> CREATE TABLE table_version.bde_crs_action_revision=0A> (=0A>= =A0 _revision_created integer NOT NULL,=0A> =A0 _revision_expired integer,= =0A> =A0 tin_id integer NOT NULL,=0A> =A0 id integer NOT NULL,=0A> =A0 "seq= uence" integer NOT NULL,=0A> =A0 att_type character varying(4) NOT NULL,=0A= > =A0 system_action character(1) NOT NULL,=0A> =A0 audit_id integer NOT NUL= L,=0A> =A0 CONSTRAINT=0A> "pkey_table_version.bde_crs_action_revision" PRIM= ARY KEY=0A> (_revision_created, audit_id),=0A> =A0 CONSTRAINT=0A> bde_crs_a= ction_revision__revision_created_fkey FOREIGN KEY=0A> (_revision_created)= =0A> =A0 =A0 =A0 REFERENCES table_version.revision (id)=0A> MATCH SIMPLE=0A= > =A0 =A0 =A0 ON UPDATE NO ACTION ON DELETE NO=0A> ACTION,=0A> =A0 CONSTRAI= NT=0A> bde_crs_action_revision__revision_expired_fkey FOREIGN KEY=0A> (_rev= ision_expired)=0A> =A0 =A0 =A0 REFERENCES table_version.revision (id)=0A> M= ATCH SIMPLE=0A> =A0 =A0 =A0 ON UPDATE NO ACTION ON DELETE NO=0A> ACTION=0A>= )=0A> WITH (=0A> =A0 OIDS=3DFALSE=0A> );=0A> ALTER TABLE table_version.bde= _crs_action_revision OWNER TO=0A> bde_dba;=0A> ALTER TABLE table_version.bd= e_crs_action_revision ALTER=0A> COLUMN audit_id SET STATISTICS 500;=0A> =0A= > =0A> CREATE INDEX idx_crs_action_audit_id=0A> =A0 ON table_version.bde_cr= s_action_revision=0A> =A0 USING btree=0A> =A0 (audit_id);=0A> =0A> CREATE I= NDEX idx_crs_action_created=0A> =A0 ON table_version.bde_crs_action_revisio= n=0A> =A0 USING btree=0A> =A0 (_revision_created);=0A> =0A> CREATE INDEX id= x_crs_action_expired=0A> =A0 ON table_version.bde_crs_action_revision=0A> = =A0 USING btree=0A> =A0 (_revision_expired);=0A> =0A> CREATE INDEX idx_crs_= action_expired_created=0A> =A0 ON table_version.bde_crs_action_revision=0A>= =A0 USING btree=0A> =A0 (_revision_expired, _revision_created);=0A> =0A> C= REATE INDEX idx_crs_action_expired_key=0A> =A0 ON table_version.bde_crs_act= ion_revision=0A> =A0 USING btree=0A> =A0 (_revision_expired, audit_id);=0A>= =0A> =0A> All of the table have been analysed before I tried to run=0A> th= e query.=0A> =0A> The fact the all of the foreign keys have a covering inde= x=0A> makes me wonder why this delete is taking so long.=0A> =0A> The expla= in for =0A> =0A> delete from table_version.revision where id =3D 1003=0A> = =0A> =0A> Delete=A0 (cost=3D0.00..1.02 rows=3D1 width=3D6)=0A> =A0 ->=A0 Se= q Scan on revision=A0=0A> (cost=3D0.00..1.02 rows=3D1 width=3D6)=0A> =A0 = =A0 =A0 =A0 Filter: (id =3D 100)=0A> =0A> I'm running POstgreSQL 9.0.2 on U= buntu 10.4=0A> =0A> Cheers=0A> Jeremy=0A> _________________________________= _____________________________________________________________________=0A> = =0A> This message contains information, which is confidential=0A> and may b= e subject to legal privilege. =0A> If you are not the intended recipient, y= ou must not peruse,=0A> use, disseminate, distribute or copy this message.= =0A> If you have received this message in error, please notify=0A> us immed= iately (Phone 0800 665 463 or info@linz.govt.nz)=0A> and destroy the origin= al message.=0A> LINZ accepts no responsibility for changes to this email,= =0A> or for any attachments, after its transmission from LINZ.=0A> =0A> Tha= nk you.=0A> _______________________________________________________________= _______________________________________=0A> =0A> -- =0A> Sent via pgsql-per= formance mailing list (pgsql-performance@postgresql.org)=0A> To make change= s to your subscription:=0A> http://www.postgresql.org/mailpref/pgsql-perfor= mance=0A> =0A=0A=0A