Re: Printing LSN made easy

From: Li Japin <japinli(at)hotmail(dot)com>
To: Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>
Cc: "ashutosh(dot)bapat(at)enterprisedb(dot)com" <ashutosh(dot)bapat(at)enterprisedb(dot)com>, Alexey Kondratov <a(dot)kondratov(at)postgrespro(dot)ru>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>, Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>, Craig Ringer <craig(dot)ringer(at)enterprisedb(dot)com>
Subject: Re: Printing LSN made easy
Date: 2020-11-30 14:08:44
Message-ID: 95EBC593-1A19-4E0B-B778-373C8A6F0238@hotmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On Nov 30, 2020, at 9:06 PM, Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com<mailto:ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>> wrote:

On Fri, Nov 27, 2020 at 9:51 PM Li Japin <japinli(at)hotmail(dot)com<mailto:japinli(at)hotmail(dot)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.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Alexey Kondratov 2020-11-30 14:12:42 Re: Allow CLUSTER, VACUUM FULL and REINDEX to change tablespace on the fly
Previous Message Amit Langote 2020-11-30 14:06:47 Re: [POC] Fast COPY FROM command for the table with foreign partitions