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 1qAbyA-0002zC-FR for pgsql-performance@arkaria.postgresql.org; Sat, 17 Jun 2023 19:52:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1qAby8-0006uq-Dq for pgsql-performance@arkaria.postgresql.org; Sat, 17 Jun 2023 19:52:20 +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 1qAby8-0006uh-2o for pgsql-performance@lists.postgresql.org; Sat, 17 Jun 2023 19:52:20 +0000 Received: from mail-ed1-x52c.google.com ([2a00:1450:4864:20::52c]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qAby0-002kw1-Gj for pgsql-performance@lists.postgresql.org; Sat, 17 Jun 2023 19:52:18 +0000 Received: by mail-ed1-x52c.google.com with SMTP id 4fb4d7f45d1cf-51a2c8e5a2cso2793269a12.2 for ; Sat, 17 Jun 2023 12:52:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1687031529; x=1689623529; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=UknoH7VIyoPYne4RnxstA0ShspQCs2H4V95XApKx5d0=; b=chqRtV5kDE9/Xeop1tO8TY8xHjcVbQ0DMOIOP4pDQs+JXhtR577ZsXsxCBmORXDNc9 CUtW7Zfk7fPDS63yor+McFSR2MOsNsGlX3cVfjjWZjSjsq8tYUDoMyLtQf3X+JTTs6e9 SjZzR/VgGV5amKv2akadUsRDFdkJ+98P3gDPJu66mRRrS2M4EsJbaHFrRh+kf+l8+jSw VEmCAkyhtnikaqHVuVMoa06hQ9t0RkIQlLb1SvCcXx2hY4IijP5Vy6L2jesrLTs0AsVi JJVfA59dij9T1PqbAELb5kfRsZVIWOHx2OLZRLmuJbONRyupLyCfKiSD3OxZateMtysp RzHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687031529; x=1689623529; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=UknoH7VIyoPYne4RnxstA0ShspQCs2H4V95XApKx5d0=; b=j2HKXt3ty9WvfuNpPUIh6ofmdykhBeZ41Z3th37uWkl7t+Mk9i1QJzGWFyBt9474h2 XO7Trh4m3cBkas8Vq36VdH95ak9aRJmhqmyqSYyLOJG/a+GBktApB4zkulhtOH7pj+Dw ToxH8b6CocE0YlMBtLz05FtDvSVGWbVToZh9MWyFXGEEAJz5qnmedUhD1SPBCTfuEDlU yaVVIiFhdrqJ8vXy6OR+/zi8PP+dCx1XXQ+k74NM/a5XKTASYATLarxsfgtx3s3y1wfk z06RFijlLEIu1BvZUwiS3SJYqtfrdKUVgDfoIQVwQd0Z3Gn9mTMsu2VCwRyPfaasVj2I jD0w== X-Gm-Message-State: AC+VfDzDF/nwOhXkTngkegUTZSSxK7C3LeYgimfz1yaxZCW/GrFZkA0f ngNbcglZYTXYvNjm1XowoDkDF93gZ+q3tFltAZw= X-Google-Smtp-Source: ACHHUZ4IyP/YlPjtaNFl7PmCOmqsouRv6FR0XA/LqI+JJYlF45IuxVncQs0wQeY8boFdUXdq87OnYA== X-Received: by 2002:a17:907:16a2:b0:982:4abc:97d6 with SMTP id hc34-20020a17090716a200b009824abc97d6mr5563756ejc.4.1687031528877; Sat, 17 Jun 2023 12:52:08 -0700 (PDT) Received: from [10.137.0.17] (ip-86-49-228-220.bb.vodafone.cz. [86.49.228.220]) by smtp.gmail.com with ESMTPSA id lv16-20020a170906bc9000b00965a52d2bf6sm12313613ejb.88.2023.06.17.12.52.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 17 Jun 2023 12:52:08 -0700 (PDT) Message-ID: Date: Sat, 17 Jun 2023 21:52:07 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.1 Subject: Re: Merge David and Goliath tables efficiently Content-Language: en-US To: Nicolas Paris , pgsql-performance@lists.postgresql.org References: <28172511-8297-4E6D-9DD0-421B5CF5240A@riseup.net> From: Tomas Vondra In-Reply-To: <28172511-8297-4E6D-9DD0-421B5CF5240A@riseup.net> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 6/17/23 15:48, Nicolas Paris wrote: > In my use case I have a 2billion / 1To table. I have daily data to upsert around 2milion, with say 50% inserts, based on the primary key in a fresh analyzed table. > > I have tested multiple strategies to merge the data, all based on first stage to copy the 2m dataset in an staging unlogged / indexed table: > > 1. Join insert then join update > 2.1. Usage of the new merge statement > 2.2 Usage of merge on two hash partitioned tables wit partition wide join enabled > 3. Usage of merge by batch of 1000 rows > > First remark is the merge statement is almost 30% faster than two statements in my benchmarks. Thanks to the pg community for this. > > While the strategies 1 and 2.x are incredibly slow (canceled after 10 hours), the third one finishes within 30 minutes. > Seems pretty terrible, provided the data is on reasonable storage (with acceptable random I/O behavior). > My interpretation reading the query plan is: well sized small batches of upserts leverage the indexes while the regular join choose the sequential scan, including sorting and hashing which takes forever time and resources including disk. You may be right, but it's hard to tell without seeing the query plan. > > Sadly my incoming dataset is too small to benefit from a seq scan and too large to benefit from an index scan join. However when splited manuallyin N portions, the problem can be tackled with N * small cost, which is cheap anyway. > Sounds very much like you'd benefit from tuning some cost parameters to make the index scan look cheaper. > Questions: > 1. Is there another strategy ? > 2. Could postgres support a "batched indexed join itself", leveraging indexes itself by dynamic sized batches ? > Not sure what 'batched indexed join' would be, but it very much sounds like a nested loop with an index scan. > > It is error prone write code to split and iterate I suspect postgres has everything internally (indexes catalog, planner) to split itself the job, making David vs Goliath something trivial. > What PostgreSQL version are you using, what hardware? Did you tune it in any way, or is everything just default? regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company