Skip to content

Commit 46cad8b

Browse files
pg_upgrade: Parallelize retrieving loadable libraries.
This commit makes use of the new task framework in pg_upgrade to parallelize retrieving the names of all libraries referenced by non-built-in C functions. This step will now process multiple databases concurrently when pg_upgrade's --jobs option is provided a value greater than 1. Reviewed-by: Daniel Gustafsson, Ilya Gladyshev Discussion: https://postgr.es/m/20240516211638.GA1688936%40nathanxps13
1 parent 7baa36d commit 46cad8b

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

src/bin/pg_upgrade/function.c

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@ library_name_compare(const void *p1, const void *p2)
4242
((const LibraryInfo *) p2)->dbnum);
4343
}
4444

45+
/*
46+
* Private state for get_loadable_libraries()'s UpgradeTask.
47+
*/
48+
struct loadable_libraries_state
49+
{
50+
PGresult **ress; /* results for each database */
51+
int totaltups; /* number of tuples in all results */
52+
};
53+
54+
/*
55+
* Callback function for processing results of query for
56+
* get_loadable_libraries()'s UpgradeTask. This function stores the results
57+
* for later use within get_loadable_libraries().
58+
*/
59+
static void
60+
process_loadable_libraries(DbInfo *dbinfo, PGresult *res, void *arg)
61+
{
62+
struct loadable_libraries_state *state = (struct loadable_libraries_state *) arg;
63+
64+
AssertVariableIsOfType(&process_loadable_libraries, UpgradeTaskProcessCB);
65+
66+
state->ress[dbinfo - old_cluster.dbarr.dbs] = res;
67+
state->totaltups += PQntuples(res);
68+
}
4569

4670
/*
4771
* get_loadable_libraries()
@@ -54,47 +78,41 @@ library_name_compare(const void *p1, const void *p2)
5478
void
5579
get_loadable_libraries(void)
5680
{
57-
PGresult **ress;
5881
int totaltups;
5982
int dbnum;
6083
int n_libinfos;
84+
UpgradeTask *task = upgrade_task_create();
85+
struct loadable_libraries_state state;
86+
char *query;
6187

62-
ress = (PGresult **) pg_malloc(old_cluster.dbarr.ndbs * sizeof(PGresult *));
63-
totaltups = 0;
88+
state.ress = (PGresult **) pg_malloc(old_cluster.dbarr.ndbs * sizeof(PGresult *));
89+
state.totaltups = 0;
6490

65-
/* Fetch all library names, removing duplicates within each DB */
66-
for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
67-
{
68-
DbInfo *active_db = &old_cluster.dbarr.dbs[dbnum];
69-
PGconn *conn = connectToServer(&old_cluster, active_db->db_name);
91+
query = psprintf("SELECT DISTINCT probin "
92+
"FROM pg_catalog.pg_proc "
93+
"WHERE prolang = %u AND "
94+
"probin IS NOT NULL AND "
95+
"oid >= %u",
96+
ClanguageId,
97+
FirstNormalObjectId);
7098

71-
/*
72-
* Fetch all libraries containing non-built-in C functions in this DB.
73-
*/
74-
ress[dbnum] = executeQueryOrDie(conn,
75-
"SELECT DISTINCT probin "
76-
"FROM pg_catalog.pg_proc "
77-
"WHERE prolang = %u AND "
78-
"probin IS NOT NULL AND "
79-
"oid >= %u;",
80-
ClanguageId,
81-
FirstNormalObjectId);
82-
totaltups += PQntuples(ress[dbnum]);
83-
84-
PQfinish(conn);
85-
}
99+
upgrade_task_add_step(task, query, process_loadable_libraries,
100+
false, &state);
101+
102+
upgrade_task_run(task, &old_cluster);
103+
upgrade_task_free(task);
86104

87105
/*
88106
* Allocate memory for required libraries and logical replication output
89107
* plugins.
90108
*/
91-
n_libinfos = totaltups + count_old_cluster_logical_slots();
109+
n_libinfos = state.totaltups + count_old_cluster_logical_slots();
92110
os_info.libraries = (LibraryInfo *) pg_malloc(sizeof(LibraryInfo) * n_libinfos);
93111
totaltups = 0;
94112

95113
for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
96114
{
97-
PGresult *res = ress[dbnum];
115+
PGresult *res = state.ress[dbnum];
98116
int ntups;
99117
int rowno;
100118
LogicalSlotInfoArr *slot_arr = &old_cluster.dbarr.dbs[dbnum].slot_arr;
@@ -129,7 +147,8 @@ get_loadable_libraries(void)
129147
}
130148
}
131149

132-
pg_free(ress);
150+
pg_free(state.ress);
151+
pg_free(query);
133152

134153
os_info.num_libraries = totaltups;
135154
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy