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 1oOzul-0007FJ-4W for pgsql-hackers@arkaria.postgresql.org; Fri, 19 Aug 2022 11:11:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oOzuj-0005dp-RH for pgsql-hackers@arkaria.postgresql.org; Fri, 19 Aug 2022 11:11:45 +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 1oOzui-0005df-Uu for pgsql-hackers@lists.postgresql.org; Fri, 19 Aug 2022 11:11:45 +0000 Received: from mail-wr1-x432.google.com ([2a00:1450:4864:20::432]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oOzub-00027u-EI for pgsql-hackers@lists.postgresql.org; Fri, 19 Aug 2022 11:11:43 +0000 Received: by mail-wr1-x432.google.com with SMTP id e20so4298117wri.13 for ; Fri, 19 Aug 2022 04:11:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=content-transfer-encoding:in-reply-to:references:to:from :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc; bh=Jp4FqmtXQwcOs6SZAC3Ycc1oylOlLM97Y3WaO8RiD24=; b=JsGBkb2v7HDTkC3XRKfR1v1szNUKOs3tv/RqYRCakeEdPQ3/Rv/okiXEl1Roe1f5LN hix1glRNVJl/sxg6RIK1isG8D4aWYACqRGE9J/ECQKKX8IcJ3oFIVtDQtTWvqThqtBCH q4zvcdjJPs9iz5hlcWDhpJLnsvqymf/DWkQZ2x268toNJHuTsa11DndGNBD8RGOhQiCM 781zjSR/0DvxJ+zaMvDL55NKc0a46ZVLLY2hD6YWph4lq0oUlU1CV7qkWPQ6GEl0KB+z K6Xe35qNTmgIRpUBH+IdakMs6rGKQIdok7y9XxczQr+cjRu3celMXXQUvOFFj3HwSVlx aAxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:references:to:from :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc; bh=Jp4FqmtXQwcOs6SZAC3Ycc1oylOlLM97Y3WaO8RiD24=; b=nOtYVy6tzHe+S1E3scVnOoG8+meQfeCDXLKcy96ttzGk4ZZx0x6Cm9dVGTRwhM2Z6P Is71ojCKX7dJ376UP4t67svn6UfL/Bkdg+IYFl5BQJBUo6KCG2dn2zjFJFa3gXOgkaH3 gs/lVrGfzgcFCdD8bF609ykz9FI1IHZ1TKsz7xRrO9JrIrCycJEW2WPERyuGvVybGF0t /HoieJWmemX/MiFmJgNmNZwoZpa8wThj2sAwGvuZ74+m+ngOCllxkelOra+Ce3odk6wi lBu70SKYgQ0B4eJFFiTdocFF51IOsGqE3nqyuQ71vXO63zUWIGYxQAqhLnl4pquAGLa9 2LuQ== X-Gm-Message-State: ACgBeo3vEY8HbYnudbQeSPXAh14LBbYxpiZxN2S/BVpWzK1/vYRV2u7I RMqBP7uLAEOMFCFFzYCkTIUhuvumV3bo9TjD0XwFobFMyT4YG+tpuK14XHZDeP3tUQU0/OihXDK VIxiWLdaTBowHCfYzE6qEkLWvtdGJYP32KmubDDoJT6X+3vDdkRx9QSz4S0i0Qqqvrznd6XtvRF A/q10XEqAn3LCfuTGq9s5WeDmqwfFvv+FXCGhBx2X6D7QW0mceFMzPU6JWGLlSrFyyUHtAsrU3D +somOZeG6bH+9XcU/inEP3+sV6Zm/Im2H9lbN241kwxOdX3ZUnBtA== X-Google-Smtp-Source: AA6agR7B2ix4AK6juvtcJM3ipJZOD5m9ZXW5GVcf2u6eNa0vpsJ+ZafRYSI2qiQ8oaeb5ToogzAMDg== X-Received: by 2002:a05:6000:1445:b0:225:3fde:5c40 with SMTP id v5-20020a056000144500b002253fde5c40mr212204wrx.625.1660907494735; Fri, 19 Aug 2022 04:11:34 -0700 (PDT) Received: from [10.137.0.17] (ip-86-49-229-52.bb.vodafone.cz. [86.49.229.52]) by smtp.gmail.com with ESMTPSA id e11-20020a056000120b00b002206236ab3dsm56458wrx.3.2022.08.19.04.11.34 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 19 Aug 2022 04:11:34 -0700 (PDT) Message-ID: Date: Fri, 19 Aug 2022 13:11:35 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 Subject: Re: logical decoding and replication of sequences, take 2 Content-Language: en-US From: Tomas Vondra To: PostgreSQL Hackers References: <76e5fcd8-8072-8ea2-d361-2e811941000c@enterprisedb.com> In-Reply-To: <76e5fcd8-8072-8ea2-d361-2e811941000c@enterprisedb.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk I've been thinking about the two optimizations mentioned at the end a bit more, so let me share my thoughts before I forget that: On 8/18/22 23:10, Tomas Vondra wrote: > > ... > > And maybe we could then use the LSN to read the increment from the WAL > during decoding, instead of having to read it and WAL-log it during > commit. Essentially, we'd run a local XLogReader. Of course, we'd have > to be careful about checkpoints, not sure what to do about that. > I think logging just the LSN is workable. I was worried about dealing with checkpoints, because imagine you do nextval() on sequence that was last WAL-logged a couple checkpoints back. Then you wouldn't be able to read the LSN (when decoding), because the WAL might have been recycled. But that can't happen, because we always force WAL-logging the first time nextval() is called after a checkpoint. So we know the LSN is guaranteed to be available. Of course, this would not reduce the amount of WAL messages, because we'd still log all sequences touched by the transaction. We wouldn't need to read the state from disk, though, and we could ignore "old" stuff in decoding (with LSN lower than the last LSN we decoded). For frequently used sequences that seems like a win. > Another idea that just occurred to me is that if we end up having to > read the sequence state during commit, maybe we could at least optimize > it somehow. For example we might track LSN of the last logged state for > each sequence (in shared memory or something), and the other sessions > could just skip the WAL-log if their "local" LSN is <= than this LSN. > Tracking the last LSN for each sequence (in a SLRU or something) should work too, I guess. In principle this just moves the skipping of "old" increments from decoding to writing, so that we don't even have to write those into WAL. We don't even need persistence, nor to keep all the records, I think. If you don't find a record for a given sequence, assume it wasn't logged yet and just log it. Of course, it requires a bit of shared memory for each sequence, say ~32B. Not sure about the overhead, but I'd bet if you have many (~thousands) frequently used sequences, there'll be a lot of other overhead making this irrelevant. Of course, if we're doing the skipping when writing the WAL, maybe we should just read the sequence state - we'd do the I/O, but only in fraction of the transactions, and we wouldn't need to read old WAL in logical decoding. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company