Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1jHqGG-0000oG-AD for pgsql-performance@arkaria.postgresql.org; Fri, 27 Mar 2020 14:47:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jHqGF-0008Nx-0j for pgsql-performance@arkaria.postgresql.org; Fri, 27 Mar 2020 14:47:03 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1jHqGE-0008Np-Ju for pgsql-performance@lists.postgresql.org; Fri, 27 Mar 2020 14:47:02 +0000 Received: from mail-wm1-x343.google.com ([2a00:1450:4864:20::343]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jHqGB-0004Fg-9P for pgsql-performance@postgresql.org; Fri, 27 Mar 2020 14:47:01 +0000 Received: by mail-wm1-x343.google.com with SMTP id e9so699163wme.4 for ; Fri, 27 Mar 2020 07:46:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=mjm89UUhcz8+DhH/kO63IzfzzvsPIcXT0vY3oH+KIwc=; b=VcpGK4ME/6/a/DVR+LtMzS1ulVqSw36JwS3T4Tau0Mt8l9pdB96kuEFlIkLR8gmFoA NAFchAW8UsMtsFOxiq70HlNhkloCwG05dW1hGbJ7041p2H0ETZS0ozMPgIRfvJeAfLXa wpHO19gByuIjnYjjEBKRfYDZQ9gOrnIzOMt+lTe5gnztsoULgoUeP1oGwdhIiQbeE1Np O4KVetHONCyrFzdQvi9mJHnrdxuc1bdXokh40jjEA/MAjN7hbl7RXfBlKxQpUdFjnr/8 gCe7caNH1VegmO2KzkECiN3MyCsHPW/l7KR+t5Yv/EoTImtnYisvkI7QaXDldDxwf/a4 6DNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=mjm89UUhcz8+DhH/kO63IzfzzvsPIcXT0vY3oH+KIwc=; b=ntoK5DWGA960m8XJP8DtqddIVFMifRUZKMoxBlex2efESyIsfZLY1rXLsRMibbiOTd HMJoYuY/Qs1kGjG3osppZsgjM22hguFJbrxIsbmktsFT68pZsATx3i/8r7UBGTUOA49G Ts1r2MwW6VqSlxMM3i5yQmM/VeMrZHTdrAzGQFOBPRCDRzikRxKSGnvZuX4OH95ttXo+ NO8Q5BiR9N8MlI9AwIarVeQ2FR6yOfSAFCv21ba4EYov1kBAB1elF4Uf9dEziKLWIl5/ PTaVhv1fr2ja5SXMsbyOfdbP0GpSEe1/5EDsvxgoQVgeIyUmie4UaXQ8q9I33gV76csV C7JA== X-Gm-Message-State: ANhLgQ0WNdty7mQJVoAOMcM25qg2vrR+3ECrG9LmsJC5yz1R8WQqOnBy cuIf9GWt8X9vIYCCseBIVvSp2A== X-Google-Smtp-Source: ADFU+vuc8noL71bLadbtWu68Grab0/LJ/4LuDHAun9P+SYzUSPUuUpe5ro4M0+V8QDp/AGlxZei0uw== X-Received: by 2002:a05:600c:20a:: with SMTP id 10mr5957489wmi.135.1585320418227; Fri, 27 Mar 2020 07:46:58 -0700 (PDT) Received: from dynamic-el0du9wub2u44fyro-pd01.res.v6.highway.a1.net (dynamic-el0du9wub2u44fyro-pd01.res.v6.highway.a1.net. [2001:871:60:1f7:9f45:635d:673f:d1c4]) by smtp.gmail.com with ESMTPSA id 71sm9097868wrc.53.2020.03.27.07.46.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Mar 2020 07:46:57 -0700 (PDT) Message-ID: <5e44d4e8ae73193efa82fb2744dbd95cc5130669.camel@cybertec.at> Subject: Re: Best way to delete big amount of records from big table From: Laurenz Albe To: Ekaterina Amez , pgsql-performance@postgresql.org Date: Fri, 27 Mar 2020 15:46:57 +0100 In-Reply-To: <6e456b07-a396-f9e3-6a22-23a96b73420c@zunibal.com> References: <6e456b07-a396-f9e3-6a22-23a96b73420c@zunibal.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.32.5 (3.32.5-1.fc30) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, 2020-03-27 at 15:13 +0100, Ekaterina Amez wrote: > I'm trying to clean up a database with millions of records of > useless-but-don't-remove-just-in-case data. [...] > > But also I'm cleaning tables with 150million records where I'm going to > remove 60% of existing data and after a few tests I'm not sure what's > the best approach as all seem to take similar time to run. These tables > are grouped in 4 tables group with master, detail, master_history, > detail_history structure. None of the tables have primary key nor > foreign key or any constraint but the sequence used for what should be > the PK column, though this column is not defined as PK. You should define primary and foreign keys if you can, but I guess I don't have to tell you that. > I've decided to delete from the last one in chunks (10 days of data per > chunk but it coud be any other quantity) so I've created a function. > I've tested it with indexes (in master_hist for filtering data and in > detail_hist for the fk and pk), without indexes, after analyzing table, > and no matter what I always end up with more or less the same execution > time. I can afford the time it's getting to run but I'd like to know if > it's there a better way to do this. There is no need to delete in batches unless you have a need to keep transactions short (danger of deadlock because the data are still modified, or you cannot afford to block autovacuum that long). If you can drop the indexes while you do it (downtime), go for it. Perhaps there is a way to use partial indexes that exclude all the data that you have to delete, then work could go on as normal. > I'm testing on version 9.2 BUT > production server is 8.4 (legacy application, supposed to be in at least > 9.2 but recently discovered it was 8.4, planning upgrade but not now). > Config parameters are default ones. Now that is a seriously bad idea. You should test on the same version as you have running in production. And you should insist in an upgrade. People who insist in running ancient software often insist in ancient hardware as well, and both is a good way to get data corruption. If the system blows up, they are going to blame you. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com