Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with arrays #28

Open
RomanTkachuk opened this issue Feb 7, 2017 · 3 comments
Open

Problem with arrays #28

RomanTkachuk opened this issue Feb 7, 2017 · 3 comments

Comments

@RomanTkachuk
Copy link

http://dba.stackexchange.com/questions/163340/temporal-tables-extension-error-with-array-columns/163459#163459do not

CREATE EXTENSION IF NOT EXISTS temporal_tables;

DROP TABLE IF EXISTS test;
DROP TABLE IF EXISTS test_history;

CREATE TABLE test
(
id SERIAL PRIMARY KEY,
a integer,
directories text[],
sys_period tstzrange NOT NULL
);

CREATE TABLE test_history (LIKE test);
CREATE TRIGGER versioning_trigger BEFORE INSERT OR UPDATE OR DELETE ON test FOR EACH ROW EXECUTE PROCEDURE versioning('sys_period', 'test_history', true);
And in separate transactions the following two commands:

INSERT INTO test(a) VALUES (1);
UPDATE test SET a = 5 WHERE id = 1;
I get the following error:

ERROR: column "directories" of relation "test" is of type text[] but column "directories" of history relation "test_history" is of type text[]

I found than problem is in check_attr where equal attndims. Main problem than heap_openrv do not correctly fill it for tables created by LIKE. Workaround - create the same table without LIKE

@arkhipov
Copy link
Owner

arkhipov commented Feb 7, 2017

Hello @RomanTkachuk ,
It is a well-known PostgreSQL bug.

@RomanTkachuk
Copy link
Author

RomanTkachuk commented Feb 7, 2017 via email

@mdrokz
Copy link

mdrokz commented Jan 12, 2024

http://dba.stackexchange.com/questions/163340/temporal-tables-extension-error-with-array-columns/163459#163459do not

CREATE EXTENSION IF NOT EXISTS temporal_tables;

DROP TABLE IF EXISTS test; DROP TABLE IF EXISTS test_history;

CREATE TABLE test ( id SERIAL PRIMARY KEY, a integer, directories text[], sys_period tstzrange NOT NULL );

CREATE TABLE test_history (LIKE test); CREATE TRIGGER versioning_trigger BEFORE INSERT OR UPDATE OR DELETE ON test FOR EACH ROW EXECUTE PROCEDURE versioning('sys_period', 'test_history', true); And in separate transactions the following two commands:

INSERT INTO test(a) VALUES (1); UPDATE test SET a = 5 WHERE id = 1; I get the following error:

ERROR: column "directories" of relation "test" is of type text[] but column "directories" of history relation "test_history" is of type text[]

I found than problem is in check_attr where equal attndims. Main problem than heap_openrv do not correctly fill it for tables created by LIKE. Workaround - create the same table without LIKE

I also encountered the same issue and i was able to fix it by re altering the array columns with the same type

heres the script to do this for all history tables

DO $$ 
DECLARE
    current_table_name text;
    current_column_name text;
    current_type text;
    current_data_type text;
    new_type text;
BEGIN
    RAISE NOTICE 'Fixing array columns';
    FOR current_table_name IN (SELECT table_name FROM information_schema.tables WHERE table_name LIKE '%_history') 
    LOOP
        RAISE NOTICE 'Table %', current_table_name;
        FOR current_column_name, current_type, current_data_type IN 
            (SELECT column_name, udt_name, data_type 
             FROM information_schema.columns 
             WHERE table_name = current_table_name AND data_type LIKE 'ARRAY')
        LOOP
            -- Assuming you want to change the number of dimensions to 1 for array columns
            RAISE NOTICE 'Column % in table % has type %', current_column_name, current_table_name, current_type;
            IF current_data_type LIKE 'ARRAY' THEN
                new_type := substr(current_type, 2) || '[]';
                EXECUTE 'ALTER TABLE ' || current_table_name || ' ALTER COLUMN ' || current_column_name || ' TYPE ' || new_type;
                RAISE NOTICE 'Altered column % in table % to type %', current_column_name, current_table_name, new_type;
            END IF;
        END LOOP;
    END LOOP;
END $$;

Leaving this here in case anybody encounters the same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants