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 1seCDi-004pJP-6o for pgsql-bugs@arkaria.postgresql.org; Wed, 14 Aug 2024 11:31: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 1seCDg-009idO-Dd for pgsql-bugs@arkaria.postgresql.org; Wed, 14 Aug 2024 11:31: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 1seCDf-009idG-P0 for pgsql-bugs@lists.postgresql.org; Wed, 14 Aug 2024 11:31:12 +0000 Received: from relay7-d.mail.gandi.net ([2001:4b98:dc4:8::227]) by magus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1seCDb-004o2x-10 for pgsql-bugs@lists.postgresql.org; Wed, 14 Aug 2024 11:31:11 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id 38B5120004; Wed, 14 Aug 2024 11:31:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vondra.me; s=gm1; t=1723635063; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9UX7Vj94b9Wqzzkil2q2c0XGqzP9kwExzt8XqP42vvc=; b=cE93ja8lar6MGj7WOMi68f6XxnjXt68Xa2aDwQ2n9bFn6RB2gCeMJ0lXRvDkpF4cJlgl9P U63OSeRFLB9RPz7ni98MjO7lBQ7afKAZoAjbxZCHVvkM9aRRCx5awsCYVLfeEP5ApFv7yQ CQiyJBW02rYg6gaQf4nErxIFZq26b+wMmqMIPXAGhkrCCS29CpLEwniOtI8iE8NFCRuymz 9RCMQCTmLwEegeQHLqkCAB1wPvc+vBmKLhfWkGksG7bsXZUFGO44hCMi1HHhnhcehuF1AY x7xg8lfB4DKgw8wqfJwc59MHyT2QIiLoRm4CdGHawLZ2RxeGpWxa0DqZAC6UDQ== Message-ID: <1ee2d452-45c8-4512-bc4b-773b0ec5d0a0@vondra.me> Date: Wed, 14 Aug 2024 13:31:02 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: FDW INSERT batching can change behavior From: Tomas Vondra To: git@jasonk.me, pgsql-bugs@lists.postgresql.org References: <20240809030755.jubqv6f6vpxkfkzv@jasonk.me> <900ad3fe-151e-4876-a399-fa12faa2c653@vondra.me> Content-Language: en-US In-Reply-To: <900ad3fe-151e-4876-a399-fa12faa2c653@vondra.me> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-GND-Sasl: tomas@vondra.me List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 8/13/24 20:31, Tomas Vondra wrote: > Hi, > > I took a closer look at this, planning to way to fix this, but I think > it's actually a bit worse than reported - both in impact and ways how to > fix that. > > The problem is it's not really specific to DEFAULT values. The exact > same issue exists whenever the insert uses the expressions directly. > That is, if you do this: > > > insert into grem1 (a) values (counter()), (counter()), > (counter()), (counter()), > (counter()); > > it will misbehave in exactly the same way as with the default values. Of > course, this also means that my original idea to disable batching if the > foreign table has (volatile) expression in the DEFAULT value won't fly. > > This can happen whenever the to-be-inserted rows have any expressions. > But those expressions are evaluated *outside* ModifyTable - in the nodes > that produce the rows. In the above example it's ValueScan. But it could > be any other node. For example: > > insert into grem1 (a) select counter() from generate_series(1,5); > > does that in a subquery. But AFAICS it could be any other node. > > Ideally we'd simply set batch_size=1 for those cases, but at this point > I have no idea how to check this from ModifyTable :-( > > In retrospect the issue is pretty obvious, yet I haven't thought about > this while working on the batching. This is embarrassing. > I've been thinking about this a bit more, and I'm not really sure using counter() as a default value can ever be "correct". The problem is it's inherently broken with concurrency - even with no batching, it'll fail if two of these inserts run at the same time. The batching only makes that more obvious / easier to hit, but it's not really the root cause. regards -- Tomas Vondra