BUG #19631: currtid2() on a view with GROUP BY ctid crashes with XX000

Started by PG Bug reporting form3 months ago9 messageshackersbugs

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org
bugshackers

The following bug has been logged on the website:

Bug reference: 19631
Logged by: Zheng Hacker
Email address: hackerzheng666@gmail.com
PostgreSQL version: 19beta3
Operating system: Linux x86_64
Description:

PostgreSQL version: 20devel (commit bdbf662, 2026-08-19)
OS: Linux x86_64

Calling currtid2() on a view whose SELECT includes ctid in a GROUP BY
hits elog(ERROR) without errcode() in tid.c, producing SQLSTATE XX000.

Reproducer:

CREATE TABLE tid_tab (a int);
INSERT INTO tid_tab VALUES (1);
CREATE VIEW tid_view_with_ctid AS
SELECT ctid, a FROM tid_tab GROUP BY ctid, a;
SELECT currtid2('tid_view_with_ctid'::text, '(0,1)'::tid);
-- ERROR: XX000: currtid cannot handle this view
-- LOCATION: currtid_for_view, tid.c:435

Note: the view has a ctid column (so it passes the tididx check at
line 389 which does have a proper errcode), but the GROUP BY prevents
the code from resolving the TLE to a simple base-table Var, so it
falls through to the elog(ERROR) at line 435 which lacks errcode().

Expected: a proper SQLSTATE (e.g. 0A000 feature_not_supported).

Found by automated SQL fuzzing.
Credit: Zheng Wang, Yanjie Zhao, Yiyang Liu

