Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fwiwW-00069u-5f for pgsql-performance@arkaria.postgresql.org; Mon, 03 Sep 2018 07:06:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fwiwT-0004nq-H0 for pgsql-performance@arkaria.postgresql.org; Mon, 03 Sep 2018 07:06:33 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fwiwT-0004nf-4t for pgsql-performance@lists.postgresql.org; Mon, 03 Sep 2018 07:06:33 +0000 Received: from mail-io0-x241.google.com ([2607:f8b0:4001:c06::241]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fwiwP-00057t-E3 for pgsql-performance@postgresql.org; Mon, 03 Sep 2018 07:06:32 +0000 Received: by mail-io0-x241.google.com with SMTP id v14-v6so15118674iob.4 for ; Mon, 03 Sep 2018 00:06:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=GJOz3/fUi3hN/mG1KbRzAvv/7oJlEnmCw5qWAy6wbxQ=; b=i8eol7WWD35bL5P0qWsYSLUOW2YTMw2DJY+wOOUSurUBU+bWWYPFzvtNi3zlW+wEhP O+/f/Z8dX2XmBe2LtwUUrpBQQ241Ds6YxrQIJrJqb0qBVQ2/aD8+FUdxctOSllb+GS5S Yh39xno0Sn8By57n+MO2gWwVCNz4Z7qYzkqlpQPPvpGg9KP/r9HwRh5upbQqC5A7MrL9 S1Ma5BgcO7bITff9j3X8zh9QxV/cl5pxxOH5mMDsUBSps0WjhTxCYXW9Xj3RCs9q+bcT D9NCU49GS+6gvNWEW94mR472Wj7WBxeYQATnsea8feHd2cglWH2tJkR6wHD1sLItlIJc 3nRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=GJOz3/fUi3hN/mG1KbRzAvv/7oJlEnmCw5qWAy6wbxQ=; b=jEGsmfI6rc/I1VqfEy35Vwst97HAjNnqx+hg5DTV343US/L3lS3ywp6wwSSTsrQ2BT ryscrIwn+9MaeULkB8achZRj6ITYt28OhvQep9esthm4rK/ZvfOYjqWWoQzJLT0Mrmqc GCkimnjNJXyyMHNu1nm6y03dor+5uBdA/nxSDBZvz5OcSsts8FUz/9A4xgnaJzO++m2p INli/nEYyZZA7D+Q+BX5gMe4QsGVNRemCUIWhjOzNw1U2CET8t7as2V38funZRnyhnXp mShixPHaoCANbgOqb3kloOlUHyNxqMiymyGtFRMsTgIIH8Ge7R/liQrj1DmrSN7EAhNA xAsQ== X-Gm-Message-State: APzg51BdU15QSGAVDlfEZ/DHOydxowuKr1sP/EmWOSWaRXnCuzKSyUb0 441D5bZxHWJzqffscHaz/FoT0g== X-Google-Smtp-Source: ANB0VdZpInyYk9slLF73zvX+tOhS/ro+SFo1KkuxUqD/BAcJFKZ2z8ZU95w1deXKT8XDf0dXosEKtA== X-Received: by 2002:a6b:9895:: with SMTP id a143-v6mr19463254ioe.290.1535958387151; Mon, 03 Sep 2018 00:06:27 -0700 (PDT) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id q19-v6sm5455389ith.1.2018.09.03.00.06.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Sep 2018 00:06:26 -0700 (PDT) Received: by pryzbyj (Postfix, from userid 1000) id 0DC828058C8; Mon, 3 Sep 2018 02:06:24 -0500 (CDT) Date: Mon, 3 Sep 2018 02:06:24 -0500 From: Justin Pryzby To: Mariel Cherkassky Cc: PostgreSQL mailing lists Subject: Re: trying to delete most of the table by range of date col Message-ID: <20180903070624.GA11702@telsasoft.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Mon, Sep 03, 2018 at 09:27:52AM +0300, Mariel Cherkassky wrote: > I'm trying to find the best way to delete most of the table but not all of it > according to a range of dates. > Indexes: > "end_date_idx" btree (end_date) > Referenced by: > TABLE "table1" CONSTRAINT "application_change_my_table_id_fkey" FOREIGN > KEY (my_table_id) REFERENCES my_table(id) > TABLE "table2" CONSTRAINT "configuration_changes_my_table_id_fkey" > FOREIGN KEY (my_table_id) REFERENCES my_table(id) ... > As you can see alot of other tables uses the id col as a foreign key which > make the delete much slower. > Trigger for constraint table1: time=14730.816 calls=1572864 > Trigger for constraint table2: time=30718.084 calls=1572864 > Trigger for constraint table3: time=28170.363 calls=1572864 ... Do the other tables have indices on their referencING columns ? https://www.postgresql.org/docs/devel/static/ddl-constraints.html#DDL-CONSTRAINTS-FK "Since a DELETE of a row from the referenced table [...] will require a scan of the referencing table for rows matching the old value, it is often a good idea to index the referencing columns too." Note, I believe it's planned in the future for foreign keys to support referenes to partitioned tables, at which point you could just DROP the monthly partition...but not supported right now. Justin