xocolatl / periods

PERIODs and SYSTEM VERSIONING for PostgreSQL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Capitalised role throws error on add_system_versioning

markdulfer opened this issue · comments

Hi,

Trying to add system versioning to a table owned by the role 'TestUser' throws an exception.

PostgreSQL 13 on Windows
Periods extension compiled locally from source
SQL to reproduce:

CREATE ROLE "TestUser";

CREATE TABLE public."TestTable"
(
    "Id" bigint NOT NULL,
    CONSTRAINT "TestTable_pkey" PRIMARY KEY ("Id")
);

ALTER TABLE public."TestTable" owner to "TestUser";

select periods.add_system_time_period('public."TestTable"', 'FromDate', 'ToDate');
select periods.add_system_versioning('public."TestTable"');

Exception thrown:

ERROR:  role ""TestUser"" does not exist
CONTEXT:  SQL statement "ALTER TABLE public."TestTable_history" OWNER TO """TestUser""""
PL/pgSQL function periods.add_system_versioning(regclass,name,name,name,name,name,name) line 147 at EXECUTE
SQL state: 42704

Looking at the sql in periods--1.2.sql, as far as I can tell the table_owner variable will already double quote the user name when converted to text, so the $I formatting in the execute statement causes another double quote, hence the error.

table_owner is set here on line 2410:

    SELECT n.nspname, c.relname, c.relowner, c.relpersistence, c.relkind
    INTO schema_name, table_name, table_owner, persistence, kind
    FROM pg_catalog.pg_class AS c
    JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace
    WHERE c.oid = table_class;

then used here, which is where it gets double quoted again on line 2521 (presumably everywhere table_owner is used with $I):

EXECUTE format('ALTER TABLE %1$I.%2$I OWNER TO %3$I', schema_name, history_table_name, table_owner);

For now I can just user lowercase, but if this project makes it to production casing will be out of my control.

My PostgreSQL knowledge is severely lacking too, sorry. My background is SQL Server.

Thanks,
Mark

Hi and thanks for the report!

I have fixed this locally and will push a new version after I make sure there are no other issues like this one.

Do you know when you can push the new version?

I really like the extension but have the same problem with capitalized roles.

I believe I have found a regression or recurrence of this problem. The table I am trying to add versioning to is called "tabToDo":

SELECT periods.add_period("tabToDo", 'validity', 'creation', 'valid_to');

ERROR:  column "tabToDo" does not exist
LINE 1: SELECT periods.add_period("tabToDo", 'validity', 'creation',...

You need to use single quotes.

Using single quotes automatically de-capitalizes the input. The table name is case sensitive: "tabToDo".

SELECT periods.add_period('tabToDo', 'validity', 'creation', 'valid_to');
ERROR:  relation "tabtodo" does not exist
LINE 1: SELECT periods.add_period('tabToDo', 'validity', 'creation',...

'"tabToDo"'

Ah! Success. Thank you.

SELECT periods.add_period('"tabToDo"', 'validity', 'creation', 'valid_to');
 add_period
------------
 t
(1 row)