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 1vkTEW-00FnqK-1J for pgsql-hackers@arkaria.postgresql.org; Mon, 26 Jan 2026 20:30:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vkTEU-00AgQb-1P for pgsql-hackers@arkaria.postgresql.org; Mon, 26 Jan 2026 20:30: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 1vkTEU-00AgQT-09 for pgsql-hackers@lists.postgresql.org; Mon, 26 Jan 2026 20:30:46 +0000 Received: from mail-lf1-x12e.google.com ([2a00:1450:4864:20::12e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vkTER-00000000b2Z-3Gya for pgsql-hackers@lists.postgresql.org; Mon, 26 Jan 2026 20:30:46 +0000 Received: by mail-lf1-x12e.google.com with SMTP id 2adb3069b0e04-59de66fdb53so3140294e87.2 for ; Mon, 26 Jan 2026 12:30:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1769459441; cv=none; d=google.com; s=arc-20240605; b=DW4inw1zCrkOJOzmOC3/cKepGEI+qqboFqvUofNoi3XRnBpF9ixsUlQ+GthidjUefB KlnIgJNa9xdQxfZ3154mzciv3bk2w7mHRH57NDRDUhBWX41YX07mueEWmC7VqXZD78jX EpoD63tthQhU+NGpHvb/5C1rfBZaww5wbo/DiUkAz0OLg8bGz4XuSFai4ChuFKMAoOrI IW7io2xwS1SrTNvfhxO+VnLFzRSMaHub7fX5JMT+3bQR1CDZ81bOcMW2D+8V2/LjvTUY UkeZh6SMwaIbj9ReFZwTrcqTVuiWInEQPf8clw0aoGcih1KS4lw7arZUIL/vyLpoHZRB iLqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=gnqfLG6OkjVQ1YU+47Tmk3tAYStFAfN50qRtNITNVKI=; fh=cwkwuxD2dvvlUMVELZog5sj2eIqdEPq1zCWDpGHoZSQ=; b=IBPu4wJmStq3S1NfWt5KPHEunyA8mc7OjH+rqNkZVGVgic6aHlAcPFQAr3XRKwK+y3 1G9qGP8MQ6yFIdHbw4ijOc6gBdiM3LfH5GaYKq6+IOGe6WtQh9iZh9Af2QYITAZf28WH SRA0e4kyTc/bt+Lwpm8a4uD5UMatffjPMJv4GBt0cNkTc+/Hqd7VZKJyVRrZAjak05SS YqR20/hCgfzAm/yggmFjXWYFpoUvznMQm1tAYtSrr7AS9kfK2WmZLP0VLascrL9XWSIS iiCdnpnaaNk5Pv+kqt0q/sMdyI9l/woDy8SAYQyv5vhgT/MpoRtBzRL/3dCT7cUprm8O 2+xQ==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769459441; x=1770064241; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=gnqfLG6OkjVQ1YU+47Tmk3tAYStFAfN50qRtNITNVKI=; b=V+x+J7gffsDlBhqLHxW5vyIalsaPlUd5ncBGgMZGiU/tq0JLoRLt4YDpyovuCB/he4 fOYCNIWWesEncQbh5qq+52wZamvqwDYTG0pO49d3YqPLk8LDh11GfcVYcIqSHzGdQmxV Kz6/+9KbloZ3ZOBn2rVsR6xuoe1uZ1ZBeF5+k101jGJBBJxoWP5L37vJmQ1dIUEUQTXc cweRpfk9tXL1OFakrhR2R/xhMjGkth+uRi9W4JxW5qkXBwecj5DeWkvb8IEfRXkp1QNM gBQo5ixcrH4qqT/Cm1VPqsaW8RzEFpDzpOIShjFLtnFy2M/ExLuSVO31AY+42C7tlxwQ pOAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769459441; x=1770064241; h=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=gnqfLG6OkjVQ1YU+47Tmk3tAYStFAfN50qRtNITNVKI=; b=v3GA0VfKca4ElAoNygH2Gl58L7nvG3hMvQItHFA0IS8BUthZxh8KlWkjALcYHjAnRB gCjF3i5zVA317xeT5AaDaRak6JqK0MrYKvrg4vtCv3JaSyYa2oq/JrycGDV71LMy4E5M FF/kwSluYbVn0y1ZYE71Q0CdCPu6qO40Ec+Hf1HSL0cLxgDFe8mP0oTjUpOyD3AuIN6h YDKi2YtTip1dTZIMGs/fg70bm9JSIUuMvvoNMKOVzU3fZT10nt+oJDC2+oM7Pg210DCp 5pFqZsucAx91Y0o3fNW2dIRGLeQCdhsPaBD0hifVUhT1+WRe/8oeroCM9eVKVZHhlUy+ N9Kw== X-Gm-Message-State: AOJu0YztOcNmGEESf4wTZuykWZcQDyh8i5rK0TQ9LUD6zymB40BXCZc/ 98OSFXPLbL+unD7oVRFrcel83aHs4h/Qf1l4qOorCqqMEcM5o6o6RiYPDAsPCfl+bHAi5dIqnxV if/eokqoG/y451MiPSy+KMR1H2NP0UrE= X-Gm-Gg: AZuq6aJlPQH7n9DWe/Rd/drKlgx+TEZOeEsOqAj12w0Vd3fEjcuC/5gy2oyKyUb0l7C P/8rwocavwa1XLUgE/eJL6kG4dvNbgsJ0LsRl5LhZ/kRn1ITJFUWrJ8GWfCX1RlKtM9CpYjxQiS l9RAyss5mbR5rby1hPb97b4nvGURhj4Viz2tZdjvH5GYuyhe2A44tFxMpr3qHIT9PNpX8BBES4P zWQ3HsPK0zAH7h1sdaE6wop81lSOTApZJ6GcxFYx8ZBQexxh2KuXChIgG72ivCFwzv7iUYe X-Received: by 2002:a05:6512:31d4:b0:59b:b3f1:cb6 with SMTP id 2adb3069b0e04-59df35f7fc3mr1700018e87.1.1769459440871; Mon, 26 Jan 2026 12:30:40 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Masahiko Sawada Date: Mon, 26 Jan 2026 12:30:03 -0800 X-Gm-Features: AZwV_QihCPLjNLjldmfxarwxR2T1ho-fkI7SXs8hNWWFle3IkgDV8Z4HBOYYOxU Message-ID: Subject: Re: Initial COPY of Logical Replication is too slow To: Marcos Pegoraro Cc: PostgreSQL Hackers Content-Type: multipart/mixed; boundary="0000000000006b0b000649506120" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000006b0b000649506120 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Jan 19, 2026 at 9:44=E2=80=AFAM Marcos Pegoraro = wrote: > > Em sex., 19 de dez. de 2025 =C3=A0s 22:59, Masahiko Sawada escreveu: >> >> 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. > > > Well, I don't know how to help but I'm sure it's working badly. > Today I added some fields on my server, then seeing logs I could see how = slow this process is. > > duration: 2213.872 ms statement: SELECT DISTINCT (CASE WHEN (array_leng= th(gpt.attrs, 1) =3D c.relnatts) THEN NULL ELSE gpt.attrs END) FROM pg_p= ublication p, LATERAL pg_get_publication_tables(p.pubname) gpt, pg_class = c WHERE gpt.relid =3D 274376788 AND c.oid =3D gpt.relid AND p.pubname IN = ( 'mypub' ) > > 2 seconds to get the list of fields of a table is really too slow. > How can we solve this ? 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->pubviaroot ? PUBLICATION_PART_ROOT = : PUBLICATION_PART_LEAF)= ; 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. Regards, -- Masahiko Sawada Amazon Web Services: https://aws.amazon.com --0000000000006b0b000649506120 Content-Type: application/octet-stream; name="sort_and_dedup.patch" Content-Disposition: attachment; filename="sort_and_dedup.patch" Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: f_mkvm61950 ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL2NhdGFsb2cvcGdfcHVibGljYXRpb24uYyBiL3NyYy9i YWNrZW5kL2NhdGFsb2cvcGdfcHVibGljYXRpb24uYwppbmRleCA5YTQ3OTFjNTczZS4uMmVhM2Q4 ZGQ5YTggMTAwNjQ0Ci0tLSBhL3NyYy9iYWNrZW5kL2NhdGFsb2cvcGdfcHVibGljYXRpb24uYwor KysgYi9zcmMvYmFja2VuZC9jYXRhbG9nL3BnX3B1YmxpY2F0aW9uLmMKQEAgLTExODMsNyArMTE4 MywxMCBAQCBwZ19nZXRfcHVibGljYXRpb25fdGFibGVzKFBHX0ZVTkNUSU9OX0FSR1MpCiAJCQkJ CQkJCQkJCQkJCQkJcHViX2VsZW0tPnB1YnZpYXJvb3QgPwogCQkJCQkJCQkJCQkJCQkJCVBVQkxJ Q0FUSU9OX1BBUlRfUk9PVCA6CiAJCQkJCQkJCQkJCQkJCQkJUFVCTElDQVRJT05fUEFSVF9MRUFG KTsKLQkJCQlwdWJfZWxlbV90YWJsZXMgPSBsaXN0X2NvbmNhdF91bmlxdWVfb2lkKHJlbGlkcywg c2NoZW1hcmVsaWRzKTsKKworCQkJCXB1Yl9lbGVtX3RhYmxlcyA9IGxpc3RfY29uY2F0KHJlbGlk cywgc2NoZW1hcmVsaWRzKTsKKwkJCQlsaXN0X3NvcnQocHViX2VsZW1fdGFibGVzLCBsaXN0X29p ZF9jbXApOworCQkJCWxpc3RfZGVkdXBsaWNhdGVfb2lkKHB1Yl9lbGVtX3RhYmxlcyk7CiAJCQl9 CiAKIAkJCS8qCg== --0000000000006b0b000649506120--