Reducing connection overhead in pg_upgrade compat check phase

Started by Daniel Gustafssonabout 3 years ago34 messageshackers
Jump to latest
#1Daniel Gustafsson
daniel@yesql.se

When adding a check to pg_upgrade a while back I noticed in a profile that the
cluster compatibility check phase spend a lot of time in connectToServer. Some
of this can be attributed to data type checks which each run serially in turn
connecting to each database to run the check, and this seemed like a place
where we can do better.

The attached patch moves the checks from individual functions, which each loops
over all databases, into a struct which is consumed by a single umbrella check
where all data type queries are executed against a database using the same
connection. This way we can amortize the connectToServer overhead across more
accesses to the database.

In the trivial case, a single database, I don't see a reduction of performance
over the current approach. In a cluster with 100 (empty) databases there is a
~15% reduction in time to run a --check pass. While it won't move the earth in
terms of wallclock time, consuming less resources on the old cluster allowing
--check to be cheaper might be the bigger win.

--
Daniel Gustafsson

Attachments:

0001-pg_upgrade-run-all-data-type-checks-per-connection.patchapplication/octet-stream; name=0001-pg_upgrade-run-all-data-type-checks-per-connection.patch; x-unix-mode=0644Download+373-421
#2Nathan Bossart
nathandbossart@gmail.com
In reply to: Daniel Gustafsson (#1)
Re: Reducing connection overhead in pg_upgrade compat check phase

On Fri, Feb 17, 2023 at 10:44:49PM +0100, Daniel Gustafsson wrote:

In the trivial case, a single database, I don't see a reduction of performance
over the current approach. In a cluster with 100 (empty) databases there is a
~15% reduction in time to run a --check pass. While it won't move the earth in
terms of wallclock time, consuming less resources on the old cluster allowing
--check to be cheaper might be the bigger win.

Nice! This has actually been on my list of things to look into, so I
intend to help review the patch. In any case, +1 for making pg_upgrade
faster.

--
Nathan Bossart
Amazon Web Services: https://aws.amazon.com

#3Nathan Bossart
nathandbossart@gmail.com
In reply to: Daniel Gustafsson (#1)
Re: Reducing connection overhead in pg_upgrade compat check phase

On Fri, Feb 17, 2023 at 10:44:49PM +0100, Daniel Gustafsson wrote:

When adding a check to pg_upgrade a while back I noticed in a profile that the
cluster compatibility check phase spend a lot of time in connectToServer. Some
of this can be attributed to data type checks which each run serially in turn
connecting to each database to run the check, and this seemed like a place
where we can do better.

The attached patch moves the checks from individual functions, which each loops
over all databases, into a struct which is consumed by a single umbrella check
where all data type queries are executed against a database using the same
connection. This way we can amortize the connectToServer overhead across more
accesses to the database.

This change consolidates all the data type checks, so instead of 7 separate
loops through all the databases, there is just one. However, I wonder if
we are leaving too much on the table, as there are a number of other
functions that also loop over all the databases:

* get_loadable_libraries
* get_db_and_rel_infos
* report_extension_updates
* old_9_6_invalidate_hash_indexes
* check_for_isn_and_int8_passing_mismatch
* check_for_user_defined_postfix_ops
* check_for_incompatible_polymorphics
* check_for_tables_with_oids
* check_for_user_defined_encoding_conversions

I suspect consolidating get_loadable_libraries, get_db_and_rel_infos, and
report_extension_updates would be prohibitively complicated and not worth
the effort. old_9_6_invalidate_hash_indexes is only needed for unsupported
versions, so that might not be worth consolidating.
check_for_isn_and_int8_passing_mismatch only loops through all databases
when float8_pass_by_value in the control data differs, so that might not be
worth it, either. The last 4 are for supported versions and, from a very
quick glance, seem possible to consolidate. That would bring us to a total
of 11 separate loops that we could consolidate into one. However, the data
type checks seem to follow a nice pattern, so perhaps this is easier said
than done.

IIUC with the patch, pg_upgrade will immediately fail as soon as a single
check in a database fails. I believe this differs from the current
behavior where all matches for a given check in the cluster are logged
before failing. I wonder if it'd be better to perform all of the data type
checks in all databases before failing so that all of the violations are
reported. Else, users would have to run pg_upgrade, fix a violation, run
pg_upgrade again, fix another one, etc.

--
Nathan Bossart
Amazon Web Services: https://aws.amazon.com

#4Justin Pryzby
pryzby@telsasoft.com
In reply to: Daniel Gustafsson (#1)
Re: Reducing connection overhead in pg_upgrade compat check phase

On Fri, Feb 17, 2023 at 10:44:49PM +0100, Daniel Gustafsson wrote:

When adding a check to pg_upgrade a while back I noticed in a profile that the
cluster compatibility check phase spend a lot of time in connectToServer. Some
of this can be attributed to data type checks which each run serially in turn
connecting to each database to run the check, and this seemed like a place
where we can do better.

src/bin/pg_upgrade/check.c | 371 +++++++++++++++---------------
src/bin/pg_upgrade/pg_upgrade.h | 28 ++-
src/bin/pg_upgrade/version.c | 394 ++++++++++++++------------------
3 files changed, 373 insertions(+), 420 deletions(-)

And saves 50 LOC.

The stated goal of the patch is to reduce overhead. But it only updates
a couple functions, and there are (I think) nine functions which loop
around all DBs. If you want to reduce the overhead, I assumed you'd
cache the DB connection for all tests ... but then I tried it, and first
ran into max_connections, and then ran into EMFILE. Which is probably
enough to kill my idea.

But maybe the existing patch could be phrased in terms of moving all the
per-db checks from functions to data structures (which has its own
merits). Then, there could be a single loop around DBs which executes
all the functions. The test runner can also test the major version and
handle the textfile output.

However (as Nathan mentioned) what's currently done shows *all* the
problems of a given type - if there were 9 DBs with 99 relations with
OIDs, it'd show all of them at once. It'd be a big step backwards to
only show problems for the first problematic DB.

But maybe that's an another opportunity to do better. Right now, if I
run pg_upgrade, it'll show all the failing objects, but only for first
check that fails. After fixing them, it might tell me about a 2nd
failing check. I've never run into multiple types of failing checks,
but I do know that needing to re-run pg-upgrade is annoying (see
3c0471b5f).

You talked about improving the two data types tests, which aren't
conditional on a maximum server version. The minimal improvement you'll
get is when only those two checks are run (like on a developer upgrade
v16=>v16). But when more checks are run during a production upgrade
like v13=>v16, you'd see a larger gain.

I fooled around with that idea in the attached patch. I have no
particular interest in optimizing --check for large numbers of DBs, so
I'm not planning to pursue it further, but maybe it'll be useful to you.

About your original patch:

+static DataTypesUsageChecks data_types_usage_checks[] = {
+	/*
+	 * Look for composite types that were made during initdb *or* belong to
+	 * information_schema; that's important in case information_schema was
+	 * dropped and reloaded.
+	 *
+	 * The cutoff OID here should match the source cluster's value of
+	 * FirstNormalObjectId.  We hardcode it rather than using that C #define
+	 * because, if that #define is ever changed, our own version's value is
+	 * NOT what to use.  Eventually we may need a test on the source cluster's
+	 * version to select the correct value.
+	 */
+	{"Checking for system-defined composite types in user tables",
+	 "tables_using_composite.txt",

I think this might e cleaner using "named initializer" struct
initialization, rather than a comma-separated list (whatever that's
called).

Maybe instead of putting all checks into an array of
DataTypesUsageChecks, they should be defined in separate arrays, and
then an array defined with the list of checks?

+			 * If the check failed, terminate the umbrella status and print
+			 * the specific status line of the check to indicate which it was
+			 * before terminating with the detailed error message.
+			 */
+			if (found)
+			{
+				PQfinish(conn);
-	base_query = psprintf("SELECT '%s'::pg_catalog.regtype AS oid",
-						  type_name);
+				report_status(PG_REPORT, "failed");
+				prep_status("%s", cur_check->status);
+				pg_log(PG_REPORT, "fatal");
+				pg_fatal("%s    %s", cur_check->fatal_check, output_path);
+			}

I think this loses the message localization/translation that currently
exists. It could be written like prep_status(cur_check->status) or
prep_status("%s", _(cur_check->status)). And _(cur_check->fatal_check).

--
Justin

Attachments:

0001-wip-pg_upgrade-data-structure.patchtext/x-diff; charset=us-asciiDownload+517-679
#5Daniel Gustafsson
daniel@yesql.se
In reply to: Nathan Bossart (#3)
Re: Reducing connection overhead in pg_upgrade compat check phase

On 18 Feb 2023, at 06:46, Nathan Bossart <nathandbossart@gmail.com> wrote:

On Fri, Feb 17, 2023 at 10:44:49PM +0100, Daniel Gustafsson wrote:

When adding a check to pg_upgrade a while back I noticed in a profile that the
cluster compatibility check phase spend a lot of time in connectToServer. Some
of this can be attributed to data type checks which each run serially in turn
connecting to each database to run the check, and this seemed like a place
where we can do better.

The attached patch moves the checks from individual functions, which each loops
over all databases, into a struct which is consumed by a single umbrella check
where all data type queries are executed against a database using the same
connection. This way we can amortize the connectToServer overhead across more
accesses to the database.

This change consolidates all the data type checks, so instead of 7 separate
loops through all the databases, there is just one. However, I wonder if
we are leaving too much on the table, as there are a number of other
functions that also loop over all the databases:

* get_loadable_libraries
* get_db_and_rel_infos
* report_extension_updates
* old_9_6_invalidate_hash_indexes
* check_for_isn_and_int8_passing_mismatch
* check_for_user_defined_postfix_ops
* check_for_incompatible_polymorphics
* check_for_tables_with_oids
* check_for_user_defined_encoding_conversions

I suspect consolidating get_loadable_libraries, get_db_and_rel_infos, and
report_extension_updates would be prohibitively complicated and not worth
the effort.

Agreed, the added complexity of the code seems hard to justify unless there are
actual reports of problems.

I did experiment with reducing the allocations of namespaces and tablespaces
with a hashtable, see the attached WIP diff. There is no measurable difference
in speed, but a synthetic benchmark where allocations cannot be reused shows
reduced memory pressure. This might help on very large schemas, but it's not
worth pursuing IMO.

old_9_6_invalidate_hash_indexes is only needed for unsupported
versions, so that might not be worth consolidating.
check_for_isn_and_int8_passing_mismatch only loops through all databases
when float8_pass_by_value in the control data differs, so that might not be
worth it, either.

Yeah, these two aren't all that interesting to spend cycles on IMO.

The last 4 are for supported versions and, from a very
quick glance, seem possible to consolidate. That would bring us to a total
of 11 separate loops that we could consolidate into one. However, the data
type checks seem to follow a nice pattern, so perhaps this is easier said
than done.

There is that, refactoring the data type checks leads to removal of duplicated
code and a slight performance improvement. Refactoring the other checks to
reduce overhead would be an interesting thing to look at, but this point in the
v16 cycle might not be ideal for that.

IIUC with the patch, pg_upgrade will immediately fail as soon as a single
check in a database fails. I believe this differs from the current
behavior where all matches for a given check in the cluster are logged
before failing.

Yeah, that's wrong. Fixed.

I wonder if it'd be better to perform all of the data type
checks in all databases before failing so that all of the violations are
reported. Else, users would have to run pg_upgrade, fix a violation, run
pg_upgrade again, fix another one, etc.

I think that's better, and have changed the patch to do it that way.

One change this brings is that check.c contains version specific checks in the
struct. Previously these were mostly contained in version.c (some, like the
9.4 jsonb check was in check.c) which maintained some level of separation.
Splitting the array init is of course one option but it also seems a tad messy.
Not sure what's best, but for now I've documented it in the array comment at
least.

This version also moves the main data types check to check.c, renames some
members in the struct, moves to named initializers (as commented on by Justin
downthread), and adds some more polish here and there.

--
Daniel Gustafsson

Attachments:

nsphash.diffapplication/octet-stream; name=nsphash.diff; x-unix-mode=0644Download+67-33
v2-0001-pg_upgrade-run-all-data-type-checks-per-connectio.patchapplication/octet-stream; name=v2-0001-pg_upgrade-run-all-data-type-checks-per-connectio.patch; x-unix-mode=0644Download+421-449
#6Nathan Bossart
nathandbossart@gmail.com
In reply to: Daniel Gustafsson (#5)
Re: Reducing connection overhead in pg_upgrade compat check phase

On Wed, Feb 22, 2023 at 10:37:35AM +0100, Daniel Gustafsson wrote:

On 18 Feb 2023, at 06:46, Nathan Bossart <nathandbossart@gmail.com> wrote:
The last 4 are for supported versions and, from a very
quick glance, seem possible to consolidate. That would bring us to a total
of 11 separate loops that we could consolidate into one. However, the data
type checks seem to follow a nice pattern, so perhaps this is easier said
than done.

There is that, refactoring the data type checks leads to removal of duplicated
code and a slight performance improvement. Refactoring the other checks to
reduce overhead would be an interesting thing to look at, but this point in the
v16 cycle might not be ideal for that.

Makes sense.

I wonder if it'd be better to perform all of the data type
checks in all databases before failing so that all of the violations are
reported. Else, users would have to run pg_upgrade, fix a violation, run
pg_upgrade again, fix another one, etc.

I think that's better, and have changed the patch to do it that way.

Thanks. This seems to work as intended. One thing I noticed is that the
"failed check" log is only printed once, even if multiple data type checks
failed. I believe this is because this message uses PG_STATUS. If I
change it to PG_REPORT, all of the "failed check" messages appear. TBH I'm
not sure we need this message at all since a more detailed explanation will
be printed afterwards. If we do keep it around, I think it should be
indented so that it looks more like this:

Checking for data type usage checking all databases
failed check: incompatible aclitem data type in user tables
failed check: reg* data types in user tables

One change this brings is that check.c contains version specific checks in the
struct. Previously these were mostly contained in version.c (some, like the
9.4 jsonb check was in check.c) which maintained some level of separation.
Splitting the array init is of course one option but it also seems a tad messy.
Not sure what's best, but for now I've documented it in the array comment at
least.

Hm. We could move check_for_aclitem_data_type_usage() and
check_for_jsonb_9_4_usage() to version.c since those are only used for
determining whether the check applies now. Otherwise, IMO things are in
roughly the right place. I don't think it's necessary to split the array.

--
Nathan Bossart
Amazon Web Services: https://aws.amazon.com

#7Daniel Gustafsson
daniel@yesql.se
In reply to: Nathan Bossart (#6)
Re: Reducing connection overhead in pg_upgrade compat check phase

On 22 Feb 2023, at 20:20, Nathan Bossart <nathandbossart@gmail.com> wrote:

One thing I noticed is that the
"failed check" log is only printed once, even if multiple data type checks
failed. I believe this is because this message uses PG_STATUS. If I
change it to PG_REPORT, all of the "failed check" messages appear. TBH I'm
not sure we need this message at all since a more detailed explanation will
be printed afterwards. If we do keep it around, I think it should be
indented so that it looks more like this:

Checking for data type usage checking all databases
failed check: incompatible aclitem data type in user tables
failed check: reg* data types in user tables

Thats a good point, that's better. I think it makes sense to keep it around.

One change this brings is that check.c contains version specific checks in the
struct. Previously these were mostly contained in version.c (some, like the
9.4 jsonb check was in check.c) which maintained some level of separation.
Splitting the array init is of course one option but it also seems a tad messy.
Not sure what's best, but for now I've documented it in the array comment at
least.

Hm. We could move check_for_aclitem_data_type_usage() and
check_for_jsonb_9_4_usage() to version.c since those are only used for
determining whether the check applies now. Otherwise, IMO things are in
roughly the right place. I don't think it's necessary to split the array.

Will do, thanks.

--
Daniel Gustafsson

#8Daniel Gustafsson
daniel@yesql.se
In reply to: Daniel Gustafsson (#7)
Re: Reducing connection overhead in pg_upgrade compat check phase

On 23 Feb 2023, at 15:12, Daniel Gustafsson <daniel@yesql.se> wrote:

On 22 Feb 2023, at 20:20, Nathan Bossart <nathandbossart@gmail.com> wrote:

One thing I noticed is that the
"failed check" log is only printed once, even if multiple data type checks
failed. I believe this is because this message uses PG_STATUS. If I
change it to PG_REPORT, all of the "failed check" messages appear. TBH I'm
not sure we need this message at all since a more detailed explanation will
be printed afterwards. If we do keep it around, I think it should be
indented so that it looks more like this:

Checking for data type usage checking all databases
failed check: incompatible aclitem data type in user tables
failed check: reg* data types in user tables

Thats a good point, that's better. I think it makes sense to keep it around.

One change this brings is that check.c contains version specific checks in the
struct. Previously these were mostly contained in version.c (some, like the
9.4 jsonb check was in check.c) which maintained some level of separation.
Splitting the array init is of course one option but it also seems a tad messy.
Not sure what's best, but for now I've documented it in the array comment at
least.

Hm. We could move check_for_aclitem_data_type_usage() and
check_for_jsonb_9_4_usage() to version.c since those are only used for
determining whether the check applies now. Otherwise, IMO things are in
roughly the right place. I don't think it's necessary to split the array.

Will do, thanks.

The attached v3 is a rebase to handle conflicts and with the above comments
adressed.

--
Daniel Gustafsson

Attachments:

v3-0001-pg_upgrade-run-all-data-type-checks-per-connectio.patchapplication/octet-stream; name=v3-0001-pg_upgrade-run-all-data-type-checks-per-connectio.patch; x-unix-mode=0644Download+433-461
#9Nathan Bossart
nathandbossart@gmail.com
In reply to: Daniel Gustafsson (#8)
Re: Reducing connection overhead in pg_upgrade compat check phase

On Mon, Mar 13, 2023 at 03:10:58PM +0100, Daniel Gustafsson wrote:

The attached v3 is a rebase to handle conflicts and with the above comments
adressed.

Thanks for the new version of the patch.

I noticed that git-am complained when I applied the patch:

Applying: pg_upgrade: run all data type checks per connection
.git/rebase-apply/patch:1023: new blank line at EOF.
+
warning: 1 line adds whitespace errors.

+				for (int rowno = 0; rowno < ntups; rowno++)
+				{
+					found = true;

It looks like "found" is set unconditionally a few lines above, so I think
this is redundant.

Also, I think it would be worth breaking check_for_data_types_usage() into
a few separate functions (or doing some other similar refactoring) to
improve readability. At this point, the function is quite lengthy, and I
count 6 levels of indentation at some lines.

--
Nathan Bossart
Amazon Web Services: https://aws.amazon.com

#10Nathan Bossart
nathandbossart@gmail.com
In reply to: Nathan Bossart (#9)
Re: Reducing connection overhead in pg_upgrade compat check phase

I put together a rebased version of the patch for cfbot.

--
Nathan Bossart
Amazon Web Services: https://aws.amazon.com

Attachments:

v4-0001-pg_upgrade-run-all-data-type-checks-per-connectio.patchtext/x-diff; charset=us-asciiDownload+433-461
#11Daniel Gustafsson
daniel@yesql.se
In reply to: Nathan Bossart (#10)
Re: Reducing connection overhead in pg_upgrade compat check phase

On 4 Jul 2023, at 21:08, Nathan Bossart <nathandbossart@gmail.com> wrote:

I put together a rebased version of the patch for cfbot.

Thanks for doing that, much appreciated! I was busy looking at other peoples
patches and hadn't gotten to my own yet =)

On 13 Mar 2023, at 19:21, Nathan Bossart <nathandbossart@gmail.com> wrote:

I noticed that git-am complained when I applied the patch:

Applying: pg_upgrade: run all data type checks per connection
.git/rebase-apply/patch:1023: new blank line at EOF.
+
warning: 1 line adds whitespace errors.

Fixed.

+				for (int rowno = 0; rowno < ntups; rowno++)
+				{
+					found = true;

It looks like "found" is set unconditionally a few lines above, so I think
this is redundant.

Correct, this must've been a leftover from a previous coding that changed.
Removed.

Also, I think it would be worth breaking check_for_data_types_usage() into
a few separate functions (or doing some other similar refactoring) to
improve readability. At this point, the function is quite lengthy, and I
count 6 levels of indentation at some lines.

It it is pretty big for sure, but it's also IMHO not terribly complicated as
it's not really performing any hard to follow logic.

I have no issues refactoring it, but trying my hand at I was only making (what
I consider) less readable code by having to jump around so I consider it a
failure. If you have any suggestions, I would be more than happy to review and
incorporate those though.

Attached is a v5 with the above fixes and a pgindenting to fix up a few runaway
comments and indentations.

--
Daniel Gustafsson

Attachments:

v5-0001-pg_upgrade-run-all-data-type-checks-per-connectio.patchapplication/octet-stream; name=v5-0001-pg_upgrade-run-all-data-type-checks-per-connectio.patch; x-unix-mode=0644Download+446-461
#12Nathan Bossart
nathandbossart@gmail.com
In reply to: Daniel Gustafsson (#11)
Re: Reducing connection overhead in pg_upgrade compat check phase

Thanks for the new patch.

On Thu, Jul 06, 2023 at 05:58:33PM +0200, Daniel Gustafsson wrote:

On 4 Jul 2023, at 21:08, Nathan Bossart <nathandbossart@gmail.com> wrote:
Also, I think it would be worth breaking check_for_data_types_usage() into
a few separate functions (or doing some other similar refactoring) to
improve readability. At this point, the function is quite lengthy, and I
count 6 levels of indentation at some lines.

It it is pretty big for sure, but it's also IMHO not terribly complicated as
it's not really performing any hard to follow logic.

I have no issues refactoring it, but trying my hand at I was only making (what
I consider) less readable code by having to jump around so I consider it a
failure. If you have any suggestions, I would be more than happy to review and
incorporate those though.

I don't have a strong opinion about this.

+				for (int rowno = 0; rowno < ntups; rowno++)
+				{
+					if (script == NULL && (script = fopen_priv(output_path, "a")) == NULL)
+						pg_fatal("could not open file \"%s\": %s",
+								 output_path,
+								 strerror(errno));
+					if (!db_used)
+					{
+						fprintf(script, "In database: %s\n", active_db->db_name);
+						db_used = true;
+					}

Since "script" will be NULL and "db_used" will be false in the first
iteration of the loop, couldn't we move this stuff to before the loop?

+					fprintf(script, " %s.%s.%s\n",
+							PQgetvalue(res, rowno, i_nspname),
+							PQgetvalue(res, rowno, i_relname),
+							PQgetvalue(res, rowno, i_attname));

nitpick: І think the current code has two spaces at the beginning of this
format string. Did you mean to remove one of them?

+				if (script)
+				{
+					fclose(script);
+					script = NULL;
+				}

Won't "script" always be initialized here? If I'm following this code
correctly, I think everything except the fclose() can be removed.

+ cur_check++;

I think this is unnecessary since we assign "cur_check" at the beginning of
every loop iteration. I see two of these.

+static int n_data_types_usage_checks = 7;

Can we determine this programmatically so that folks don't need to remember
to update it?

+	/* Prepare an array to store the results of checks in */
+	results = pg_malloc(sizeof(bool) * n_data_types_usage_checks);
+	memset(results, true, sizeof(*results));

IMHO it's a little strange that this is initialized to all "true", only
because I think most other Postgres code does the opposite.

+bool
+check_for_aclitem_data_type_usage(ClusterInfo *cluster)

Do you think we should rename these functions to something like
"should_check_for_*"? They don't actually do the check, they just tell you
whether you should based on the version. In fact, I wonder if we could
just add the versions directly to data_types_usage_checks so that we don't
need the separate hook functions.

--
Nathan Bossart
Amazon Web Services: https://aws.amazon.com

#13Daniel Gustafsson
daniel@yesql.se
In reply to: Nathan Bossart (#12)
Re: Reducing connection overhead in pg_upgrade compat check phase

On 8 Jul 2023, at 23:43, Nathan Bossart <nathandbossart@gmail.com> wrote:

Thanks for reviewing!

Since "script" will be NULL and "db_used" will be false in the first
iteration of the loop, couldn't we move this stuff to before the loop?

We could. It's done this way to match how all the other checks are performing
the inner loop for consistency. I think being consistent is better than micro
optimizing in non-hot codepaths even though it adds some redundancy.

nitpick: І think the current code has two spaces at the beginning of this
format string. Did you mean to remove one of them?

Nice catch, I did not. Fixed.

Won't "script" always be initialized here? If I'm following this code
correctly, I think everything except the fclose() can be removed.

You are right that this check is superfluous. This is again an artifact of
modelling the code around how the other checks work for consistency. At least
I think that's a good characteristic of the code.

I think this is unnecessary since we assign "cur_check" at the beginning of
every loop iteration. I see two of these.

Right, this is a pointless leftover from a previous version which used a
while() loop, and I had missed removing them. Fixed.

+static int n_data_types_usage_checks = 7;

Can we determine this programmatically so that folks don't need to remember
to update it?

Fair point, I've added a counter loop to the beginning of the check function to
calculate it.

IMHO it's a little strange that this is initialized to all "true", only
because I think most other Postgres code does the opposite.

Agreed, but it made for a less contrived codepath in knowing when an error has
been seen already, to avoid duplicate error output, so I think it's worth it.

Do you think we should rename these functions to something like
"should_check_for_*"? They don't actually do the check, they just tell you
whether you should based on the version.

I've been pondering that too, and did a rename now along with moving them all
to a single place as well as changing the comments to make it clearer.

In fact, I wonder if we could just add the versions directly to
data_types_usage_checks so that we don't need the separate hook functions.

We could, but it would be sort of contrived I think since some check <= and
some == while some check the catversion as well (and new ones may have other
variants. I think this is the least paint-ourselves-in-a-corner version, if we
feel it's needlessly complicated and no other variants are added we can revisit
this.

--
Daniel Gustafsson

Attachments:

v6-0001-pg_upgrade-run-all-data-type-checks-per-connectio.patchapplication/octet-stream; name=v6-0001-pg_upgrade-run-all-data-type-checks-per-connectio.patch; x-unix-mode=0644Download+450-463
#14Nathan Bossart
nathandbossart@gmail.com
In reply to: Daniel Gustafsson (#13)
Re: Reducing connection overhead in pg_upgrade compat check phase

On Mon, Jul 10, 2023 at 04:43:23PM +0200, Daniel Gustafsson wrote:

On 8 Jul 2023, at 23:43, Nathan Bossart <nathandbossart@gmail.com> wrote:
Since "script" will be NULL and "db_used" will be false in the first
iteration of the loop, couldn't we move this stuff to before the loop?

We could. It's done this way to match how all the other checks are performing
the inner loop for consistency. I think being consistent is better than micro
optimizing in non-hot codepaths even though it adds some redundancy.

[ ... ]

Won't "script" always be initialized here? If I'm following this code
correctly, I think everything except the fclose() can be removed.

You are right that this check is superfluous. This is again an artifact of
modelling the code around how the other checks work for consistency. At least
I think that's a good characteristic of the code.

I can't say I agree with this, but I'm not going to hold up the patch over
it. FWIW I was looking at this more from a code simplification/readability
standpoint.

+static int n_data_types_usage_checks = 7;

Can we determine this programmatically so that folks don't need to remember
to update it?

Fair point, I've added a counter loop to the beginning of the check function to
calculate it.

+	/* Gather number of checks to perform */
+	while (tmp->status != NULL)
+		n_data_types_usage_checks++;

I think we need to tmp++ somewhere here.

In fact, I wonder if we could just add the versions directly to
data_types_usage_checks so that we don't need the separate hook functions.

We could, but it would be sort of contrived I think since some check <= and
some == while some check the catversion as well (and new ones may have other
variants. I think this is the least paint-ourselves-in-a-corner version, if we
feel it's needlessly complicated and no other variants are added we can revisit
this.

Makes sense.

--
Nathan Bossart
Amazon Web Services: https://aws.amazon.com

#15Daniel Gustafsson
daniel@yesql.se
In reply to: Nathan Bossart (#14)
Re: Reducing connection overhead in pg_upgrade compat check phase

On 11 Jul 2023, at 01:09, Nathan Bossart <nathandbossart@gmail.com> wrote:
On Mon, Jul 10, 2023 at 04:43:23PM +0200, Daniel Gustafsson wrote:

+static int n_data_types_usage_checks = 7;

Can we determine this programmatically so that folks don't need to remember
to update it?

Fair point, I've added a counter loop to the beginning of the check function to
calculate it.

+	/* Gather number of checks to perform */
+	while (tmp->status != NULL)
+		n_data_types_usage_checks++;

I think we need to tmp++ somewhere here.

Yuk, yes, will fix when caffeinated. Thanks.

--
Daniel Gustafsson

#16Daniel Gustafsson
daniel@yesql.se
In reply to: Daniel Gustafsson (#15)
Re: Reducing connection overhead in pg_upgrade compat check phase

On 11 Jul 2023, at 01:26, Daniel Gustafsson <daniel@yesql.se> wrote:

On 11 Jul 2023, at 01:09, Nathan Bossart <nathandbossart@gmail.com> wrote:

I think we need to tmp++ somewhere here.

Yuk, yes, will fix when caffeinated. Thanks.

I did have coffee before now, but only found time to actually address this now
so here is a v7 with just that change and a fresh rebase.

--
Daniel Gustafsson

Attachments:

v7-0001-pg_upgrade-run-all-data-type-checks-per-connectio.patchapplication/octet-stream; name=v7-0001-pg_upgrade-run-all-data-type-checks-per-connectio.patch; x-unix-mode=0644Download+453-463
#17Nathan Bossart
nathandbossart@gmail.com
In reply to: Daniel Gustafsson (#16)
Re: Reducing connection overhead in pg_upgrade compat check phase

On Wed, Jul 12, 2023 at 12:43:14AM +0200, Daniel Gustafsson wrote:

I did have coffee before now, but only found time to actually address this now
so here is a v7 with just that change and a fresh rebase.

Thanks. I think the patch is in decent shape.

--
Nathan Bossart
Amazon Web Services: https://aws.amazon.com

#18Daniel Gustafsson
daniel@yesql.se
In reply to: Nathan Bossart (#17)
Re: Reducing connection overhead in pg_upgrade compat check phase

On 12 Jul 2023, at 01:36, Nathan Bossart <nathandbossart@gmail.com> wrote:

On Wed, Jul 12, 2023 at 12:43:14AM +0200, Daniel Gustafsson wrote:

I did have coffee before now, but only found time to actually address this now
so here is a v7 with just that change and a fresh rebase.

Thanks. I think the patch is in decent shape.

Due to ENOTENOUGHTIME it bitrotted a bit, so here is a v8 rebase which I really
hope to close in this CF.

--
Daniel Gustafsson

Attachments:

v8-0001-pg_upgrade-run-all-data-type-checks-per-connectio.patchapplication/octet-stream; name=v8-0001-pg_upgrade-run-all-data-type-checks-per-connectio.patch; x-unix-mode=0644Download+453-463
#19Peter Eisentraut
peter_e@gmx.net
In reply to: Daniel Gustafsson (#18)
Re: Reducing connection overhead in pg_upgrade compat check phase

On 31.08.23 23:34, Daniel Gustafsson wrote:

On 12 Jul 2023, at 01:36, Nathan Bossart <nathandbossart@gmail.com> wrote:

On Wed, Jul 12, 2023 at 12:43:14AM +0200, Daniel Gustafsson wrote:

I did have coffee before now, but only found time to actually address this now
so here is a v7 with just that change and a fresh rebase.

Thanks. I think the patch is in decent shape.

Due to ENOTENOUGHTIME it bitrotted a bit, so here is a v8 rebase which I really
hope to close in this CF.

The alignment of this output looks a bit funny:

...
Checking for prepared transactions ok
Checking for contrib/isn with bigint-passing mismatch ok
Checking for data type usage checking all databases
ok
Checking for presence of required libraries ok
Checking database user is the install user ok
...

Also, you should put gettext_noop() calls into the .status = "Checking ..."
assignments and arrange to call gettext() where they are used, to maintain
the translatability.

#20Daniel Gustafsson
daniel@yesql.se
In reply to: Peter Eisentraut (#19)
Re: Reducing connection overhead in pg_upgrade compat check phase

On 13 Sep 2023, at 16:12, Peter Eisentraut <peter@eisentraut.org> wrote:

The alignment of this output looks a bit funny:

...
Checking for prepared transactions ok
Checking for contrib/isn with bigint-passing mismatch ok
Checking for data type usage checking all databases
ok
Checking for presence of required libraries ok
Checking database user is the install user ok
...

I was using the progress reporting to indicate that it hadn't stalled for slow
systems, but it's not probably not all that important really. Removed such
that "ok" aligns.

Also, you should put gettext_noop() calls into the .status = "Checking ..."
assignments and arrange to call gettext() where they are used, to maintain
the translatability.

Ah, yes of course. Fixed.

--
Daniel Gustafsson

Attachments:

v9-0001-pg_upgrade-run-all-data-type-checks-per-connectio.patchapplication/octet-stream; name=v9-0001-pg_upgrade-run-all-data-type-checks-per-connectio.patch; x-unix-mode=0644Download+451-463
#21Daniel Gustafsson
daniel@yesql.se
In reply to: Daniel Gustafsson (#20)
#22vignesh C
vignesh21@gmail.com
In reply to: Daniel Gustafsson (#21)
#23vignesh C
vignesh21@gmail.com
In reply to: vignesh C (#22)
#24Nathan Bossart
nathandbossart@gmail.com
In reply to: vignesh C (#23)
#25Daniel Gustafsson
daniel@yesql.se
In reply to: Nathan Bossart (#24)
#26Nathan Bossart
nathandbossart@gmail.com
In reply to: Daniel Gustafsson (#25)
#27Daniel Gustafsson
daniel@yesql.se
In reply to: Daniel Gustafsson (#25)
#28Peter Eisentraut
peter_e@gmx.net
In reply to: Daniel Gustafsson (#27)
#29Daniel Gustafsson
daniel@yesql.se
In reply to: Peter Eisentraut (#28)
#30Daniel Gustafsson
daniel@yesql.se
In reply to: Daniel Gustafsson (#29)
#31Daniel Gustafsson
daniel@yesql.se
In reply to: Daniel Gustafsson (#30)
#32Daniel Gustafsson
daniel@yesql.se
In reply to: Daniel Gustafsson (#31)
#33Peter Eisentraut
peter_e@gmx.net
In reply to: Daniel Gustafsson (#32)
#34Daniel Gustafsson
daniel@yesql.se
In reply to: Peter Eisentraut (#33)