Postgresql 在Postgres 13中,tsrange字段需要多少字节?(俄罗斯方块专栏)

Postgresql 在Postgres 13中,tsrange字段需要多少字节?(俄罗斯方块专栏),postgresql,range,Postgresql,Range,当我在一张大桌子上玩俄罗斯方块列时,我试图找出tsrange字段的字节要求 我没有在手册中找到详细说明,所以我尝试了pg_类型: 根据这些结果: typlen typalign typname 8 d timestamp -1 d tsrange 文档解释说,d的typealign表示许多机器上的双对齐8字节,但决不是全部。一个-1的typelen表示一个“varlena”类型,它有一个长单词 如果我读对了,那么tsrange是可

当我在一张大桌子上玩俄罗斯方块列时,我试图找出tsrange字段的字节要求

我没有在手册中找到详细说明,所以我尝试了pg_类型:

根据这些结果:

typlen  typalign   typname
 8      d          timestamp
-1      d          tsrange
文档解释说,d的typealign表示许多机器上的双对齐8字节,但决不是全部。一个-1的typelen表示一个“varlena”类型,它有一个长单词

如果我读对了,那么tsrange是可变长度的,最多8+8+8=24字节?是这样吗?在这种情况下,我会将其放置在表的末尾/右侧,以及其他可变长度字段,如text或citext

听起来对吗?出于好奇,为什么它是可变长度类型

我想我应该用一个例子来充实我的问题,说明我在追求什么。也就是说,在其他列中,找出列的位置,以实现最佳存储布局。巨大的桌子,可能真的很重要。我猜那个顺序应该是

fixed length types, from largest to smallest: uuid-bigint-integer, etc.
variable length types, with tsrange first
这并不是我的俄罗斯方块脚本目前给出建议顺序的方式。脚本提出了一些毫无意义的建议,建议重新排列具有等效类型长度和填充的列……这不是问题所在。这不妨碍我。问题是,具有8字节对齐方式的可变长度类型与具有4字节对齐方式的可变长度类型属于何处?从左开始还是从右结束

表定义 俄罗斯方块结果 选择pg_column_sizetsrangelocaltimestamp,localtimestamp返回9pg_column_大小,很好,谢谢!我刚刚在一张有tsrange的桌子上运行了这个,我得到的长度是6和22。用check_it as select pg_column_sizefrom_to_range as sz from edge select sz,count*from check_it group by sz order by sz
fixed length types, from largest to smallest: uuid-bigint-integer, etc.
variable length types, with tsrange first
DROP TABLE IF EXISTS ascendco.edge CASCADE;

CREATE TABLE IF NOT EXISTS ascendco.edge (
    id                           uuid        NOT NULL DEFAULT NULL,
    inv_id                       uuid        NOT NULL DEFAULT NULL,
    from_scan_at_facility_id     uuid        NOT NULL DEFAULT NULL,
    from_associated_to_id        uuid        NOT NULL DEFAULT NULL,
    to_associated_to_id          uuid        NOT NULL DEFAULT NULL,
    from_web_user_id             uuid        NOT NULL DEFAULT NULL,
    from_assembly_id             uuid        NOT NULL DEFAULT NULL,
    from_scan_id                 uuid        NOT NULL DEFAULT NULL,
    from_sterilizer_load_id      uuid        NOT NULL DEFAULT NULL,
    from_facility_location_id    uuid        NOT NULL DEFAULT NULL,
    from_facility_id             uuid        NOT NULL DEFAULT NULL,
    from_washer_load_id          uuid        NOT NULL DEFAULT NULL,
    from_scase_id                uuid        NOT NULL DEFAULT NULL,
    from_clinic_id               uuid        NOT NULL DEFAULT NULL,
    from_facility_department_id  uuid        NOT NULL DEFAULT NULL,
    hsys_id                      uuid        NOT NULL DEFAULT NULL,
    to_scan_id                   uuid        NOT NULL DEFAULT NULL,
    to_scan_at_facility_id       uuid        NOT NULL DEFAULT NULL,
    to_web_user_id               uuid        NOT NULL DEFAULT NULL,
    to_facility_location_id      uuid        NOT NULL DEFAULT NULL,
    to_sterilizer_load_id        uuid        NOT NULL DEFAULT NULL,
    to_washer_load_id            uuid        NOT NULL DEFAULT NULL,
    to_facility_id               uuid        NOT NULL DEFAULT NULL,
    to_scase_id                  uuid        NOT NULL DEFAULT NULL,
    to_clinic_id                 uuid        NOT NULL DEFAULT NULL,
    to_facility_department_id    uuid        NOT NULL DEFAULT NULL,
    to_assembly_id               uuid        NOT NULL DEFAULT NULL,
    num_inst                     integer     NOT NULL DEFAULT 0,
    seconds                      integer     NOT NULL DEFAULT 0,
    from_node_dts                timestamp   NOT NULL DEFAULT NULL,
    to_node_dts                  timestamp   NOT NULL DEFAULT NULL,
    created_dts                  timestamp   NOT NULL DEFAULT NULL,
    updated_dts                  timestamp   NOT NULL DEFAULT NULL,
    from_to_range                tsrange     GENERATED ALWAYS AS (tsrange(from_node_dts, to_node_dts)) STORED,
    sequence_                    citext      NOT NULL DEFAULT NULL,
    from_node                    citext      NOT NULL DEFAULT NULL,
    to_node                      citext      NOT NULL DEFAULT NULL,
    from_to_node                 citext      NOT NULL DEFAULT NULL,
    from_associated_to           citext      NOT NULL DEFAULT NULL,
    to_associated_to             citext      NOT NULL DEFAULT NULL,
    from_user_name               citext      NOT NULL DEFAULT NULL,
    source_                      citext      NOT NULL DEFAULT NULL,
    is_fake                      citext      NOT NULL DEFAULT NULL,
    to_user_name                 citext      NOT NULL DEFAULT NULL,

CONSTRAINT edge_id_pkey
    PRIMARY KEY (id),

CONSTRAINT finishes_after_starts
    CHECK (from_node_dts <= to_node_dts) -- Make sure that we don't end up with *any* records with a start time later than the end time. = is fine.
);
/*
 Table_in is a 'regclass'. It's a way of avoiding SQL injection problems,
not that there is a chance of that here. (No dynamic SQL.) The really nice
part is that you can pass in table_name or schema_name.table_name this way
and either work magically.
*/

