Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qWDKz-002UCy-T0 for pgsql-hackers@arkaria.postgresql.org; Wed, 16 Aug 2023 10:01:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qWDKy-005Sa5-FI for pgsql-hackers@arkaria.postgresql.org; Wed, 16 Aug 2023 10:01:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qWDKy-005SWz-4F for pgsql-hackers@lists.postgresql.org; Wed, 16 Aug 2023 10:01:12 +0000 Received: from mail-ej1-x62c.google.com ([2a00:1450:4864:20::62c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qWDKt-000S7f-0P for pgsql-hackers@lists.postgresql.org; Wed, 16 Aug 2023 10:01:10 +0000 Received: by mail-ej1-x62c.google.com with SMTP id a640c23a62f3a-99c1f6f3884so861521666b.0 for ; Wed, 16 Aug 2023 03:01:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1692180064; x=1692784864; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=Lx6M1r3u7e0TpbjUiEox1wiI1npbSwfOF9GeA1Qly/s=; b=jdzbyh6/NLV+VXvYs7scuewQGakLIzS7/6HabqdlNIhNa9GZmSXfJrsDe2g7TDW7KX US9m2o7NbcSFWmjtP5xOToVa1ak1ZqJ/X+cNNDmAdckQBk+Ms3yIJoSHvOYzoPNPv7vN KZwbO88cSej1/ifaQp+xocfTqn2Ern3TgstgFCG6SJCoPS/H1SVO2fUnDAJfFtI5wU1g cYIdLJ+2u4jcNKTJ4R6gBUgvxUSt2laFj8V2yH4sHUY1u0RCzjQl+rIOpgx43WevgBqH 8GxliwBmQDuyKyUzM8daFsB8+wgOurxQfxznGSDCSXgziuGade6glfKCIdI2lK1t1Sa7 osRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692180064; x=1692784864; h=content-transfer-encoding:in-reply-to:from:references:cc: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=Lx6M1r3u7e0TpbjUiEox1wiI1npbSwfOF9GeA1Qly/s=; b=i/YXgVGUO3YQ8bA7IOaW26QzohlCPIoA/j+T7H9ddsF1hl3ubUtuw6YnPy7mSvELsG T5DLOd8dnb5TFyxH7G5WlP5orbsSakw00lr4GrShucvVM+HAjNyDMa++bNTr+JuZy8Xp S7Ft6E3793WSDLSM0+7otgj7HgI+M1v7MZisCg1wqIruUi7LtqkBR0hsdqS5VNW1Fqgy 2UjyxB2kY6oA4+dDjlIIgRUvSeV7wK87HyfmrDTGxFvW5nRoyAlD3mA9NgyLt5EuJEny TvYxFtqWEcLhUs00KtLW4p/gnjx/Utm8V9P5/4DS3cySacdAMvUz+qgteR6QXBBmdNve adCQ== X-Gm-Message-State: AOJu0Yz3XjKx+K+Og/W+dfOng1tcqUPBbAyEpT3optUiRy98NRntJbhX fIEKpFZ7rOrJoTjElEcpwtrEHA== X-Google-Smtp-Source: AGHT+IExhGvK9slxWg5q6NBKcBygklc9aoEH7/93q96LERI+IRNrfS/uPYLjE7QI/aUAeXWGHLwd+A== X-Received: by 2002:a17:906:76c8:b0:99b:5689:1331 with SMTP id q8-20020a17090676c800b0099b56891331mr1057475ejn.59.1692180063679; Wed, 16 Aug 2023 03:01:03 -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 u14-20020a17090657ce00b00992ed412c74sm8220707ejr.88.2023.08.16.03.01.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 16 Aug 2023 03:01:03 -0700 (PDT) Message-ID: Date: Wed, 16 Aug 2023 12:01:01 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Subject: Re: pg_logical_emit_message() misses a XLogFlush() Content-Language: en-US To: Andres Freund Cc: Michael Paquier , Postgres hackers References: <4e680b60-9bce-7c4a-0419-f478a30d30e3@enterprisedb.com> <20230816003333.7hn2rx5m2l7una3d@awork3.anarazel.de> <619e219c-5894-cfd0-5634-0d465c41130b@enterprisedb.com> <20230816041336.4tsksa2qcudfgcc6@awork3.anarazel.de> From: Tomas Vondra In-Reply-To: <20230816041336.4tsksa2qcudfgcc6@awork3.anarazel.de> 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 8/16/23 06:13, Andres Freund wrote: > Hi, > > On 2023-08-16 03:20:53 +0200, Tomas Vondra wrote: >> On 8/16/23 02:33, Andres Freund wrote: >>> Hi, >>> >>> On 2023-08-16 06:58:56 +0900, Michael Paquier wrote: >>>> On Tue, Aug 15, 2023 at 11:37:32AM +0200, Tomas Vondra wrote: >>>>> Shouldn't the flush be done only for non-transactional messages? The >>>>> transactional case will be flushed by regular commit flush. >>>> >>>> Indeed, that would be better. I am sending an updated patch. >>>> >>>> I'd like to backpatch that, would there be any objections to that? >>> >>> Yes, I object. This would completely cripple the performance of some uses of >>> logical messages - a slowdown of several orders of magnitude. It's not clear >>> to me that flushing would be the right behaviour if it weren't released, but >>> it certainly doesn't seem right to make such a change in a minor release. >>> >> >> So are you objecting to adding the flush in general, or just to the >> backpatching part? > > Both, I think. I don't object to adding a way to trigger flushing, but I think > it needs to be optional. > > >> IMHO we either guarantee durability of non-transactional messages, in which >> case this would be a clear bug - and I'd say a fairly serious one. I'm >> curious what the workload that'd see order of magnitude of slowdown does >> with logical messages I've used it, but even if such workload exists, would >> it really be enough to fix any other durability bug? > > Not sure what you mean with the last sentence? > Sorry, I meant to write "enough not to fix any other durability bug". That is, either we must not lose messages, in which case it's a bug and we should fix that. Or it's acceptable for the intended use cases, in which case there's nothing to fix. To me losing messages seems like a bad thing, but if the users are aware of it and are fine with it ... I'm simply arguing that if we conclude this is a durability bug, we should not leave it unfixed because it might have performance impact. > I've e.g. used non-transactional messages for: > > - A non-transactional queuing system. Where sometimes one would dump a portion > of tables into messages, with something like > SELECT pg_logical_emit_message(false, 'app:', to_json(r)) FROM r; > Obviously flushing after every row would be bad. > > This is useful when you need to coordinate with other systems in a > non-transactional way. E.g. letting other parts of the system know that > files on disk (or in S3 or ...) were created/deleted, since a database > rollback wouldn't unlink/revive the files. > > - Audit logging, when you want to log in a way that isn't undone by rolling > back transaction - just flushing every pg_logical_emit_message() would > increase the WAL flush rate many times, because instead of once per > transaction, you'd now flush once per modified row. It'd basically make it > impractical to use for such things. > > - Optimistic locking. Emitting things that need to be locked on logical > replicas, to be able to commit on the primary. A pre-commit hook would wait > for the WAL to be replayed sufficiently - but only once per transaction, not > once per object. > How come the possibility of losing messages is not an issue for these use cases? I mean, surely auditors would not like that, and I guess forgetting locks might be bad too. > >> Or perhaps we don't want to guarantee durability for such messages, in >> which case we don't need to fix it at all (even in master). > > Well, I can see adding an option to flush, or perhaps a separate function to > flush, to master. >> >> The docs are not very clear on what to expect, unfortunately. It says >> that non-transactional messages are "written immediately" which I could >> interpret in either way. > > Yea, the docs certainly should be improved, regardless what we end up with. > regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company