What does regclass signify in Postgresql

后端 未结 2 1196
旧巷少年郎
旧巷少年郎 2021-01-31 07:37

I have the following line in a CREATE TABLE statement:

field1_id bigint DEFAULT nextval(\'table1_field1_id_seq\'::regclass) NOT NULL,

What does

相关标签:
2条回答
  • 2021-01-31 08:25

    No, you do not need the cast to regclass when calling a function like nextval that accepts a regclass parameter, as there is an implict cast from text to regclass. In some other contexts an explicit cast to regclass may be required.

    Explanation:

    ::regclass is a cast, like ::integer.

    regclass is a "magic" data type; it's actually an alias for oid, or "object identifier". See Object identifier types in the documentation. Casting to regclass is a shortcut way of saying "this the name of a relation, please convert it to the oid of that relation". Casts to regclass are aware of the search_path, unlike querying pg_class for a relation's oid directly, so casting to regclass isn't exactly equivalent to subquerying pg_class.

    Tables are relations. So are sequences, and views. So you can get the oid of a view or sequence by casting to regclass too.

    There are implicit casts defined for text to regclass, so if you omit the explicit cast and you're calling a function that accepts regclass the cast is done automatically. So you do not need it in, for example, nextval calls.

    There are other places where you may. For example you can't compare text directly with oid; so you can do this:

    regress=> select * from pg_class where oid = 'table1'::regclass;
    

    but not this:

    regress=> select * from pg_class where oid = 'table1';
    ERROR:  invalid input syntax for type oid: "table1"
    LINE 1: select * from pg_class where oid = 'table1';
    

    Just for fun I tried to write a query that performed the equivalent operation of casting to regclass. Don't use it, it's mostly for fun, and as an attempt to demo what's actually happening. Unless you're really interested in how Pg's guts work you can stop reading here.

    As I understand it, 'sequence_name'::regclass::oid is roughly equivalent to the following query:

    WITH sp(sp_ord, sp_schema) AS (
      SELECT 
        generate_series(1, array_length(current_schemas('t'),1)),
        unnest(current_schemas('t'))
    )
    SELECT c.oid
    FROM pg_class c INNER JOIN pg_namespace n ON (c.relnamespace = n.oid)
    INNER JOIN sp ON (n.nspname = sp.sp_schema)
    WHERE c.relname = 'sequence_name'
    ORDER BY sp.sp_ord
    LIMIT 1;
    

    except that it's a lot shorter and a lot faster. See System information functions for the definition of current_schemas(...), etc.

    In other words:

    • Get a ab array listing all schemas we have access to and pair each entry up with an ordinal number for its position in the array
    • Search pg_class for relations with matching names and associate each with its namespace (schema)
    • Sort the list of remaining relations by the order in which their schemas appeared in search_path
    • and pick the first match
    0 讨论(0)
  • 2021-01-31 08:43

    From what I understand of the documentation, oid are subdivided in types. regclass are database objects representing relations (so that they belong to the metadata table pg_class).

    It expresses a dependency between the sequence and the DEFAULT expression (meaning the process of producing a default value if no explicit value is provided in a INSERT query for instance), so that if one emits a DROP SEQUENCE ... on the sequence, the query won't pass, unless it's cascaded (by writing DROP SEQUENCE table1_field1_id_seq CASCADE).

    0 讨论(0)
提交回复
热议问题