Fixes a clip bug in pg_stat_get_backend_activity()

Started by Chao Li4 months ago4 messageshackers
Jump to latest
#1Chao Li
li.evan.chao@gmail.com

Hi Hackers,

This is an eyeball caught bug:
```
clipped_activity = pgstat_clip_activity(activity);
ret = cstring_to_text(activity); <== bug here: should use clipped_activity
pfree(clipped_activity);

PG_RETURN_TEXT_P(ret);
```
clipped result is stored in clipped_activity, but it's not used.

This should be an edge case bug. It would only be triggered by using
multi-byte characters in SQL and the SQL statement length
exceeds track_activity_query_size, and a multi-byte character just across
the boundary and gets truncated in the middle.

I tried to reproduce the bug, but failed. Anyway, this is an obvious fix,
no logic change.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

v1-0001-Fixes-a-clip-bug-in-pg_stat_get_backend_activity.patchapplication/octet-stream; name=v1-0001-Fixes-a-clip-bug-in-pg_stat_get_backend_activity.patchDownload+1-2
#2Michael Paquier
michael@paquier.xyz
In reply to: Chao Li (#1)
Re: Fixes a clip bug in pg_stat_get_backend_activity()

On Fri, Dec 26, 2025 at 11:54:38AM +0800, Chao Li wrote:

This is an eyeball caught bug:
```
clipped_activity = pgstat_clip_activity(activity);
ret = cstring_to_text(activity); <== bug here: should use clipped_activity
pfree(clipped_activity);

PG_RETURN_TEXT_P(ret);
```
clipped result is stored in clipped_activity, but it's not used.

Ohh.... Oops. Nice catch.

This should be an edge case bug. It would only be triggered by using
multi-byte characters in SQL and the SQL statement length
exceeds track_activity_query_size, and a multi-byte character just across
the boundary and gets truncated in the middle.

Yep, that seems wrong since 54b6cd589ac2, meaning a backpatch all the
way down. pg_stat_get_backend_activity() is a less popular solution
than pg_stat_activity, so this would unlikely be hit, still wrong it
is.
--
Michael

#3Michael Paquier
michael@paquier.xyz
In reply to: Michael Paquier (#2)
Re: Fixes a clip bug in pg_stat_get_backend_activity()

On Fri, Dec 26, 2025 at 02:59:33PM +0900, Michael Paquier wrote:

Yep, that seems wrong since 54b6cd589ac2, meaning a backpatch all the
way down. pg_stat_get_backend_activity() is a less popular solution
than pg_stat_activity, so this would unlikely be hit, still wrong it
is.

Applied this one as 36b8f4974a88 down to v14.
--
Michael

#4Chao Li
li.evan.chao@gmail.com
In reply to: Michael Paquier (#3)
Re: Fixes a clip bug in pg_stat_get_backend_activity()

On Dec 28, 2025, at 07:30, Michael Paquier <michael@paquier.xyz> wrote:

On Fri, Dec 26, 2025 at 02:59:33PM +0900, Michael Paquier wrote:

Yep, that seems wrong since 54b6cd589ac2, meaning a backpatch all the
way down. pg_stat_get_backend_activity() is a less popular solution
than pg_stat_activity, so this would unlikely be hit, still wrong it
is.

Applied this one as 36b8f4974a88 down to v14.
--
Michael

Hi Micheal, thanks a lot for taking care of this patch, especially for working in the weekend.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/