Skip to content

Why not use 'create table if not exist' ? #43

@soyking

Description

@soyking

The before-insert trigger in Postgres is something like:

IF NOT EXISTS(
    SELECT 1 FROM information_schema.tables WHERE table_name=tablename)
THEN
    BEGIN
        EXECUTE 'CREATE TABLE ' || tablename || ' (
            CHECK (' || checks || '),
            LIKE "{{parent_table}}" INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING INDEXES
        ) INHERITS ("{{parent_table}}");';
    EXCEPTION WHEN duplicate_table THEN
        -- pass
    END;
END IF;

And it will check whether table's existense by SELETC 1, then create table if select failed.I tried to replace with CREATE TABLE IF NOT EXIST

BEGIN
    EXECUTE 'CREATE TABLE IF NOT EXISTS ' || tablename || ' (
        CHECK (' || checks || '),
        LIKE "{{parent_table}}" INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING INDEXES
    ) INHERITS ("{{parent_table}}");';
EXCEPTION WHEN duplicate_table THEN
    -- pass
END;

In my case, it will speed up insert action about 25% (I really care about the decrease of insert action's speed). But I doubt that it is too special, so I open this issue to discuss about it. Thanks!

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions