[PATCH] Support casts from numeric to oid8

Started by Koshi Shibagaki (Fujitsu)about 1 month ago5 messageshackers

Thread notes

1 note#feature#minor#enhancement
hackorum-bot#feature#minor#enhancement
hackorum-bot
2 days ago

@global #feature #minor #enhancement

#feature#minor#enhancement
Jump to latest
#1Koshi Shibagaki (Fujitsu)
shibagaki.koshi@fujitsu.com

Hi,

Is it intentional that this cast fails, even though the value fits in
oid8?

SELECT 18446744073709551615::oid8;
ERROR: cannot cast type numeric to oid8

An integer literal this large is parsed as numeric, so the cast fails
because there is no numeric-to-oid8 cast. '18446744073709551615'::oid8
works.

The built-in signed integer types cannot represent the upper half of
oid8's range. Adding a numeric-to-oid8 cast would allow integer values
across that full range to be converted exactly. This would let N::oid8
work across the full range without changing how the value is written
when it exceeds INT64_MAX.

Attached is a patch adding an assignment cast. An implicit cast would
round fractional values in comparisons. For example,

SELECT 2::oid8 = 1.5;

would return true because 1.5 would be rounded to 2.

The earlier discussion says negative text input wraps around
intentionally [1]/messages/by-id/aV2WL3ciOySe0PLi@paquier.xyz. The patch does the same for numeric input.

Comments welcome.

[1]: /messages/by-id/aV2WL3ciOySe0PLi@paquier.xyz

Koshi Shibagaki
FUJITSU LIMITED
https://www.fujitsu.com/

Attachments:

t254340_1
v1-0001-Add-an-assignment-cast-from-numeric-to-oid8.patchapplication/octet-stream; name=v1-0001-Add-an-assignment-cast-from-numeric-to-oid8.patchDownload+109-2
#2Aleksander Alekseev
aleksander@tigerdata.com
In reply to: Koshi Shibagaki (Fujitsu) (#1)
Re: [PATCH] Support casts from numeric to oid8

Hi Koshi,

Attached is a patch adding an assignment cast.

The patch looks correct but its value is debatable.

Is it intentional that this cast fails, even though the value fits in
oid8?

My best guess is that no one actually needed this cast before. Could
you please describe your case in more detail?

As an observation we don't support this for xid8 either:

```
=# SELECT 18446744073709551615::xid8;
ERROR: cannot cast type numeric to xid8
```

--
Best regards,
Aleksander Alekseev

#3Koshi Shibagaki (Fujitsu)
shibagaki.koshi@fujitsu.com
In reply to: Aleksander Alekseev (#2)
Re: [PATCH] Support casts from numeric to oid8

Hi Aleksander,

Could you please describe your case in more detail?

I noticed this when I was adding oid8 support to rust-postgres [1]https://github.com/rust-postgres/rust-postgres/pull/1392.
I don't have a concrete use case at the moment.

I thought it was inconsistent that 9223372036854775807::oid8 works, but
9223372036854775808::oid8 fails. Both values are within the range of oid8,
so I expected both casts to work. That's why I sent the patch.

[1]: https://github.com/rust-postgres/rust-postgres/pull/1392

Koshi Shibagaki
FUJITSU LIMITED
https://www.fujitsu.com/

#4Aleksander Alekseev
aleksander@tigerdata.com
In reply to: Koshi Shibagaki (Fujitsu) (#3)
Re: [PATCH] Support casts from numeric to oid8

Hi Koshi,

I thought it was inconsistent that 9223372036854775807::oid8 works, but
9223372036854775808::oid8 fails. Both values are within the range of oid8,
so I expected both casts to work. That's why I sent the patch.

Then I wonder what could be the use case for the existing partial
conversion. Perhaps instead of adding more casts we should remove the
existing ones in order to make it consistent with xid8.

I believe we need more opinions from the community before moving any
patches in this area forward.

--
Best regards,
Aleksander Alekseev

#5Daniel Gustafsson
daniel@yesql.se
In reply to: Aleksander Alekseev (#4)
Re: [PATCH] Support casts from numeric to oid8

On 25 Nov 2026, at 09:33, Aleksander Alekseev <aleksander@tigerdata.com> wrote:

I thought it was inconsistent that 9223372036854775807::oid8 works, but
9223372036854775808::oid8 fails. Both values are within the range of oid8,
so I expected both casts to work. That's why I sent the patch.

Then I wonder what could be the use case for the existing partial
conversion. Perhaps instead of adding more casts we should remove the
existing ones in order to make it consistent with xid8.

Historically we've been pretty careful about deprecating and removing
functionality which isn't blatantly broken, and this doesn't really fall in
that category IMO. That being said, I can't imagine what anyone would use it
for, and I agree that the proposed cast likely will be code only ever
excercised in a test suite.

--
Daniel Gustafsson