CREATE OR REPLACE FUNCTION dba.column_tetris (table_in regclass)
    RETURNS table (
      column_name           text,
      type_name             text,
      type_align            text,
      alignment_description text,
      type_length           int2,
      suggestioned_position int8,
      current_position      int2
    )

AS $BODY$

   SELECT a.attname::text  AS column_name,
          t.typname::text  AS type_name,
          t.typalign::text AS type_align,

          CASE
             WHEN typalign = 'c' THEN 'char alignment, no alignment needed'
             WHEN typalign = 's' THEN 'short alignment, 2 bytes on most machines'
             WHEN typalign = 'i' THEN 'int alignment, 4 bytes on most machines'
             WHEN typalign = 'd' THEN 'double alignment, 8 bytes on many machines, but by no means all'
             ELSE 'Unexpected typalign ' || typalign
            END AS alignment_description,

          t.typlen   AS type_length,
          ROW_NUMBER () OVER (ORDER BY t.typlen DESC,a.attname) as suggested_position,
          a.attnum   AS currrent_position

     FROM pg_class c
     JOIN pg_attribute a ON (a.attrelid = c.oid)
     JOIN pg_type t ON (t.oid = a.atttypid)

    WHERE c.relname = table_in::name AND
          a.attnum >= 0

 ORDER BY t.typlen DESC,a.attname;

$BODY$
    LANGUAGE sql;

COMMENT ON FUNCTION dba.column_tetris (regclass) IS '
/*
Adapted from this excellent blog post:
https://blog.2ndquadrant.com/on-rocks-and-sand/
*/';

