[PATCH] Remove workarounds to format [u]int64's

Started by Aleksander Alekseevover 4 years ago12 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

won't retrysuccessCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t45680
psql -h localhost -U postgres

Built from patchset v11 (message #11), August 18, 2026 at 03:50 PM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t45680_11 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t45680_11 && git checkout t45680_11

Patchset v11 (message #11) is on t45680_11

Jump to latest
#1Aleksander Alekseev
aleksander@timescale.com

Hi hackers,

I learned from Tom [1]/messages/by-id/771048.1647528068@sss.pgh.pa.us that we can simplify the code like:

```
char buff[32];
snprintf(buf, sizeof(buf), INT64_FORMAT, ...)
ereport(WARNING, (errmsg("%s ...", buf)));
```

... and rely on %lld/%llu now as long as we explicitly cast the
argument to long long int / unsigned long long. This was previously
addressed in 6a1cd8b9 and d914eb34, but I see more places where we
still use an old approach.

Suggested patch fixes this. Tested locally - no warnings; passes all the tests.

[1]: /messages/by-id/771048.1647528068@sss.pgh.pa.us

--
Best regards,
Aleksander Alekseev

Attachments:

v1-0001-Remove-workarounds-to-format-u-int64-s.patchapplication/octet-stream; name=v1-0001-Remove-workarounds-to-format-u-int64-s.patchDownload+39-98
#2Pavel Borisov
pashkin.elfe@gmail.com
In reply to: Aleksander Alekseev (#1)
Re: [PATCH] Remove workarounds to format [u]int64's

пн, 21 мар. 2022 г. в 12:52, Aleksander Alekseev <aleksander@timescale.com>:

Hi hackers,

I learned from Tom [1] that we can simplify the code like:

```
char buff[32];
snprintf(buf, sizeof(buf), INT64_FORMAT, ...)
ereport(WARNING, (errmsg("%s ...", buf)));
```

... and rely on %lld/%llu now as long as we explicitly cast the
argument to long long int / unsigned long long. This was previously
addressed in 6a1cd8b9 and d914eb34, but I see more places where we
still use an old approach.

Suggested patch fixes this. Tested locally - no warnings; passes all the
tests.

[1]
/messages/by-id/771048.1647528068@sss.pgh.pa.us

Hi, Alexander!

Probably you can do (long long) instead of (long long int). It is shorter
and this is used elsewhere in the code.

--
Best regards,
Pavel Borisov

Postgres Professional: http://postgrespro.com <http://www.postgrespro.com&gt;

#3Aleksander Alekseev
aleksander@timescale.com
In reply to: Pavel Borisov (#2)
Re: [PATCH] Remove workarounds to format [u]int64's

Hi Pavel,

Probably you can do (long long) instead of (long long int). It is shorter and this is used elsewhere in the code.

Thanks! Here is the updated patch. I also added Reviewed-by: and
Discussion: to the commit message.

--
Best regards,
Aleksander Alekseev

Attachments:

v2-0001-Remove-workarounds-to-format-u-int64-s.patchapplication/octet-stream; name=v2-0001-Remove-workarounds-to-format-u-int64-s.patchDownload+39-98
#4Pavel Borisov
pashkin.elfe@gmail.com
In reply to: Aleksander Alekseev (#3)
Re: [PATCH] Remove workarounds to format [u]int64's

Probably you can do (long long) instead of (long long int). It is

shorter and this is used elsewhere in the code.

Thanks! Here is the updated patch. I also added Reviewed-by: and
Discussion: to the commit message.

Thanks, Alexander!
I suggest the patch is in a good shape to be committed.
(
Maybe some strings that don't fit screen cloud be reflowed:
(long long int)seqdataform->last_value, (long long int)seqform->seqmax)));
)

--
Best regards,
Pavel Borisov

Postgres Professional: http://postgrespro.com <http://www.postgrespro.com&gt;

#5Japin Li
japinli@hotmail.com
In reply to: Aleksander Alekseev (#3)
Re: [PATCH] Remove workarounds to format [u]int64's

On Mon, 21 Mar 2022 at 17:23, Aleksander Alekseev <aleksander@timescale.com> wrote:

Hi Pavel,

Probably you can do (long long) instead of (long long int). It is shorter and this is used elsewhere in the code.

Thanks! Here is the updated patch. I also added Reviewed-by: and
Discussion: to the commit message.

Hi,

After apply the patch, I found pg_checksums.c also has the similar code.

In progress_report(), I'm not sure we can do this replace for this code.

snprintf(total_size_str, sizeof(total_size_str), INT64_FORMAT,
total_size / (1024 * 1024));
snprintf(current_size_str, sizeof(current_size_str), INT64_FORMAT,
current_size / (1024 * 1024));

fprintf(stderr, _("%*s/%s MB (%d%%) computed"),
(int) strlen(current_size_str), current_size_str, total_size_str,
percent);

--
Regrads,
Japin Li.
ChengDu WenWu Information Technology Co.,Ltd.

Attachments:

remove-workarounds-int64-format-in-pg_checksums.patchtext/x-patchDownload+5-5
#6Aleksander Alekseev
aleksander@timescale.com
In reply to: Japin Li (#5)
Re: [PATCH] Remove workarounds to format [u]int64's

Hi Japin,

After apply the patch, I found pg_checksums.c also has the similar code.

Thanks for noticing it.

In progress_report(), I'm not sure we can do this replace for this code.

I added the corresponding change as a separate commit so it can be
easily reverted if necessary.

Here is a complete patchset with some additional changes by me.

--
Best regards,
Aleksander Alekseev

Attachments:

v3-0001-Remove-workarounds-to-format-u-int64-s.patchapplication/octet-stream; name=v3-0001-Remove-workarounds-to-format-u-int64-s.patchDownload+39-98
v3-0002-Remove-workarounds-to-format-int64-s-in-pg_checks.patchapplication/octet-stream; name=v3-0002-Remove-workarounds-to-format-int64-s-in-pg_checks.patchDownload+55-67
#7Aleksander Alekseev
aleksander@timescale.com
In reply to: Aleksander Alekseev (#1)
Re: [PATCH] Remove workarounds to format [u]int64's

Hi Japin,

As Tom said in [1], we don't need to touch the *.po files, since those

files

are managed by the translation team.

[1]

/messages/by-id/1110708.1647623560@sss.pgh.pa.us

True, but I figured that simplifying the work of the translation team would
not harm either. In any case, the committer can easily exclude these
changes from the patch, if necessary.

--
Best regards,
Aleksander Alekseev

#8Tom Lane
tgl@sss.pgh.pa.us
In reply to: Aleksander Alekseev (#7)
Re: [PATCH] Remove workarounds to format [u]int64's

Aleksander Alekseev <aleksander@timescale.com> writes:

As Tom said in [1], we don't need to touch the *.po files, since those
files are managed by the translation team.

True, but I figured that simplifying the work of the translation team would
not harm either.

It would not simplify things for them at all, just mess it up.
The master copies of the .po files are kept in a different repo.
Also, I believe that extraction of new message strings is automated
already.

https://www.postgresql.org/docs/devel/nls.html

https://wiki.postgresql.org/wiki/NLS

regards, tom lane

#9Aleksander Alekseev
aleksander@timescale.com
In reply to: Tom Lane (#8)
Re: [PATCH] Remove workarounds to format [u]int64's

Hi Tom,

It would not simplify things for them at all, just mess it up.
The master copies of the .po files are kept in a different repo.
Also, I believe that extraction of new message strings is automated
already.

Got it, thanks. Here is the corrected patch. It includes all the
changes by me and Japin, and doesn't touch PO files.

--
Best regards,
Aleksander Alekseev

Attachments:

v4-0001-Remove-workarounds-to-format-u-int64-s.patchapplication/octet-stream; name=v4-0001-Remove-workarounds-to-format-u-int64-s.patchDownload+47-117
#10Tom Lane
tgl@sss.pgh.pa.us
In reply to: Aleksander Alekseev (#9)
Re: [PATCH] Remove workarounds to format [u]int64's

Aleksander Alekseev <aleksander@timescale.com> writes:

Got it, thanks. Here is the corrected patch. It includes all the
changes by me and Japin, and doesn't touch PO files.

Pushed. I removed now-unnecessary braces, reflowed some lines
as suggested by Pavel, and pgindent'ed (which insisted on adding
spaces after the casts, as is project style).

regards, tom lane

#11Peter Eisentraut
peter_e@gmx.net
In reply to: Aleksander Alekseev (#9)
Re: [PATCH] Remove workarounds to format [u]int64's

On 21.03.22 15:37, Aleksander Alekseev wrote:

It would not simplify things for them at all, just mess it up.
The master copies of the .po files are kept in a different repo.
Also, I believe that extraction of new message strings is automated
already.

Got it, thanks. Here is the corrected patch. It includes all the
changes by me and Japin, and doesn't touch PO files.

I think in some cases we can make this even simpler (and cast-free) by
changing the underlying variable to be long long instead of int64.
Especially in cases where the whole point of the variable is to be some
counter that ends up being printed, there isn't a need to use int64 in
the first place. See attached patch for examples.

Attachments:

t45680_11
0001-Change-some-variables-to-long-long-from-int64.patchtext/plain; charset=UTF-8; name=0001-Change-some-variables-to-long-long-from-int64.patchDownload+32-33
#12Pavel Borisov
pashkin.elfe@gmail.com
In reply to: Peter Eisentraut (#11)
Re: [PATCH] Remove workarounds to format [u]int64's

On 21.03.22 15:37, Aleksander Alekseev wrote:

It would not simplify things for them at all, just mess it up.
The master copies of the .po files are kept in a different repo.
Also, I believe that extraction of new message strings is automated
already.

Got it, thanks. Here is the corrected patch. It includes all the
changes by me and Japin, and doesn't touch PO files.

I think in some cases we can make this even simpler (and cast-free) by
changing the underlying variable to be long long instead of int64.
Especially in cases where the whole point of the variable is to be some
counter that ends up being printed, there isn't a need to use int64 in
the first place. See attached patch for examples.

Yes, this will work, when we can define a variable itself as *long long*.
But for some applications: [1]/messages/by-id/CACG=ezYV3FM5i9ws2QLyF+rz5WHTqheL59VRsHGsgAwfx8gh4g@mail.gmail.com, [2]/messages/by-id/CACG=ezZe1NQSCnfHOr78AtAZxJZeCvxrts0ygrxYwe=pyyjVWA@mail.gmail.com, I suppose we'll need exactly uint64 to
represent TransactionId. uint64 is warrantied to fit into *unsigned long
long*, but on most of archs it is just *unsigned long*. Defining what we
need to be uint64 as *unsigned long long* on these archs will mean it
become uint128, which we may not like.

In my opinion, in many places, it's better to have casts when it's for
printing fixed-width int/uint variables than the alternative.

[1]: /messages/by-id/CACG=ezYV3FM5i9ws2QLyF+rz5WHTqheL59VRsHGsgAwfx8gh4g@mail.gmail.com
/messages/by-id/CACG=ezYV3FM5i9ws2QLyF+rz5WHTqheL59VRsHGsgAwfx8gh4g@mail.gmail.com
[2]: /messages/by-id/CACG=ezZe1NQSCnfHOr78AtAZxJZeCvxrts0ygrxYwe=pyyjVWA@mail.gmail.com
/messages/by-id/CACG=ezZe1NQSCnfHOr78AtAZxJZeCvxrts0ygrxYwe=pyyjVWA@mail.gmail.com

--
Best regards,
Pavel Borisov

Postgres Professional: http://postgrespro.com <http://www.postgrespro.com&gt;