#2Ayush Tiwari
ayushtiwari.slg01@gmail.com
In reply to: PG Bug reporting form (#1)
bugshackers
Re: BUG #19631: currtid2() on a view with GROUP BY ctid crashes with XX000

Hi,

On Wed, 19 Aug 2026 at 14:54, PG Bug reporting form <noreply@postgresql.org>
wrote:

The following bug has been logged on the website:

Bug reference: 19631
Logged by: Zheng Hacker
Email address: hackerzheng666@gmail.com
PostgreSQL version: 19beta3
Operating system: Linux x86_64
Description:

PostgreSQL version: 20devel (commit bdbf662, 2026-08-19)
OS: Linux x86_64

Calling currtid2() on a view whose SELECT includes ctid in a GROUP BY
hits elog(ERROR) without errcode() in tid.c, producing SQLSTATE XX000.

Reproducer:

CREATE TABLE tid_tab (a int);
INSERT INTO tid_tab VALUES (1);
CREATE VIEW tid_view_with_ctid AS
SELECT ctid, a FROM tid_tab GROUP BY ctid, a;
SELECT currtid2('tid_view_with_ctid'::text, '(0,1)'::tid);
-- ERROR: XX000: currtid cannot handle this view
-- LOCATION: currtid_for_view, tid.c:435

Note: the view has a ctid column (so it passes the tididx check at
line 389 which does have a proper errcode), but the GROUP BY prevents
the code from resolving the TLE to a simple base-table Var, so it
falls through to the elog(ERROR) at line 435 which lacks errcode().

Expected: a proper SQLSTATE (e.g. 0A000 feature_not_supported).

Found by automated SQL fuzzing.
Credit: Zheng Wang, Yanjie Zhao, Yiyang Liu

Thanks for the reports. #19629, #19630 and #19631 share one cause: an
error ordinary SQL can reach is raised without an errcode(), so the user
sees XX000.

I scanned the backend for the same pattern, and the attached patch fixes
those three plus four more sites with the same problem: unicode_assigned()
on a non-UTF8 encoding, the pg_control_*() CRC check, a GiST tuple marked
invalid, and ALTER COLLATION ... REFRESH VERSION on "default".

Regards,
Ayush

Attachments:

t253483_2
v1-0001-Add-missing-SQLSTATEs-to-user-reachable-errors.patchapplication/octet-stream; name=v1-0001-Add-missing-SQLSTATEs-to-user-reachable-errors.patchDownload+26-13
#3Ayush Tiwari
ayushtiwari.slg01@gmail.com
In reply to: Ayush Tiwari (#2)
bugshackers
Re: BUG #19631: currtid2() on a view with GROUP BY ctid crashes with XX000

Hi,

On Wed, 19 Aug 2026 at 16:16, Ayush Tiwari <ayushtiwari.slg01@gmail.com>
wrote:

Hi,

On Wed, 19 Aug 2026 at 14:54, PG Bug reporting form <
noreply@postgresql.org> wrote:

The following bug has been logged on the website:

Bug reference: 19631
Logged by: Zheng Hacker
Email address: hackerzheng666@gmail.com
PostgreSQL version: 19beta3
Operating system: Linux x86_64
Description:

PostgreSQL version: 20devel (commit bdbf662, 2026-08-19)
OS: Linux x86_64

Calling currtid2() on a view whose SELECT includes ctid in a GROUP BY
hits elog(ERROR) without errcode() in tid.c, producing SQLSTATE XX000.

Reproducer:

CREATE TABLE tid_tab (a int);
INSERT INTO tid_tab VALUES (1);
CREATE VIEW tid_view_with_ctid AS
SELECT ctid, a FROM tid_tab GROUP BY ctid, a;
SELECT currtid2('tid_view_with_ctid'::text, '(0,1)'::tid);
-- ERROR: XX000: currtid cannot handle this view
-- LOCATION: currtid_for_view, tid.c:435

Note: the view has a ctid column (so it passes the tididx check at
line 389 which does have a proper errcode), but the GROUP BY prevents
the code from resolving the TLE to a simple base-table Var, so it
falls through to the elog(ERROR) at line 435 which lacks errcode().

Expected: a proper SQLSTATE (e.g. 0A000 feature_not_supported).

Found by automated SQL fuzzing.
Credit: Zheng Wang, Yanjie Zhao, Yiyang Liu

Thanks for the reports. #19629, #19630 and #19631 share one cause: an
error ordinary SQL can reach is raised without an errcode(), so the user
sees XX000.

I scanned the backend for the same pattern, and the attached patch fixes
those three plus four more sites with the same problem: unicode_assigned()
on a non-UTF8 encoding, the pg_control_*() CRC check, a GiST tuple marked
invalid, and ALTER COLLATION ... REFRESH VERSION on "default".

Attaching new patch addressing just #19631 along with
rest files mentioned upthread.

Regards,
Ayush

Attachments:

t253483_3
v1-0001-Fix-internal-errors-reachable-from-SQL.patchapplication/octet-stream; name=v1-0001-Fix-internal-errors-reachable-from-SQL.patchDownload+17-9
#4Pierre Forstmann
pierre.forstmann@gmail.com
In reply to: Ayush Tiwari (#3)
hackersbugs
Re: BUG #19631: currtid2() on a view with GROUP BY ctid crashes with XX000

Hello,

I think that the that SQLSTATE chosen in unicode_assigned() is wrong: this not a SQL statement syntax error but more a feature not supported for the current database encoding;
ERRCODE_FEATURE_NOT_SUPPORTED is more appropriate.

Maybe you could add a regression test for tid.c ?

Other modifications look good to me and more difficult to test automatically.

Regards,
PF

#5Ayush Tiwari
ayushtiwari.slg01@gmail.com
In reply to: PG Bug reporting form (#1)
hackersbugs
Re: BUG #19631: currtid2() on a view with GROUP BY ctid crashes with XX000

Hi,

Thanks for the review!

On Sat, 12 Sept 2026 at 14:46, Pierre Forstmann
<pierre.forstmann@gmail.com> wrote:

I think that the that SQLSTATE chosen in unicode_assigned() is wrong: this not a SQL statement syntax error but more a feature not supported for the current database encoding;
ERRCODE_FEATURE_NOT_SUPPORTED is more appropriate.

Hmm, you are right, added that instead.

Maybe you could add a regression test for tid.c ?

I've added it for now, but personally I don't think it is required.

(PS. This patch is intended for master only)

Attached v2.

Regards,
Ayush

Attachments:

t253483_5
v2-0001-Fix-internal-errors-reachable-from-SQL.patchapplication/octet-stream; name=v2-0001-Fix-internal-errors-reachable-from-SQL.patchDownload+32-9
#6Pierre Forstmann
pierre.forstmann@gmail.com
In reply to: Ayush Tiwari (#5)
hackersbugs
Re: BUG #19631: currtid2() on a view with GROUP BY ctid crashes with XX000

The following review has been posted through the commitfest application:
make installcheck-world: tested, passed
Implements feature: tested, passed
Spec compliant: not tested
Documentation: not tested

Hello,

I have reviewed v2 of patch and it looks good to me.

There is no SQL change: no need to test SQL compliance.

I don't think that documentation should be updated.

Regards,
PF.

The new status of this patch is: Ready for Committer

#7Fujii Masao
masao.fujii@gmail.com
In reply to: Ayush Tiwari (#5)
hackers
Re: BUG #19631: currtid2() on a view with GROUP BY ctid crashes with XX000

On Sun, Sep 13, 2026 at 12:50 AM Ayush Tiwari
<ayushtiwari.slg01@gmail.com> wrote:

I've added it for now, but personally I don't think it is required.

I agree that the additional test isn't necessary here.

- (errmsg("Unicode categorization can only be performed if server
encoding is UTF8")));
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("Unicode categorization can only be performed if server
encoding is UTF8")));

unicode_norm_form_from_string() and str_casefold() have similar encoding
checks, but use ERRCODE_SYNTAX_ERROR. I think
ERRCODE_FEATURE_NOT_SUPPORTED is more appropriate, so I'd keep it here.
Should we change those two checks to use it too?

  if (!crc_ok)
  ereport(ERROR,
- (errmsg("calculated CRC checksum does not match value stored in file")));
+ (errcode(ERRCODE_DATA_CORRUPTED),
+ errmsg("calculated CRC checksum does not match value stored in file")));

perform_base_backup() has the same control-file CRC check without
an errcode(). Should we add ERRCODE_DATA_CORRUPTED there as well?

(PS. This patch is intended for master only)

+1

Regards,

--
Fujii Masao

#8Ayush Tiwari
ayushtiwari.slg01@gmail.com
In reply to: Fujii Masao (#7)
hackers
Re: BUG #19631: currtid2() on a view with GROUP BY ctid crashes with XX000

Hi,

Thanks for the review, Fujii-san!

On Fri, 20 Nov 2026 at 21:13, Fujii Masao <masao.fujii@gmail.com> wrote:

On Sun, Sep 13, 2026 at 12:50 AM Ayush Tiwari
<ayushtiwari.slg01@gmail.com> wrote:

I've added it for now, but personally I don't think it is required.

I agree that the additional test isn't necessary here.

Dropped it in v3.

- (errmsg("Unicode categorization can only be performed if server
encoding is UTF8")));
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("Unicode categorization can only be performed if server
encoding is UTF8")));

unicode_norm_form_from_string() and str_casefold() have similar encoding
checks, but use ERRCODE_SYNTAX_ERROR. I think
ERRCODE_FEATURE_NOT_SUPPORTED is more appropriate, so I'd keep it here.
Should we change those two checks to use it too?

Yeah, makes sense. In v1 I had picked ERRCODE_SYNTAX_ERROR to match
those two, and then only updated unicode_assigned() in v2. Changed both
to ERRCODE_FEATURE_NOT_SUPPORTED in v3 so all three UTF8 checks match.

if (!crc_ok)
ereport(ERROR,
- (errmsg("calculated CRC checksum does not match value stored in file")));
+ (errcode(ERRCODE_DATA_CORRUPTED),
+ errmsg("calculated CRC checksum does not match value stored in file")));

perform_base_backup() has the same control-file CRC check without
an errcode(). Should we add ERRCODE_DATA_CORRUPTED there as well?

Ahh yeah, good catch. Added it there too (the remaining get_controlfile()
callers are all in frontend tools)..

Attached v3.

Regards,
Ayush

Attachments:

t253483_8
v3-0001-Fix-internal-errors-reachable-from-SQL.patchapplication/octet-stream; name=v3-0001-Fix-internal-errors-reachable-from-SQL.patchDownload+21-12
#9Fujii Masao
masao.fujii@gmail.com
In reply to: Ayush Tiwari (#8)
hackers
Re: BUG #19631: currtid2() on a view with GROUP BY ctid crashes with XX000

On Sat, Nov 21, 2026 at 1:59 AM Ayush Tiwari
<ayushtiwari.slg01@gmail.com> wrote:

Attached v3.

Thanks for updating the patch! I've pushed it.

Regards,

--
Fujii Masao