Check if a user-defined type already exists in PostgreSQL

后端 未结 10 1532
离开以前
离开以前 2020-12-29 01:43

Say I have created some user-defined types in the DB,

i.e. CREATE TYPE abc ...

Is it then possible to determine if the user-defined type exists

相关标签:
10条回答
  • 2020-12-29 02:41

    I'm trying to do the same thing, ensure a type exists.

    I started psql with the --echo-hidden (-E) option and entered \dT:

    $ psql -E
    psql (9.1.9)
    testdb=> \dT
    ********* QUERY **********
    SELECT n.nspname as "Schema",
      pg_catalog.format_type(t.oid, NULL) AS "Name",
      pg_catalog.obj_description(t.oid, 'pg_type') as "Description"
    FROM pg_catalog.pg_type t
         LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
    WHERE (t.typrelid = 0 OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid))
      AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)
          AND n.nspname <> 'pg_catalog'
          AND n.nspname <> 'information_schema'
      AND pg_catalog.pg_type_is_visible(t.oid)
    ORDER BY 1, 2;
    **************************
     List of data types
     Schema |       Name       | Description 
    --------+------------------+-------------
     public | errmsg_agg_state | 
    (1 row)
    

    If you are using schemas and search_path (I am) then you'll probably need to keep the pg_catalog.pg_type_is_visible(t.oid) check. I don't know what all the conditions in the WHERE are doing, but they didn't seem relevant to my case. Currently using:

    SELECT 1 FROM pg_catalog.pg_type as t
       WHERE typname = 'mytype' AND pg_catalog.pg_type_is_visible(t.oid);
    
    0 讨论(0)
  • 2020-12-29 02:45

    You can look in the pg_type table:

    select exists (select 1 from pg_type where typname = 'abc');
    

    If that is true then abc exists.

    0 讨论(0)
  • 2020-12-29 02:45

    Indeed, Postgres does not have CREATE OR REPLACE functionality for types. So the best approach is to drop it:

    DROP TYPE IF EXISTS YOUR_TYPE;
    CREATE TYPE YOUR_TYPE AS (
        id      integer,
        field   varchar
    );
    

    Simple solution is always the best one.

    0 讨论(0)
  • 2020-12-29 02:45
    -- All of this to create a type if it does not exist
    CREATE OR REPLACE FUNCTION create_abc_type() RETURNS integer AS $$
    DECLARE v_exists INTEGER;
    
    BEGIN
        SELECT into v_exists (SELECT 1 FROM pg_type WHERE typname = 'abc');
        IF v_exists IS NULL THEN
            CREATE TYPE abc AS ENUM ('height', 'weight', 'distance');
        END IF;
        RETURN v_exists;
    END;
    $$ LANGUAGE plpgsql;
    
    -- Call the function you just created
    SELECT create_abc_type();
    
    -- Remove the function you just created
    DROP function create_abc_type();
    -----------------------------------
    
    0 讨论(0)
提交回复
热议问题