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 1s9aXL-003QUN-PO for pgsql-hackers@arkaria.postgresql.org; Wed, 22 May 2024 01:13:01 +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 1s9aXL-00DSC0-CI for pgsql-hackers@arkaria.postgresql.org; Wed, 22 May 2024 01:12:59 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s9aXK-00DSBs-Sl for pgsql-hackers@lists.postgresql.org; Wed, 22 May 2024 01:12:59 +0000 Received: from mail-ot1-x32a.google.com ([2607:f8b0:4864:20::32a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s9aXD-001NIF-IO for pgsql-hackers@postgresql.org; Wed, 22 May 2024 01:12:57 +0000 Received: by mail-ot1-x32a.google.com with SMTP id 46e09a7af769-6f0f7ec684cso2533284a34.0 for ; Tue, 21 May 2024 18:12:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=j-davis-com.20230601.gappssmtp.com; s=20230601; t=1716340371; x=1716945171; darn=postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=wI4o0j/EAenP3dM0RkDREyFdZJq5LrH5Ijg6XMBxuUk=; b=UZidFfXUNq18O9TW5tKdMyMhbaMgH3v7CcgWQufLoMi/eCgVhJrcjgsdj0QDVEh320 Uns/oVmu4Y9xnQICd+pDNBp/N8IuhNNniiF0cIo/dqDSK541H+diS7uEvQV6CLRkluNu GM9tFpzZyw8aVNSwPPSuAlwNfskdjMMkYzRSW/L5eVpsG2ADJZgDBDFqIPqC9Qp8VgKj /nXq/+cy9Afj0JhNVl5Aq96iZSwc8kUBqIGf/dDBGgCQOzwAFv8KLL3N/cZO6KvQGdPJ nEvXSkY9z4erXI3//Q/U5LbUgO0baMKzimEB+KgUYcRrXA3PakABysTvB+Z8yOCZZk4O xWXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716340371; x=1716945171; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=wI4o0j/EAenP3dM0RkDREyFdZJq5LrH5Ijg6XMBxuUk=; b=G725ZDifScZ1RAKJtfRaIyLa6AVpbXnX7YJk+gcSdAw63PCN8OwXsG4wRGAgMtJKKo MYsesuZf3QA9gpPMc61b9Ct9UBRHbuaONLIoVYWnyLJXAY0F/TaijQbqmAYxjLXgXBMj Hm63mT8Ee4PvDtD2fC4mkj29xKsKnUrwzvymuA5OlsoxcoqPlEga08STvQpeMbD9Dd5m uZnF6kBig6RIWwARX9YhyNLvRQAm0cKqZR/YaotY2VqouIt5wwjArmZdtxVdijcGxzT2 CiFTn4vXnolZkGAaR+9srP0ztRDhegQtND9MaZp9mwYs/gV2ufbrA/DGNYr/QrPLq2tn /ozA== X-Forwarded-Encrypted: i=1; AJvYcCVYnDbyXeLCl1hOWe4i4KzGtDS+0O245vMwPcmEGgm7VkIITC1HOqTZzK1TB4r7mTgYgzmG3oLcLqBYecZ1kOzYCuYrDKQFw9Byhxlv X-Gm-Message-State: AOJu0Yy1Yv5Q3dDqlXiLc68MKYgQXSWJhVvVxzxi6Uiq0tcr03yeHbuF QlvoxMSvwmhRv44qL4fntaOPVnwNCR1Pa0r7iqkITxSgUOzeXTe8KSNMAJ8rlRGkc7KHR0QF9f0 = X-Google-Smtp-Source: AGHT+IELGTh/+l9XBrtlqPvTeNiomg7ie6R5IpnUpvjasmu+nte0agDDmP2FMkiOpK1L/hbuQrxSyA== X-Received: by 2002:a05:6871:28a6:b0:24c:5cd6:6401 with SMTP id 586e51a60fabf-24c68b4e8f5mr616106fac.13.1716340370771; Tue, 21 May 2024 18:12:50 -0700 (PDT) Received: from [172.18.10.46] ([12.126.244.130]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-6759e74575fsm319466a12.54.2024.05.21.18.12.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 18:12:50 -0700 (PDT) Message-ID: Subject: Re: Skip adding row-marks for non target tables when result relation is foreign table. From: Jeff Davis To: SAIKIRAN AVULA , pgsql-hackers@postgresql.org Date: Tue, 21 May 2024 18:12:49 -0700 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.44.4-0ubuntu2 MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 2024-05-06 at 23:10 +0100, SAIKIRAN AVULA wrote: > I would like to bring to your attention an observation regarding the > planner's behavior for foreign table update/delete operations. It > appears that the planner adds rowmarks (ROW_MARK_COPY) for non-target > tables, which I believe is unnecessary when using the postgres-fdw. > This is because postgres-fdw performs early locking on tuples > belonging to the target foreign table by utilizing the SELECT FOR > UPDATE clause. I agree with your reasoning here. If it reads the row with SELECT FOR UPDATE, what's the purpose of row marks? The cost of ROW_MARK_COPY is that it brings the whole tuple along rather than a reference. I assume you are concerned about wide tables involved in the join or is there another concern? > In an attempt to address this, I tried implementing late locking. For others in the thread, see: https://www.postgresql.org/docs/current/fdw-row-locking.html > However, this approach still doesn't work as intended because the API > assumes that foreign table rows can be re-fetched using TID (ctid). > This assumption is invalid for partitioned tables on the foreign > server. It looks like it's a "Datum rowid", but is currently only allowed to be a ctid, which can't identify the partition. I wonder how much work it would be to fix this? > Additionally, the commit afb9249d06f47d7a6d4a89fea0c3625fe43c5a5d, > which introduced late locking for foreign tables, mentions that the > benefits of late locking against a remote server are unclear, as the > extra round trips required are likely to outweigh any potential > concurrency improvements. The extra round trip only happens when EPQ finds a newer version of the tuple, which should be the exceptional case. I'm not sure how this balances out, but to me late locking still seems preferable. Early locking is a huge performance hit in some cases (locking many more rows than necessary). Early locking is also a violation of the documentation here: "When a locking clause appears at the top level of a SELECT query, the rows that are locked are exactly those that are returned by the query; in the case of a join query, the rows locked are those that contribute to returned join rows." https://www.postgresql.org/docs/current/sql-select.html#SQL-FOR-UPDATE-SHAR= E > To address this issue, I have taken the initiative to create a patch > that prevents the addition of rowmarks for non-target tables when the > target table is using early locking. I would greatly appreciate it if > you could review the patch and provide any feedback or insights I may > be overlooking. A couple comments: * You're using GetFdwRoutineForRelation() with makecopy=3Dfalse, and then closing the relation. If the rd_fdwroutine was already set previously, then the returned pointer will point into the relcache, which may be invalid after closing the relation. I'd probably pass makecopy=3Dtrue and then free it. (Weirdly if you pass makecopy=3Dfalse, you may or may not get a copy, so there's no way to know whether to free it or not.) * Core postgres doesn't really choose early locking. If RefetchForeignRow is not defined, then late locking is impossible, so it assumes that early locking is happening. That assumption is true for postgres_fdw, but might not be for other FDWs. What if an FDW doesn't do early locking and also doesn't define RefetchForeignRow? Regards, Jeff Davis