pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedAW: Cast INTEGER to BIT confusion
4+ messages / 2 participants
[nested] [flat]
* AW: Cast INTEGER to BIT confusion
@ 2023-08-15 11:59 [Quipsy] Markus Karg <karg@quipsy.de>
1 sibling, 1 reply; 4+ messages in thread
From: [Quipsy] Markus Karg @ 2023-08-15 11:59 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Thank you, Erik! Prefixing the search path in fact looks very interesting, and I think in this particular application it is a safe (and the only) solution.
Is setting the search path something that has to be done for each new connection / each user, or is this something static and global for the database?
Thanks a lot!
-Markus
-----Ursprüngliche Nachricht-----
Von: Erik Wienhold <ewie@ewie.name>
Gesendet: Dienstag, 15. August 2023 13:48
An: [Quipsy] Markus Karg <karg@quipsy.de>; pgsql-general@lists.postgresql.org
Betreff: Re: Cast INTEGER to BIT confusion
> On 15/08/2023 10:49 CEST [Quipsy] Markus Karg <karg@quipsy.de> wrote:
>
> Hello PostgreSQL Community,
>
> I like to store just a single bit but that can be either 1 or 0, so I
> tried to do this:
>
> CREATE TABLE T (c BIT);
> INSERT INTO T VALUES (1);
> -- I MUST NOT change both lines in any way as these are part of a third-party application!
>
> Unfortunately this tells me:
>
> column "c" is of type bit but expression is of type integer
>
> So I logged in as a cluster admin and I tried this:
>
> CREATE CAST (integer AS bit) WITH INOUT AS IMPLICIT;
>
> Unfortunately that tells me:
>
> cast from type integer to type bit already exists
>
> This is confusing! Apparently PostgreSQL 15.3 comes with the needed
> cast out-of-the-box but it does not apply it? This is confusing!
>
> What is my fault?
The built-in cast is explicit (castcontext = 'e'):
=# SELECT * FROM pg_cast WHERE castsource = 'int'::regtype AND casttarget = 'bit'::regtype;
oid | castsource | casttarget | castfunc | castcontext | castmethod
-------+------------+------------+----------+-------------+------------
10186 | 23 | 1560 | 1683 | e | f
(1 row)
It's not possible to drop that cast and replace it with a custom one:
=# DROP CAST (int AS bit);
ERROR: cannot drop cast from integer to bit because it is required by the database system
You could create a custom domain if you're only interested in values 0 and 1 and don't use bit string functions. The search path must be changed so that domain bit overrides pg_catalog.bit:
=# CREATE SCHEMA xxx;
=# CREATE DOMAIN xxx.bit AS int;
=# SET search_path = xxx, pg_catalog;
=# CREATE TABLE t (c bit);
=# INSERT INTO t VALUES (1);
INSERT 0 1
But I would do that only if the third-party code cannot be tweaked because the custom domain could be confusing. It's also prone to errors as it relies on a specific search path order. Also make sure that regular users cannot create objects in schema xxx that would override objects in pg_catalog.
--
Erik
^ permalink raw reply [nested|flat] 4+ messages in thread
* AW: Cast INTEGER to BIT confusion
@ 2023-08-15 12:02 [Quipsy] Markus Karg <karg@quipsy.de>
1 sibling, 1 reply; 4+ messages in thread
From: [Quipsy] Markus Karg @ 2023-08-15 12:02 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Erik,
I just tried out your proposal on PostgreSQL 15.3 and this is the result:
ERROR: column "c" is of type bit but expression is of type integer
LINE 5: INSERT INTO t VALUES (1);
^
HINT: You will need to rewrite or cast the expression.
Apparently the search path is ignored?!
-Markus
-----Ursprüngliche Nachricht-----
Von: Erik Wienhold <ewie@ewie.name>
Gesendet: Dienstag, 15. August 2023 13:48
An: [Quipsy] Markus Karg <karg@quipsy.de>; pgsql-general@lists.postgresql.org
Betreff: Re: Cast INTEGER to BIT confusion
> On 15/08/2023 10:49 CEST [Quipsy] Markus Karg <karg@quipsy.de> wrote:
>
> Hello PostgreSQL Community,
>
> I like to store just a single bit but that can be either 1 or 0, so I
> tried to do this:
>
> CREATE TABLE T (c BIT);
> INSERT INTO T VALUES (1);
> -- I MUST NOT change both lines in any way as these are part of a third-party application!
>
> Unfortunately this tells me:
>
> column "c" is of type bit but expression is of type integer
>
> So I logged in as a cluster admin and I tried this:
>
> CREATE CAST (integer AS bit) WITH INOUT AS IMPLICIT;
>
> Unfortunately that tells me:
>
> cast from type integer to type bit already exists
>
> This is confusing! Apparently PostgreSQL 15.3 comes with the needed
> cast out-of-the-box but it does not apply it? This is confusing!
>
> What is my fault?
The built-in cast is explicit (castcontext = 'e'):
=# SELECT * FROM pg_cast WHERE castsource = 'int'::regtype AND casttarget = 'bit'::regtype;
oid | castsource | casttarget | castfunc | castcontext | castmethod
-------+------------+------------+----------+-------------+------------
10186 | 23 | 1560 | 1683 | e | f
(1 row)
It's not possible to drop that cast and replace it with a custom one:
=# DROP CAST (int AS bit);
ERROR: cannot drop cast from integer to bit because it is required by the database system
You could create a custom domain if you're only interested in values 0 and 1 and don't use bit string functions. The search path must be changed so that domain bit overrides pg_catalog.bit:
=# CREATE SCHEMA xxx;
=# CREATE DOMAIN xxx.bit AS int;
=# SET search_path = xxx, pg_catalog;
=# CREATE TABLE t (c bit);
=# INSERT INTO t VALUES (1);
INSERT 0 1
But I would do that only if the third-party code cannot be tweaked because the custom domain could be confusing. It's also prone to errors as it relies on a specific search path order. Also make sure that regular users cannot create objects in schema xxx that would override objects in pg_catalog.
--
Erik
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: AW: Cast INTEGER to BIT confusion
@ 2023-08-15 14:27 Erik Wienhold <ewie@ewie.name>
parent: [Quipsy] Markus Karg <karg@quipsy.de>
0 siblings, 0 replies; 4+ messages in thread
From: Erik Wienhold @ 2023-08-15 14:27 UTC (permalink / raw)
To: [Quipsy] Markus Karg <karg@quipsy.de>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
> On 15/08/2023 13:59 CEST [Quipsy] Markus Karg <karg@quipsy.de> wrote:
>
> Is setting the search path something that has to be done for each new
> connection / each user, or is this something static and global for the
> database?
The search path is set per connection and the initial search path can be
configured on the database and/or individual roles:
1. ALTER DATABASE mydb SET search_path = ...
2. ALTER ROLE myrole SET search_path = ...
3. ALTER ROLE myrole IN DATABASE mydb SET search_path = ...
Those three statements do not affect your current connection though. Users can
still set a different search path after connecting.
--
Erik
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: AW: Cast INTEGER to BIT confusion
@ 2023-08-15 14:27 Erik Wienhold <ewie@ewie.name>
parent: [Quipsy] Markus Karg <karg@quipsy.de>
0 siblings, 0 replies; 4+ messages in thread
From: Erik Wienhold @ 2023-08-15 14:27 UTC (permalink / raw)
To: [Quipsy] Markus Karg <karg@quipsy.de>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
> On 15/08/2023 14:02 CEST [Quipsy] Markus Karg <karg@quipsy.de> wrote:
>
> I just tried out your proposal on PostgreSQL 15.3 and this is the result:
>
> ERROR: column "c" is of type bit but expression is of type integer
> LINE 5: INSERT INTO t VALUES (1);
> ^
> HINT: You will need to rewrite or cast the expression.
>
> Apparently the search path is ignored?!
>
> -----Ursprüngliche Nachricht-----
> Von: Erik Wienhold <ewie@ewie.name>
> Gesendet: Dienstag, 15. August 2023 13:48
> An: [Quipsy] Markus Karg <karg@quipsy.de>; pgsql-general@lists.postgresql.org
> Betreff: Re: Cast INTEGER to BIT confusion
>
> You could create a custom domain if you're only interested in values 0 and 1
> and don't use bit string functions. The search path must be changed so that
> domain bit overrides pg_catalog.bit:
>
> =# CREATE SCHEMA xxx;
> =# CREATE DOMAIN xxx.bit AS int;
> =# SET search_path = xxx, pg_catalog;
> =# CREATE TABLE t (c bit);
> =# INSERT INTO t VALUES (1);
> INSERT 0 1
>
> But I would do that only if the third-party code cannot be tweaked because
> the custom domain could be confusing. It's also prone to errors as it relies
> on a specific search path order. Also make sure that regular users cannot
> create objects in schema xxx that would override objects in pg_catalog.
Hmm, I thought that Postgres resolves all types through the search path, but
apparently that is not the case for built-in types. I never used this to
override built-in types so this is a surprise to me. (And obviously I haven't
tested the search path feature before posting.)
Neither [1] or [2] mention that special (?) case or if there's a distinction
between built-in types and user-defined types. The USAGE privilege is required
according to [2] but I was testing as superuser anyway.
[1] https://www.postgresql.org/docs/15/ddl-schemas.html
[2] https://www.postgresql.org/docs/15/runtime-config-client.html#GUC-SEARCH-PATH
--
Erik
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2023-08-15 14:27 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-08-15 11:59 ` [Quipsy] Markus Karg <karg@quipsy.de>
2023-08-15 14:27 ` Erik Wienhold <ewie@ewie.name>
2023-08-15 12:02 ` [Quipsy] Markus Karg <karg@quipsy.de>
2023-08-15 14:27 ` Erik Wienhold <ewie@ewie.name>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox