Datentyp einer Spalte einer partitionierten Tabelle ändern

christofj

Fleissiger Benutzer
Beiträge
55
Hallo zusammen,

ist es möglich den Datentyp einer Spalte aus einer partitionierten Tabelle zu ändern?
Dieser Versuch:
Code:
ALTER TABLE modul_data_phrases ALTER COLUMN server_id TYPE bytea;
Ergab diesen Fehler:
Code:
FEHLER:  Spalte »server_id« kann nicht automatisch in Typ bytea umgewandelt werden
TIP:  Sie müssen möglicherweise »USING server_id::bytea« angeben.
Kann das mit dem TIP funktionieren?
Wie würde die Syntax dazu lauten?
 
Werbung:
Was für ein Datentyp ist die Spalte? Eine "ID" Spalte als byte array zu definieren klingt aber wie eine sehr, sehr schlechte Idee.
 
Ja, das könnte funktionieren, warum versuchst Du es nicht einmal?

Code:
test=*# create table foo(id int, val int, data text) partition by range (val);
CREATE TABLE
test=*# create table foo_10 partition of foo for values from (1) to (10);
CREATE TABLE
test=*# create table foo_20 partition of foo for values from (11) to (20);
CREATE TABLE
test=*# alter table foo alter COLUMN data type bytea using data::bytea;
ALTER TABLE
test=*# \d+ foo
                                    Table "public.foo"
 Column |  Type   | Collation | Nullable | Default | Storage  | Stats target | Description
--------+---------+-----------+----------+---------+----------+--------------+-------------
 id     | integer |           |          |         | plain    |              |
 val    | integer |           |          |         | plain    |              |
 data   | bytea   |           |          |         | extended |              |
Partition key: RANGE (val)
Partitions: foo_10 FOR VALUES FROM (1) TO (10),
            foo_20 FOR VALUES FROM (11) TO (20)

test=*#
 
Danke schon mal,
Die neue Fehlermeldung:
Code:
=# ALTER TABLE modul_data_phrases ALTER COLUMN server_id TYPE bytea USING server_id::bytea;
FEHLER:  kann Typ numeric nicht in Typ bytea umwandeln                                                                                                                                                                                                                        
ZEILE 1: ...es ALTER COLUMN server_id TYPE bytea USING server_id::bytea;
 
Code:
test=*# select 10::numeric::bytea;
FEHLER:  kann Typ numeric nicht in Typ bytea umwandeln
LINE 1: select 10::numeric::bytea;
                          ^
test=*# select 10::numeric::text::bytea;
 bytea  
--------
 \x3130
(1 row)

test=*#

Deine Datentypen erscheinen seltsam ...
 
Ok, hat funktioniert. (Die id hat nichts mit der ID einer Tabelle zu tun, es handelt sich um eine uuid des Servers die ich dokumentieren möchte.)
 
Werbung:
Da stimme ich akretschmer zu: für UUIDs sollte Datentyp uuid verwenden werden. Ein byte array ist weniger effizient, unpraktisch und macht das Leben nur schwerer.
 
Zurück
Oben