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 1uC1PX-009Wkj-UK for pgsql-hackers@arkaria.postgresql.org; Mon, 05 May 2025 19:23:32 +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 1uC1PW-005EqU-Vg for pgsql-hackers@arkaria.postgresql.org; Mon, 05 May 2025 19:23:30 +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 1uC1PW-005EqM-I3 for pgsql-hackers@lists.postgresql.org; Mon, 05 May 2025 19:23:30 +0000 Received: from mail-il1-x133.google.com ([2607:f8b0:4864:20::133]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uC1PT-000JDh-2N for pgsql-hackers@lists.postgresql.org; Mon, 05 May 2025 19:23:29 +0000 Received: by mail-il1-x133.google.com with SMTP id e9e14a558f8ab-3cfce97a3d9so24474175ab.2 for ; Mon, 05 May 2025 12:23:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1746473008; x=1747077808; darn=lists.postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=JHQoPobbYWw1+Inxxgh+sB2rk00U9MReMyqc7EC5Ts8=; b=LW5bpvLpgBuSLXTWm5tJCQehKQcNFL8cCDHPBuDw6Zg6y4cMq3gjaCIM2mDJEtR5sz NnjHZVhj3cpiAkI8pmQIx2zbKESmLczwtrHe+zd5StHiwym7Zj4ID8VvzaWqDjWZ56Xx tJ6bnUDvHyRJTqlTx+pEWuBOi1pSRfVtKOcLrijmiobQLhXItszZd/GZvqPpkoiht1qr 9SHOsKiAJo8dSMYxsD6QCH1YkGMmHF54piPVIV3OU/3EQ8Na1GhUxIOQw4/LUi3zjHqx jN7xRXb6oSacMJHfNUJ/gqjhpvr45cx/ZVfeBkszK17upXc4mvTDQIGYFB+iRY0uT71c lmcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746473008; x=1747077808; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=JHQoPobbYWw1+Inxxgh+sB2rk00U9MReMyqc7EC5Ts8=; b=HcjvVgBBLF8L+MUv+IZJEtLmF91xZQJNzzcutsWuDbKi8cgp0vfr/UHCW1ZF7aXshG oXPWBb6BTX9QBMkIARUuWwrJDWr7V7d7w4mLD7ub1pSv3ifYJ8s0xiisEyyUZ8mEXtsE luU216/Ed2zL+CYqYUq4/hlOA8iMXqkMUNxPma2kYT1mVSqUg+g9ClwfnWWWLaCrTsHv YHxrFu8i1OMeg24was001thgmXtMvMQm9DkPVuUwWaVCo5MTyLbpOjrJQLO/ak7c+BC2 F5pPB1Eq7GXlRL0h7Sq4pFfRwb8AzmlkrHBrxgSLH/LN/MAEy70ssz42o1VSIqGxOfrj bAAA== X-Forwarded-Encrypted: i=1; AJvYcCXhdFzwhbMlLWqE3S3akmE3cWzAe1RZLqwtcJwx13xqsYyAOt3Wz7D9SqHCStQBfmA15RgfYEMPLLv5WbJt@lists.postgresql.org X-Gm-Message-State: AOJu0Yw0BqDOHUsQpGq1ETrVIjKTWdVtzX45AWQvrfZdv54gkoTDxmTk 5mM0XilcTZx3tisxWjwWwgOGZ2uo7WAeGuCOMJ47KWLFZ8YAdbPY X-Gm-Gg: ASbGncuww1cHsERfwqJ03nG9ZgcLMfZP8FsR4ofdjor/g3iP/sOxqwdiqKe7wLDlpon cbBhxHN8wm3/eGQ6f3eIRhX9HhGAp02sGJiOQdYNJPrMOU/QXRiTrX97w5hXqzwjkzq7jVJNYez hq8JxXs9dJlmicBRXZDEuT17PGK5aXx8ritxLKp1O09Jq1Hz3z6bFCBc2WcxLbZaVSgv9mdK16t ryQeS/de51YqH9mVKU3nt3nySma7TD9FUtEbXj487wNrTFUrY/qKKLm0lDycMgeLRbKH40QvVc+ mH4rLa5Xcjj3aCxz8pnNXo6s3/0Zis/HTPNgmEoU2VR79rqVT5ggQ/Z67CR/wZ9sOuby0vE75x5 4eO+4jQ97wKDyRJx4uBzdS80fad7EdPLmlUJY X-Google-Smtp-Source: AGHT+IFsLkrme73Vq9XLGpq6kTMwTRABq/9POINPTkmHyD+29mGCeZO2Uwa/ANODw6NibvLskUguww== X-Received: by 2002:a05:6e02:1a26:b0:3d6:d162:be12 with SMTP id e9e14a558f8ab-3da5b3437e9mr90169395ab.21.1746473007606; Mon, 05 May 2025 12:23:27 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4f88aa8d6b7sm1908946173.118.2025.05.05.12.23.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 May 2025 12:23:26 -0700 (PDT) Date: Mon, 5 May 2025 14:23:25 -0500 From: Nathan Bossart To: Tom Lane Cc: Hannu Krosing , PostgreSQL Hackers Subject: Re: Horribly slow pg_upgrade performance with many Large Objects Message-ID: References: <4044567.1744128814@sss.pgh.pa.us> <4047312.1744130263@sss.pgh.pa.us> <4050387.1744132029@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="1L9BvNMT6l66k1TO" Content-Disposition: inline In-Reply-To: <4050387.1744132029@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --1L9BvNMT6l66k1TO Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Apr 08, 2025 at 01:07:09PM -0400, Tom Lane wrote: > Nathan Bossart writes: >> I do think it's worth considering going back to copying >> pg_largobject_metadata's files for upgrades from v16 and newer. > > (If we do this) I don't see why we'd need to stop at v16. I'm > envisioning that we'd use COPY, which will be dealing in the > text representation of aclitems, and I don't think that's changed > in a long time. The sort of thing that would break it is changes > in the set of available/default privilege bits for large objects. > > That is, where the dump currently contains something like > > SELECT pg_catalog.lo_create('2121'); > ALTER LARGE OBJECT 2121 OWNER TO postgres; > GRANT ALL ON LARGE OBJECT 2121 TO joe; > > we'd have > > COPY pg_largeobject_metadata FROM STDIN; > ... > 2121 10 {postgres=rw/postgres,joe=rw/postgres} > ... > > and some appropriate COPY data for pg_shdepend too. Attached is a proof-of-concept grade patch for using COPY for pg_largeobject_metadata and the relevant pg_shdepend entries. On my laptop, pg_upgrade with 10M LOs (each with a non-bootstrap-superuser owner and another role with SELECT rights) goes from ~8.5 minutes to ~1 minute with this patch. I originally set out to invent a new lo_create_with_owner() function and teach pg_dump to batch those together in large groups, but as I started the required pg_dump surgery, I was quickly scared away by the complexity. Next, I gave COPY a try. The improvements from using COPY will likely be limited to the pg_upgrade case, but that's the only case I regularly hear complaints about for zillions of large objects, so maybe it's good enough for now. For the COPY approach, I modified pg_dump to dump the contents of pg_largeobject_metadata. This is easy enough, but I ran into problems with the dependent comments and security labels. It turns out that even before v12, we run all the lo_create() commands just so that creating the comments and security labels works (AFAICT). So I suspect upgrading with many large objects has always been slow. The comment/security label dependency issue can be fixed (at least well enough for the tests) by moving PRIO_LARGE_OBJECT below PRIO_TABLE_DATA. There might be an existing issue here, because dbObjectTypePriorities has the following comment: * NOTE: object-type priorities must match the section assignments made in * pg_dump.c; that is, PRE_DATA objects must sort before DO_PRE_DATA_BOUNDARY, * POST_DATA objects must sort after DO_POST_DATA_BOUNDARY, and DATA objects * must sort between them. But dumpLO() puts large objects in SECTION_DATA, and PRIO_LARGE_OBJECT is before PRIO_PRE_DATA_BOUNDARY. I admittedly haven't spent too much time investigating this, though. In any case, it might be a good idea to also make sure we explicitly mark the large objects and their comments/seclabels as dependent on the pg_largeobject_metadata data. That leaves pg_shdepend. For now, I've just instructed pg_upgrade to COPY the relevant pg_shdepend rows as an independent step, but perhaps there's a reasonably straightforward way to put that in pg_dump, too. -- nathan --1L9BvNMT6l66k1TO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0001-pg_upgrade-Use-COPY-for-large-object-metadata.patch From 59fa6558a9258719840e4d423c86313937156f95 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Mon, 5 May 2025 14:11:56 -0500 Subject: [PATCH v1 1/1] pg_upgrade: Use COPY for large object metadata. --- src/bin/pg_dump/pg_backup_archiver.c | 5 +++++ src/bin/pg_dump/pg_dump.c | 16 +++++++++++++++- src/bin/pg_dump/pg_dump_sort.c | 2 +- src/bin/pg_dump/t/002_pg_dump.pl | 4 +++- src/bin/pg_upgrade/dump.c | 9 +++++++++ src/bin/pg_upgrade/pg_upgrade.c | 9 +++++++++ 6 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c index afa42337b11..0d61155ec0a 100644 --- a/src/bin/pg_dump/pg_backup_archiver.c +++ b/src/bin/pg_dump/pg_backup_archiver.c @@ -2974,6 +2974,11 @@ _tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH) int res = REQ_SCHEMA | REQ_DATA; RestoreOptions *ropt = AH->public.ropt; + if (ropt->binary_upgrade && + strcmp(te->tag, "pg_largeobject_metadata") == 0 && + strcmp(te->namespace, "pg_catalog") == 0) + return REQ_DATA; + /* These items are treated specially */ if (strcmp(te->desc, "ENCODING") == 0 || strcmp(te->desc, "STDSTRINGS") == 0 || diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index e2e7975b34e..312e1010456 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -1083,6 +1083,20 @@ main(int argc, char **argv) if (!dopt.dumpData && dopt.sequence_data) getTableData(&dopt, tblinfo, numTables, RELKIND_SEQUENCE); + if (dopt.binary_upgrade) + { + for (int i = 0; i < numTables; i++) + { + if (tblinfo[i].relkind == RELKIND_RELATION && + strcmp(tblinfo[i].dobj.name, "pg_largeobject_metadata") == 0 && + strcmp(tblinfo[i].dobj.namespace->dobj.name, "pg_catalog") == 0) + { + makeTableDataInfo(&dopt, &(tblinfo[i])); + break; + } + } + } + /* * In binary-upgrade mode, we do not have to worry about the actual LO * data or the associated metadata that resides in the pg_largeobject and @@ -3925,7 +3939,7 @@ getLOs(Archive *fout) * pg_largeobject_metadata, after the dump is restored. */ if (dopt->binary_upgrade) - loinfo->dobj.dump &= ~DUMP_COMPONENT_DATA; + loinfo->dobj.dump &= ~(DUMP_COMPONENT_DATA | DUMP_COMPONENT_ACL | DUMP_COMPONENT_DEFINITION); /* * Create a "BLOBS" data item for the group, too. This is just a diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c index 0b0977788f1..538e7dcb493 100644 --- a/src/bin/pg_dump/pg_dump_sort.c +++ b/src/bin/pg_dump/pg_dump_sort.c @@ -76,10 +76,10 @@ enum dbObjectTypePriorities PRIO_TABLE_ATTACH, PRIO_DUMMY_TYPE, PRIO_ATTRDEF, - PRIO_LARGE_OBJECT, PRIO_PRE_DATA_BOUNDARY, /* boundary! */ PRIO_TABLE_DATA, PRIO_SEQUENCE_SET, + PRIO_LARGE_OBJECT, PRIO_LARGE_OBJECT_DATA, PRIO_STATISTICS_DATA_DATA, PRIO_POST_DATA_BOUNDARY, /* boundary! */ diff --git a/src/bin/pg_dump/t/002_pg_dump.pl b/src/bin/pg_dump/t/002_pg_dump.pl index 55d892d9c16..2417f537ac1 100644 --- a/src/bin/pg_dump/t/002_pg_dump.pl +++ b/src/bin/pg_dump/t/002_pg_dump.pl @@ -1029,6 +1029,7 @@ my %tests = ( test_schema_plus_large_objects => 1, }, unlike => { + binary_upgrade => 1, no_large_objects => 1, no_owner => 1, schema_only => 1, @@ -1517,6 +1518,7 @@ my %tests = ( test_schema_plus_large_objects => 1, }, unlike => { + binary_upgrade => 1, schema_only => 1, schema_only_with_statistics => 1, no_large_objects => 1, @@ -4524,9 +4526,9 @@ my %tests = ( no_schema => 1, section_data => 1, test_schema_plus_large_objects => 1, - binary_upgrade => 1, }, unlike => { + binary_upgrade => 1, no_large_objects => 1, no_privs => 1, schema_only => 1, diff --git a/src/bin/pg_upgrade/dump.c b/src/bin/pg_upgrade/dump.c index 23cb08e8347..e5b73b7e7a0 100644 --- a/src/bin/pg_upgrade/dump.c +++ b/src/bin/pg_upgrade/dump.c @@ -15,6 +15,9 @@ void generate_old_dump(void) { + char *path = psprintf("%s/lo_shdep.out", realpath(log_opts.dumpdir, NULL)); + PQExpBuffer buf = createPQExpBuffer(); + PGconn *conn; int dbnum; prep_status("Creating dump of global objects"); @@ -70,5 +73,11 @@ generate_old_dump(void) ; end_progress_output(); + + conn = connectToServer(&old_cluster, "template1"); + appendStringLiteralConn(buf, path, conn); + PQclear(executeQueryOrDie(conn, "COPY (SELECT * FROM pg_shdepend WHERE classid = 'pg_largeobject'::regclass) TO %s", buf->data)); + destroyPQExpBuffer(buf); + PQfinish(conn); check_ok(); } diff --git a/src/bin/pg_upgrade/pg_upgrade.c b/src/bin/pg_upgrade/pg_upgrade.c index 536e49d2616..7a6234d48c0 100644 --- a/src/bin/pg_upgrade/pg_upgrade.c +++ b/src/bin/pg_upgrade/pg_upgrade.c @@ -571,6 +571,8 @@ prepare_new_globals(void) static void create_new_objects(void) { + char *path = psprintf("%s/lo_shdep.out", realpath(log_opts.dumpdir, NULL)); + PQExpBuffer buf = createPQExpBuffer(); int dbnum; PGconn *conn_new_template1; @@ -688,6 +690,13 @@ create_new_objects(void) ; end_progress_output(); + + conn_new_template1 = connectToServer(&new_cluster, "template1"); + appendStringLiteralConn(buf, path, conn_new_template1); + PQclear(executeQueryOrDie(conn_new_template1, "COPY pg_shdepend FROM %s", buf->data)); + destroyPQExpBuffer(buf); + PQfinish(conn_new_template1); + check_ok(); /* -- 2.39.5 (Apple Git-154) --1L9BvNMT6l66k1TO--