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 1mhHhV-0007Wv-1M for pgsql-hackers@arkaria.postgresql.org; Sun, 31 Oct 2021 20:45:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mhHhT-0005Rt-VE for pgsql-hackers@arkaria.postgresql.org; Sun, 31 Oct 2021 20:45:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mhHhT-0005Ri-Dh for pgsql-hackers@lists.postgresql.org; Sun, 31 Oct 2021 20:45:07 +0000 Received: from mail-ed1-x52a.google.com ([2a00:1450:4864:20::52a]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mhHhL-0002SX-V2 for pgsql-hackers@lists.postgresql.org; Sun, 31 Oct 2021 20:45:06 +0000 Received: by mail-ed1-x52a.google.com with SMTP id j21so34953142edt.11 for ; Sun, 31 Oct 2021 13:44:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=message-id:date:mime-version:user-agent:subject:content-language:to :cc:references:from:in-reply-to:content-transfer-encoding; bh=ACltez+oNDqUXVJVhLc0gKBdeBUON6p0A7UM7Iw3t4o=; b=I72BtOXprNvZxlpNesgKNacI4HmcZVmCx//gsD7bOes5mr3Q7KiaefkjDWjDI36IYj zozHgHKeCQN2EuEMfk8LHUCEVJITPZVpKLncyrQOaGvPQFoMu0PuHw/P4ixkcrk4J6H5 LYOV/DG6E7VJAGdV+60ANiIGre931U26BgiTRZ5zJhk+JmIK3EXr2/KXK+jOfea2/2oS H0unxM8xYY+Dn3qnILZQ+qLppYCiFSU1H0IP77LlBeMBZ18olaunP3QRqjYELvWkn0ui 43FbWhqS0Yg81WyMfOVe0OW+7v+8p4f3hVhDZBGdrukE8lDUnvL2oFiwg+7gqcCpiPHG YEyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=ACltez+oNDqUXVJVhLc0gKBdeBUON6p0A7UM7Iw3t4o=; b=29p683FLa68rDsh6eheSMUoFwyv+BXARG7e8AtUEtcLz4/vBdXSPm+Y+u5ItaSdG/E id+DLYOYVSMxQKbaFyaNESjM/85b5S4G0FVOXCuoTXCZwmMotrBiPNIxmTW6FSVwKbp6 mkLzPoYyJFaYNDeNT3gXBiy+xi5ImrAVFqC44en3KHnnX3ToMZCS4ii5ziRkcvp8IzFS jRU+541zYciNwv2/dqHUAPIBStJMW/E/dPNnrjISBHagFXZOB1W3dj6Sr3K3mubgNU8j m8gtR2c2vO4mWSJFjTvg30lWPJ/6heGs1UiVuDcuok2fGgdfwm53t9r+wHILhN47neD9 YQQA== X-Gm-Message-State: AOAM530WNwyGi3VnDtI9aTgOXiy/p93LDBBCC66Sk2OWLkjS+5bpq72n yXot48DrQHoYRQbEGo2gmA1H7G1fUbspOpgxwMX68tREnAm3co5UPgaHw2fK29h1q6mMo3A7vcz UyXDU7moYMmpUUU2TEgniXEOuR60JoQrUo2AZ8bNTJ0S92bak/94CxerYJRNBpMeQ8ooTcBZphW ubDL2xIZJuugFVnAkXAcYKSI69toWA7nymnOZNpTuFimhkUbIyBZNJ X-Google-Smtp-Source: ABdhPJx0XkOPlr3NuYS0ddPMLp5gsUfM3ZUMHqwaFMZ/Ax4dTKGuB9HL7Y1KYhd3M3QY0NS3n6scjA== X-Received: by 2002:a17:906:1146:: with SMTP id i6mr32512833eja.12.1635713098091; Sun, 31 Oct 2021 13:44:58 -0700 (PDT) Received: from [10.137.0.18] (ip-86-49-251-104.net.upcbroadband.cz. [86.49.251.104]) by smtp.gmail.com with ESMTPSA id h20sm3248466eds.88.2021.10.31.13.44.56 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 31 Oct 2021 13:44:57 -0700 (PDT) Message-ID: Date: Sun, 31 Oct 2021 21:44:53 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.1.0 Subject: Re: logical decoding and replication of sequences Content-Language: en-US To: Hannu Krosing Cc: Peter Eisentraut , vignesh C , PostgreSQL Hackers , Petr Jelinek References: <377c6fda-c3eb-7a69-e981-c77ae459c5db@enterprisedb.com> <5105c3ad-d726-fc22-1d09-514b6852a15e@enterprisedb.com> <3005299b-267d-8e4a-68a3-abdad3e96aba@enterprisedb.com> <3d6df331-5532-6848-eb45-344b265e0238@enterprisedb.com> <482c10c1-b110-819f-b0e7-17549fa2f0dc@enterprisedb.com> <12a9ecd6-4edb-a367-33da-4e2d03b0aace@enterprisedb.com> From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed 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 On 9/25/21 22:05, Hannu Krosing wrote: > Just a note for some design decisions > >> 1) By default, sequences are treated non-transactionally, i.e. sent to the output plugin right away. > > If our aim is just to make sure that all user-visible data in > *transactional* tables is consistent with sequence state then one > very much simplified approach to this could be to track the results of > nextval() calls in a transaction at COMMIT put the latest sequence > value in WAL (or just track the sequences affected and put the latest > sequence state in WAL at commit which needs extra read of sequence but > protects against race conditions with parallel transactions which get > rolled back later) > Not sure. TBH I feel rather uneasy about adding more stuff in COMMIT. > This avoids sending redundant changes for multiple nextval() calls > (like loading a million-row table with sequence-generated id column) > Yeah, it'd be nice to have to optimize this a bit, somehow. But I'd bet it's a negligible amount of data / changes, compared to the table. > And one can argue that we can safely ignore anything in ROLLBACKED > sequences. This is assuming that even if we did advance the sequence > paste the last value sent by the latest COMMITTED transaction it does > not matter for database consistency. > I don't think we can ignore aborted (ROLLBACK) transactions, in the sense that you can't just discard the increments. Imagine you have this sequence of transactions: BEGIN; SELECT nextval('s'); -- allocates new chunk of values ROLLBACK; BEGIN; SELECT nextval('s'); -- returns one of the cached values COMMIT; If you ignore the aborted transaction, then the sequence increment won't be replicated -- but that's wrong, because user now has a visible sequence value from that chunk. So I guess we'd have to maintain a cache of sequences incremented in the current session, do nothing in aborted transactions (i.e. keep the contents but don't log anything) and log/reset at commit. I wonder if multiple sessions make this even more problematic (e.g. due to session just disconnecting mid transansaction, without writing the abort record at all). But AFAICS that's not an issue, because the other session has a separate cache for the sequence. > It can matter if customers just call nextval() in rolled-back > transactions and somehow expect these values to be replicated based on > reasoning along "sequences are not transactional - so rollbacks should > not matter" . > I don't think we guarantee anything for data in transactions that did not commit, so this seems like a non-issue. I.e. we don't need to go out of our way to guarantee something we never promised. > Or we may get away with most in-detail sequence tracking on the source > if we just keep track of the xmin of the sequence and send the > sequence info over at commit if it == current_transaction_id ? > Not sure I understand this proposal. Can you explain? regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company