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.96) (envelope-from ) id 1vWmG3-009ENd-05 for pgsql-hackers@arkaria.postgresql.org; Sat, 20 Dec 2025 01:59:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vWmG1-009RH6-1v for pgsql-hackers@arkaria.postgresql.org; Sat, 20 Dec 2025 01:59:46 +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.96) (envelope-from ) id 1vWmG1-009RGx-0v for pgsql-hackers@lists.postgresql.org; Sat, 20 Dec 2025 01:59:46 +0000 Received: from mail-lf1-x133.google.com ([2a00:1450:4864:20::133]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vWmFz-001h0z-03 for pgsql-hackers@lists.postgresql.org; Sat, 20 Dec 2025 01:59:45 +0000 Received: by mail-lf1-x133.google.com with SMTP id 2adb3069b0e04-598eaafa587so2588812e87.3 for ; Fri, 19 Dec 2025 17:59:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766195977; x=1766800777; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=mlXcQvrOR92rLoa606bcRZDKIU7/RU3PwRcUntIBuno=; b=PaQFztWlGoDQcLuxzDYb9QRsGVXLyEK480quXyfCKuHyQBzQ2GVm8MKAFlvf5mDqL8 fxbdy8Ifgux74q7JWulz+XIF5E8EJxudKm/wJAzJsXmHgtE7RDvRLPey1KzebvpKeU2E vYAXz/Zn1mojuwFNSOio2XQ5NE0bHPkF9NhiKfzXgTbDcFLv6ti2m3qWBtwXvC+a2kU0 MN5TlzVAR6lzObfyj8LbwQENgtul4p+b5nKOzBUJ99DEux+MjT/HPP4kTdMNKmIr7YV2 7oAeIlv8nPk9i/5dZsJrR71i4EQLG+xtNXFDmtzNo/4QuqmS3Aj+Thq9GJAHSOK6IeD5 sxHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766195977; x=1766800777; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=mlXcQvrOR92rLoa606bcRZDKIU7/RU3PwRcUntIBuno=; b=sxUHaAIYPnmbnD5xpn614lC8H8L1lAXRORb7jc4iOrEC9JmQ+4xRkfnD3u2eB0xOCA I4PLL0rqGvrhWZuPnufdNxheJootBOe+xIqzABely8wE922qEMFyQV5iK7+4S+ERIR8u XAraZg5GW7gkHmFhbvRFQC1LZ11FlyXKN8M/5nZ+S61kJEP27Yi02eGiRhUR6ASi6T21 PAMj4erBoqFYFPIdnCYklGqv6JcfxvEUIX6UKHLbbbmP81pLqIrkd39s9VxAAGUcperu m61I2htQCA8RDPL8Rgj8OZkWGGeKR0/u61bxs9j2ZargiYUvtVzeI8KubHzqdqISeNIe 1L2w== X-Gm-Message-State: AOJu0YyJ822TAq69bOJFFG+5orotSUGEvq8AXobMCq5kuDtv/vniRXLw hp6VVvvIsqaaq8qlmUIOPTqO2zPof4iaMqZeqJqdtJPFYV4CZ+Eh9ES+GOmMdsUMfa3PI+b+IEg YqDpjd7k66Cly8AP3cjOb0Hv78QMQZZ0c7Fsn X-Gm-Gg: AY/fxX4P0aXW7/bTwyL7ThkMVY3kFHGDGoF0gm+G8VOXUiO0m1lqZC4ejVpQbOaJcBy /tJZaC6zE+bj4PLq6WvfoTYlhOrO2oqtSs7jHyASDxtxack4261nsTMWf1662Y7BIFw6u5Qpjy1 wLhVNtCb3NrdvlBcICZqE4YJvDDd0mwyXHXQDm/HaFNiE6aoFnPbU8Ilh6eJ9pemwPLz5cFL38U ZEkfGwKzTfQl4s51PrjOmAusQ560PaJJiuXok9MUGY2ZqYMmMYjlnG8VXXwkmGAbWsGaUOh X-Google-Smtp-Source: AGHT+IFPR4PMnCsWjCiqytYykPCtOLi+T/ucgtb85D6TTn7ZwKcq9OEfDtvMIROPbkkYXxMsavNUkJUpZ1hDvvrBDi0= X-Received: by 2002:a05:6512:690:b0:594:51ac:13e with SMTP id 2adb3069b0e04-59a17d95799mr1561461e87.17.1766195976252; Fri, 19 Dec 2025 17:59:36 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Masahiko Sawada Date: Fri, 19 Dec 2025 17:58:59 -0800 X-Gm-Features: AQt7F2p8D0aUCEHkgxti7ZNKXmLuKirPw8KV0NfjTQyk-Pi7r38bCtQWF3wcKIs Message-ID: Subject: Re: Initial COPY of Logical Replication is too slow To: Marcos Pegoraro Cc: PostgreSQL Hackers Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On Sat, Dec 6, 2025 at 4:19=E2=80=AFAM Marcos Pegoraro = wrote: > > Subscriber needs to ask publisher about tables and fields to COPY and it = uses pg_get_publication_tables for that, and it is too slow when the number= of tables is high because on every table it's subscribed it has to run thi= s select. Yeah, if we pass a publication that a lot of tables belong to to pg_get_publication_tables(), it could take a long time to return as it needs to construct many entries. > We can get the same result with a join on pg_publication_rel. You changed the query not to use pg_get_publication_tables(): - "SELECT DISTINCT" - " (CASE WHEN (array_length(gpt.attrs, 1) =3D c.relnatts)" - " THEN NULL ELSE gpt.attrs END)" - " FROM pg_publication p," - " LATERAL pg_get_publication_tables(p.pubname) gpt," - " pg_class c" - " WHERE gpt.relid =3D %u AND c.oid =3D gpt.relid" - " AND p.pubname IN ( %s = )", - lrel->remoteid, - pub_names->data); + "SELECT CASE WHEN cardinality(r.prattrs) <> relnatts THEN " + "r.prattrs END FROM pg_clas= s c " + "LEFT JOIN LATERAL (SELECT DISTINCT prattrs FROM " + "pg_publication_rel r INNER JOIN pg_publication p " + "ON p.oid =3D r.prpubid WHERE c.oid =3D r.prrelid AND " + "pubname in ( %s )) r ON TRUE WHERE c.oid =3D %u", + pub_names->data, + lrel->remoteid); Simply replacing pg_get_publication_tables() with joining on pg_publication_rel doesn't work since pg_get_publication_tables() cares for several cases, for example where the specified columns are generated columns and the specified table is a partitioned table etc. Therefore the patch doesn't pass the regression tests. I think it would make more sense to introduce a dedicated SQL function that takes the reloid as well as the list of publications and returns the relation's the column list and row filter expression while filtering unnecessary rows inside the function. Regards, -- Masahiko Sawada Amazon Web Services: https://aws.amazon.com