Re: Printing LSN made easy
Japin Li <japinli@hotmail.com>
From: Li Japin <japinli@hotmail.com>
To: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Cc: "ashutosh.bapat@enterprisedb.com" <ashutosh.bapat@enterprisedb.com>, Alexey Kondratov <a.kondratov@postgrespro.ru>, pgsql-hackers <pgsql-hackers@postgresql.org>, Peter Eisentraut <peter.eisentraut@2ndquadrant.com>, Craig Ringer <craig.ringer@enterprisedb.com>
Date: 2020-11-30T14:08:44Z
Lists: pgsql-hackers
Hi,
On Nov 30, 2020, at 9:06 PM, Ashutosh Bapat <ashutosh.bapat.oss@gmail.com<mailto:ashutosh.bapat.oss@gmail.com>> wrote:
On Fri, Nov 27, 2020 at 9:51 PM Li Japin <japinli@hotmail.com<mailto:japinli@hotmail.com>> wrote:
Hi,
Here, we cannot use sizeof(but) to get the buf size, because it is a pointer, so it always
8 bytes on 64-bit or 4 bytes on 32-bit machine.
For an array, the sizeof() returns the size of memory consumed by the
array. See section "Application to arrays" at
https://en.wikipedia.org/wiki/Sizeof.
That’s true! However, in pg_lsn_out_buffer(), it converts to a pointer, not an array. See the following test:
```c
#include <stdio.h>
#include <stdint.h>
typedef uint64_t XLogRecPtr;
typedef uint32_t uint32;
#define MAXPG_LSNLEN 17
#define LSN_FORMAT "%X/%X"
#define LSN_FORMAT_ARG(lsn) (uint32) ((lsn) >> 32), (uint32) (lsn)
char *
pg_lsn_out_buffer(XLogRecPtr lsn, char *buf)
{
printf("pg_lsn_out_buffer: sizeof(buf) = %lu\n", sizeof(buf));
snprintf(buf, sizeof(buf), LSN_FORMAT, LSN_FORMAT_ARG(lsn));
return buf;
}
char *
pg_lsn_out_buffer1(XLogRecPtr lsn, char *buf, size_t len)
{
printf("pg_lsn_out_buffer1: sizeof(buf) = %lu, len = %lu\n", sizeof(buf), len);
snprintf(buf, len, LSN_FORMAT, LSN_FORMAT_ARG(lsn));
return buf;
}
int
main(void)
{
char buf[MAXPG_LSNLEN + 1];
XLogRecPtr lsn = 1234567UL;
printf("main: sizeof(buf) = %lu\n", sizeof(buf));
pg_lsn_out_buffer(lsn, buf);
printf("buffer's content from pg_lsn_out_buffer: %s\n", buf);
pg_lsn_out_buffer1(lsn, buf, sizeof(buf));
printf("buffer's content from pg_lsn_out_buffer1: %s\n", buf);
return 0;
}
```
The above output is:
```
main: sizeof(buf) = 18
pg_lsn_out_buffer: sizeof(buf) = 8
buffer's content from pg_lsn_out_buffer: 0/12D68
pg_lsn_out_buffer1: sizeof(buf) = 8, len = 18
buffer's content from pg_lsn_out_buffer1: 0/12D687
```
--
Best regards
Japin Li
ChengDu WenWu Information Technolog Co.,Ltd.
Commits
-
Simplify printing of LSNs
- 6f6f284c7ee4 14.0 landed
-
Unwind some workarounds for lack of portable int64 format specifier
- 6a1cd8b9236d 13.0 cited