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 1vvMRp-00FZkA-0T for pgsql-hackers@arkaria.postgresql.org; Wed, 25 Feb 2026 21:29:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vvMRo-008g9I-01 for pgsql-hackers@arkaria.postgresql.org; Wed, 25 Feb 2026 21:29:32 +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 1vvMRn-008g9A-2K for pgsql-hackers@lists.postgresql.org; Wed, 25 Feb 2026 21:29:31 +0000 Received: from mail-vs1-xe2e.google.com ([2607:f8b0:4864:20::e2e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vvMRk-00000001HY4-2atE for pgsql-hackers@lists.postgresql.org; Wed, 25 Feb 2026 21:29:31 +0000 Received: by mail-vs1-xe2e.google.com with SMTP id ada2fe7eead31-5ff196450feso120835137.1 for ; Wed, 25 Feb 2026 13:29:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772054966; x=1772659766; darn=lists.postgresql.org; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=IRRjmeR+NeHZIxB1X9xqYxdTKHQd5BXkB2yt7Ud6qes=; b=BOkGvLSTkp2RgpagVJ7OQqEBn3hMXJVm/h3hywvVhtDj6P8Pkf+JvnGsS4QLYIc5cd 8xBfpxFH+NpZylIWm9tZ1Z+E6H+cEEInzfcPXzYNreaNgtxS0DUpMnGvNDZETEAA2lMw 2P3AhWIfWs2jsMxvoA+YkN4xiqpBagygPZeRrQkWYggOxiJUeZiUVYh3Z7XvoLWrkP+u XFWOetlSJ9M3035zmV0HFWrembxlP8Ez3EjtlBvfOG2V6hMmNo3Rs14wuywENNDc1c19 HX5078UZZr1C/uVJSfhNVKZ7wRG1/iYd5eN7nUktgh0R5l0oSEeOAW7T9OECR0Ujb7au NhLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772054966; x=1772659766; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-transfer-encoding:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=IRRjmeR+NeHZIxB1X9xqYxdTKHQd5BXkB2yt7Ud6qes=; b=E8tZXBsRJzJh1V0hVD8KrS2e1hVASJiuorgU6P74gEromoIZvT7LG31dVoEVEHaD7U vYyWHDA6bSWmmF7kbG+v1Jh3iQWi7x7pfVhdSQ84VhrQc7OWqAZ8Bjg/ZG+ZKVWZdjuI Y9eDZdmc3kGFh1zYhLOj8UKRh/piHQCS3+jp2AeCmPnSux5WrY902FCDJz4aoLm0+Pg3 GQKv4hIlB5SHB3I7ys7v3fCUftPLmDZqmTkdfbH/bBKTpfvP1rAhOPKVihgzHU1LEWxb KTtdVZfYTmHJ3SXtzPAJ1AOfKkJQSN243BaLDBP1YoDQ0/SS1zS+ueCiT+YJm/dg/KlI AV1w== X-Gm-Message-State: AOJu0YwiVLNy/P1ll2JVbcu2oL45IpVJa5GHV9EzDvXwOMvqbHFMr1GR 1ZMLXZn8lNHdODlxJ3Lm38GOFrZqZZgfvdB9FdMhhWQOousmg2Sr0DeON8F/xg== X-Gm-Gg: ATEYQzzDiDraEe2ybOnAXBL41bnO6oUv49qdKJ0xQ46+fueA2castcF6Fspo9vFJJ8Z hpWPb3BprFjaWyl2OtrqnUs4Itwcx7noTqsRlZnhyfa7KUdPze7phvvtgpyRCxX28Qhugh/sOhn hr4YeLg3b4ijY6FfNGsXFD42wpAjIq6pMflgqc2sGmtSBbzbcd9TFLkXgNPPa83Nm17uR1lmKNq 0JEiuxTP2yjwxGKwIJ8tNijDIL9+wcBX2TNZ4sWDiDVp4VReiWn6e7yIMIECaA4lJ9m00C5UQnB Z18PuZw9S5dfUot+bYU0xJ7EHyQaVc2lVlk3ypyTjpOHhNa/qLR5Vy99Ge2wuK0XtC3dIY8NuES x0biBdDDLkFetWWc4qLytpxDv8p4qU/+LBm1lGzwhEuF1FeFY7Kbl6qpxZ95CazKFXY5lkI9Ui+ PineSEfY2EKMaT19OSuAO8JZ5wIzRAXmm6EeuJL+GhoFEZfg== X-Received: by 2002:a05:6102:945:b0:5f1:b69b:f9e3 with SMTP id ada2fe7eead31-5ff1ce835famr227439137.12.1772054966483; Wed, 25 Feb 2026 13:29:26 -0800 (PST) Received: from localhost ([2804:14d:328a:a59c:a4b8:2a46:26e5:b6cc]) by smtp.gmail.com with ESMTPSA id ada2fe7eead31-5ff1e80818dsm274802137.5.2026.02.25.13.29.24 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 25 Feb 2026 13:29:26 -0800 (PST) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Wed, 25 Feb 2026 18:29:23 -0300 Message-Id: Cc: "PostgreSQL Hackers" Subject: Re: Initial COPY of Logical Replication is too slow From: "Matheus Alcantara" To: "Masahiko Sawada" , "Marcos Pegoraro" X-Mailer: aerc 0.21.0 References: In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi On Wed Feb 25, 2026 at 4:03 PM -03, Masahiko Sawada wrote: >> After more investigation of slowness, it seems that the >> list_concat_unique_oid() called below is quite slow when the database >> has a lot of tables to publish: >> >> relids =3D GetPublicationRelations(pub_elem->oid, >> pub_elem->pubviaroot ? >> PUBLICATION_PART_ROOT : >> PUBLICATION_PART_LEAF); >> schemarelids =3D GetAllSchemaPublicationRelations(pub_elem->oid, >> pub_elem->pubviaroo= t ? >> PUBLICATION_PART_RO= OT : >> PUBLICATION_PART_LE= AF); >> pub_elem_tables =3D list_concat_unique_oid(relids, schemarelids); >> >> This is simply because it's O(n^2), where n is the number of oids in >> schemarelids in the test case. A simple change would be to do sort & >> dedup instead. With the attached experimental patch, the >> pg_get_publication_tables() execution time gets halved in my >> environment (796ms -> 430ms with 50k tables). If the number of tables >> is not large, this method might be slower than today but it's not a >> huge regression. >> >> In the initial tablesync cases, it could be optimized further in a way >> that we introduce a new SQL function that gets the column list and >> expr of the specific table. This way, we can filter the result by >> relid at an early stage instead of getting all information and >> filtering by relid as the tablesync worker does today, avoiding >> overheads of gathering system catalog scan results. > > I've drafted this idea and I find it looks like a better approach. The > patch introduces the pg_get_publication_table_info() SQL function that > returns the column list and row filter expression like > pg_get_publication_tables() returns but it checks only the specific > table unlike pg_get_publication_tables(). On my env, the tablesync > worker's query in question becomes 0.6ms from 288 ms with 50k tables > in one publication. Feedback is very welcome. > Thanks for patch. I did a review and here are my comments: + values[0] =3D ObjectIdGetDatum(pub->oid); + values[1] =3D ObjectIdGetDatum(relid); + + values[0] =3D ObjectIdGetDatum(pub->oid); + values[1] =3D ObjectIdGetDatum(relid); Duplicated assignments? -------------- + /* ALL TALBES publication */ Typo on TALBES -------------- + * Common routine for pg_get_publication_tables() and + * pg_get_publication_table_info() to construct the result tuple. + */ +static HeapTuple +construct_published_rel_tuple(published_rel *table_info, TupleDesc tuple_d= esc) construct_published_rel_tuple is only being used on pg_get_publication_table_info(). Perhaps it can also be used on "if (funcctx->call_cntr < list_length(table_infos))" block on pg_get_publication_tables()? -------------- Is new regression tests needed or the current ones already cover the new function engouth? The code of pg_get_publication_table_info seems well coveraged. -------------- It seems that pgindent is missing on src/backend/catalog/pg_publication.c -- Matheus Alcantara EDB: https://www.enterprisedb.com