+-----------------------------+-----------+------------+-----------------------------------------------------------------+-------------+-----------------------+------------------+
| column_name                 | type_name | type_align | alignment_description                                           | type_length | suggestioned_position | current_position |
+-----------------------------+-----------+------------+-----------------------------------------------------------------+-------------+-----------------------+------------------+
| from_assembly_id            | uuid      | c          | char alignment, no alignment needed                             | 16          | 1                     | 7                |
| from_associated_to_id       | uuid      | c          | char alignment, no alignment needed                             | 16          | 2                     | 4                |
| from_clinic_id              | uuid      | c          | char alignment, no alignment needed                             | 16          | 3                     | 14               |
| from_facility_department_id | uuid      | c          | char alignment, no alignment needed                             | 16          | 4                     | 15               |
| from_facility_id            | uuid      | c          | char alignment, no alignment needed                             | 16          | 5                     | 11               |
| from_facility_location_id   | uuid      | c          | char alignment, no alignment needed                             | 16          | 6                     | 10               |
| from_scan_at_facility_id    | uuid      | c          | char alignment, no alignment needed                             | 16          | 7                     | 3                |
| from_scan_id                | uuid      | c          | char alignment, no alignment needed                             | 16          | 8                     | 8                |
| from_scase_id               | uuid      | c          | char alignment, no alignment needed                             | 16          | 9                     | 13               |
| from_sterilizer_load_id     | uuid      | c          | char alignment, no alignment needed                             | 16          | 10                    | 9                |
| from_washer_load_id         | uuid      | c          | char alignment, no alignment needed                             | 16          | 11                    | 12               |
| from_web_user_id            | uuid      | c          | char alignment, no alignment needed                             | 16          | 12                    | 6                |
| hsys_id                     | uuid      | c          | char alignment, no alignment needed                             | 16          | 13                    | 16               |
| id                          | uuid      | c          | char alignment, no alignment needed                             | 16          | 14                    | 1                |
| inv_id                      | uuid      | c          | char alignment, no alignment needed                             | 16          | 15                    | 2                |
| to_assembly_id              | uuid      | c          | char alignment, no alignment needed                             | 16          | 16                    | 27               |
| to_associated_to_id         | uuid      | c          | char alignment, no alignment needed                             | 16          | 17                    | 5                |
| to_clinic_id                | uuid      | c          | char alignment, no alignment needed                             | 16          | 18                    | 25               |
| to_facility_department_id   | uuid      | c          | char alignment, no alignment needed                             | 16          | 19                    | 26               |
| to_facility_id              | uuid      | c          | char alignment, no alignment needed                             | 16          | 20                    | 23               |
| to_facility_location_id     | uuid      | c          | char alignment, no alignment needed                             | 16          | 21                    | 20               |
| to_scan_at_facility_id      | uuid      | c          | char alignment, no alignment needed                             | 16          | 22                    | 18               |
| to_scan_id                  | uuid      | c          | char alignment, no alignment needed                             | 16          | 23                    | 17               |
| to_scase_id                 | uuid      | c          | char alignment, no alignment needed                             | 16          | 24                    | 24               |
| to_sterilizer_load_id       | uuid      | c          | char alignment, no alignment needed                             | 16          | 25                    | 21               |
| to_washer_load_id           | uuid      | c          | char alignment, no alignment needed                             | 16          | 26                    | 22               |
| to_web_user_id              | uuid      | c          | char alignment, no alignment needed                             | 16          | 27                    | 19               |
| created_dts                 | timestamp | d          | double alignment, 8 bytes on many machines, but by no means all | 8           | 28                    | 32               |
| from_node_dts               | timestamp | d          | double alignment, 8 bytes on many machines, but by no means all | 8           | 29                    | 30               |
| to_node_dts                 | timestamp | d          | double alignment, 8 bytes on many machines, but by no means all | 8           | 30                    | 31               |
| updated_dts                 | timestamp | d          | double alignment, 8 bytes on many machines, but by no means all | 8           | 31                    | 33               |
| num_inst                    | int4      | i          | int alignment, 4 bytes on most machines                         | 4           | 32                    | 28               |
| seconds                     | int4      | i          | int alignment, 4 bytes on most machines                         | 4           | 33                    | 29               |
| from_associated_to          | citext    | i          | int alignment, 4 bytes on most machines                         | -1          | 34                    | 39               |
| from_node                   | citext    | i          | int alignment, 4 bytes on most machines                         | -1          | 35                    | 36               |
| from_to_node                | citext    | i          | int alignment, 4 bytes on most machines                         | -1          | 36                    | 38               |
| from_to_range               | tsrange   | d          | double alignment, 8 bytes on many machines, but by no means all | -1          | 37                    | 34               |
| from_user_name              | citext    | i          | int alignment, 4 bytes on most machines                         | -1          | 38                    | 41               |
| is_fake                     | citext    | i          | int alignment, 4 bytes on most machines                         | -1          | 39                    | 43               |
| sequence_                   | citext    | i          | int alignment, 4 bytes on most machines                         | -1          | 40                    | 35               |
| source_                     | citext    | i          | int alignment, 4 bytes on most machines                         | -1          | 41                    | 42               |
| to_associated_to            | citext    | i          | int alignment, 4 bytes on most machines                         | -1          | 42                    | 40               |
| to_node                     | citext    | i          | int alignment, 4 bytes on most machines                         | -1          | 43                    | 37               |
| to_user_name                | citext    | i          | int alignment, 4 bytes on most machines                         | -1          | 44                    | 44               |
+-----------------------------+-----------+------------+-----------------------------------------------------------------+-------------+-----------------------+------------------+