alter varchar field to super returns " ERROR: target data type "super" is not supported" - amazon-redshift

I have a varchar field in super/json format:
select detail
from mytable
limit 1;
{"child_category":"organize","gallery_id":"123456","detail":"[\"789876"]"}
I know that detail is currently varchar because inspecting the table in my client shows it as varchar(32768)
I want alter this field to be super:
ALTER TABLE mytable ALTER COLUMN detail TYPE super;
Returns:
[0A000] ERROR: target data type "super" is not supported
How can I cast the detail field as a super field?

From the doc https://docs.aws.amazon.com/redshift/latest/dg/r_ALTER_TABLE.html
with alter column you can only change varchar size.
As a suggestion, you can add new temp column, drop first, and rename temp column
alter table mytable add column temp_super super;
update mytable set temp_super = json_parse(detail);
alter table mytable drop column detail;
alter table mytable rename column temp_super to detail;

Related

How to convert JSONB[] to JSONB?

I have this table with existing data:
CREATE TABLE table (
col JSONB[],
);
How can I convert col to JSONB now without dropping the column?
I tried:
ALTER TABLE table
ALTER COLUMN col TYPE JSONB USING col::jsonb
But it says cannot cast type jsonb[] to jsonb
This did the trick:
ALTER TABLE table
ALTER COLUMN blocks col JSONB USING to_json(col)

I'm getting column "my_column" contains null values' when adding a composite primary key

Is it not supposed to delete null values before altering the table? I'm confused...
My query looks roughly like this:
BEGIN;
DELETE FROM my_table
WHERE my_column IS NULL;
ALTER TABLE my_table DROP CONSTRAINT my_table_pk;
ALTER TABLE my_table ADD PRIMARY KEY (id, my_column);
-- this is to repopulate the data afterwards
INSERT INTO my_table (name, other_table_id, my_column)
SELECT
ya.name,
ot.id,
my_column
FROM other_table ot
LEFT JOIN yet_another ya
ON ya.id = ot."fileId"
WHERE NOT EXISTS (
SELECT
1
FROM my_table mt
WHERE ot.id = mt.other_table_id AND ot.my_column = mt.my_column
) AND my_column IS NOT NULL;
COMMIT;
sorry for naming
There are two possible explanations:
A concurrent session inserted a new row with a NULL value between the start of the DELETE and the start of ALTER TABLE.
To avoid that, lock the table in SHARE mode before you DELETE.
There is a row where id has a NULL value.

Alter column default value is not working in postgres

I tried to update column's default value with the below queries in postgres. But seems its not working. May be I am missing something. Could you help?
ALTER TABLE tableName ADD COLUMN newColumn INTEGER DEFAULT 0;
ALTER TABLE tableName ALTER COLUMN newColumn DROP DEFAULT;
or
ALTER TABLE tableName ALTER COLUMN newColumn SET DEFAULT NULL;
SELECT * FROM tableName;
Here I still find 0.
The change only applies to new records. After the modification you have to heal all the previous data with a migration like this:
UPDATE tableName SET newColumn = NULL WHERE newColumn = 0

Unable to change field type to not null with default value

I have a simple SQL statement, which looks like so:
alter table my_table alter column my_field set data type numeric(12,4) not null default 0;
But I get an error message, that points to not. What is wrong with that?
Use separate ALTER COLUMN clauses for the type, null behavior, and default value:
ALTER TABLE my_table
ALTER COLUMN my_field TYPE numeric(12,4),
ALTER COLUMN my_field SET DEFAULT 0,
ALTER COLUMN my_field SET NOT NULL;

Change column type and set not null

How do you change the column type and also set that column to not null together?
I am trying:
ALTER TABLE mytable ALTER COLUMN col TYPE character varying(15) SET NOT NULL
This returns an error.
What is the right syntax?
This should be correct:
ALTER TABLE mytable
ALTER COLUMN col TYPE character varying(15),
ALTER COLUMN col SET NOT NULL
Also, if you want to REMOVE NOT NULL constrain in postgresql:
ALTER TABLE mytable
ALTER COLUMN email DROP NOT NULL;