PostgreSQL tuple alignment padding - 对齐规则,以及如何选择字段顺序

本文探讨PostgreSQL中字段顺序对空间占用的影响,通过调整定长字段(从大到小)和变长字段的顺序,有效减少了tuple padding,使空间占用降低了约10%。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景

PostgreSQL tuple内部有ALIGN机制,因此字段顺序选择实际上也是有讲究的,选择不好,可能因为ALIGN导致占用空间放大。

src/backend/access/common/heaptuple.c

 * Before Postgres 8.3 varlenas always had a 4-byte length header, and  
 * therefore always needed 4-byte alignment (at least).  This wasted space  
 * for short varlenas, for example CHAR(1) took 5 bytes and could need up to  
 * 3 additional padding bytes for alignment.  
  
 * Now, a short varlena (up to 126 data bytes) is reduced to a 1-byte header  
 * and we don't align it.  To hide this from datatype-specific functions that  
 * don't want to deal with it, such a datum is considered "toasted" and will  
 * be expanded back to the normal 4-byte-header format by pg_detoast_datum.  
 * (In performance-critical code paths we can use pg_detoast_datum_packed  
 * and the appropriate access macros to avoid that overhead.)  Note that this  
 * conversion is performed directly in heap_form_tuple, without invoking  
 * tuptoaster.c.

https://www.postgresql.org/docs/devel/static/catalog-pg-type.html

对齐规则

tuple对齐规则

src/include/access/tupmacs.h

/*  
 * att_align_datum aligns the given offset as needed for a datum of alignment  
 * requirement attalign and typlen attlen.  attdatum is the Datum variable  
 * we intend to pack into a tuple (it's only accessed if we are dealing with  
 * a varlena type).  Note that this assumes the Datum will be stored as-is;  
 * callers that are intending to convert non-short varlena datums to short  
 * format have to account for that themselves.  
 */  
#define att_align_datum(cur_offset, attalign, attlen, attdatum) \  
( \  
        ((attlen) == -1 && VARATT_IS_SHORT(DatumGetPointer(attdatum))) ? \  
        (uintptr_t) (cur_offset) : \  
        att_align_nominal(cur_offset, attalign) \  
)  
  
/*  
 * att_align_pointer performs the same calculation as att_align_datum,  
 * but is used when walking a tuple.  attptr is the current actual data  
 * pointer; when accessing a varlena field we have to "peek" to see if we  
 * are looking at a pad byte or the first byte of a 1-byte-header datum.  
 * (A zero byte must be either a pad byte, or the first byte of a correctly  
 * aligned 4-byte length word; in either case we can align safely.  A non-zero  
 * byte must be either a 1-byte length word, or the first byte of a correctly  
 * aligned 4-byte length word; in either case we need not align.)  
 *  
 * Note: some callers pass a "char *" pointer for cur_offset.  This is  
 * a bit of a hack but should work all right as long as uintptr_t is the  
 * correct width.  
 */  
#define att_align_pointer(cur_offset, attalign, attlen, attptr) \  
( \  
        ((attlen) == -1 && VARATT_NOT_PAD_BYTE(attptr)) ? \  
        (uintptr_t) (cur_offset) : \  
        att_align_nominal(cur_offset, attalign) \  
)  
  
/*  
 * att_align_nominal aligns the given offset as needed for a datum of alignment  
 * requirement attalign, ignoring any consideration of packed varlena datums.  
 * There are three main use cases for using this macro directly:  
 *      * we know that the att in question is not varlena (attlen != -1);  
 *        in this case it is cheaper than the above macros and just as good.  
 *      * we need to estimate alignment padding cost abstractly, ie without  
 *        reference to a real tuple.  We must assume the worst case that  
 *        all varlenas are aligned.  
 *      * within arrays, we unconditionally align varlenas (XXX this should be  
 *        revisited, probably).  
 *  
 * The attalign cases are tested in what is hopefully something like their  
 * frequency of occurrence.  
 */  
#define att_align_nominal(cur_offset, attalign) \  
( \  
        ((attalign) == 'i') ? INTALIGN(cur_offset) : \  
         (((attalign) == 'c') ? (uintptr_t) (cur_offset) : \  
          (((attalign) == 'd') ? DOUBLEALIGN(cur_offset) : \  
           ( \  
                        AssertMacro((attalign) == 's'), \  
                        SHORTALIGN(cur_offset) \  
           ))) \  
)

通过pg_attribute系统表,查看对齐情况

https://www.postgresql.org/docs/devel/static/catalog-pg-type.html

typalign is the alignment required when storing a value of this type. It applies to storage on disk as well as most representations of the value inside PostgreSQL. When multiple values are stored consecutively, such as in the representation of a complete row on disk, padding is inserted before a datum of this type so that it begins on the specified boundary. The alignment reference is the beginning of the first datum in the sequence.

Possible values are:  
  
c = char alignment, i.e., no alignment needed.  
  
s = short alignment (2 bytes on most machines).  
  
i = int alignment (4 bytes on most machines).  
  
d = double alignment (8 bytes on many machines, but by no means all).

表示在这个字段前面必须已经以以上大小对齐。

例如,

c表示,这个字段的前面已占用空间(包括24字节的tuple head)必须1字节的倍数。如果不是,则前面一个字段末尾必须有padding,使得符合这个对齐条件。

s表示,这个字段前面已占用空间(包括24字节的tuple head)必须是2字节的倍数。如果不是,则前面一个字段末尾必须有padding,使得符合这个对齐条件。

i表示,这个字段前面已占用空间(包括24字节的tuple head)必须是4字节的倍数。如果不是,则前面一个字段末尾必须有padding,使得符合这个对齐条件。

d表示,这个字段前面已占用空间(包括24字节的tuple head)必须是8字节的倍数。如果不是,则前面一个字段末尾必须有padding,使得符合这个对齐条件。

例如

1、

1,4,8

会变成

1,3(padding),4,8

2、

1,1,8

会变成

1,1,6(padding),8

3、

1,1,1,8

会变成

1,1,1,5,8

例子

1、空行

postgres=# select pg_column_size(row());  
 pg_column_size   
----------------  
             24  
(1 row)

2、PADDING

postgres=# select pg_column_size(row(char 'a', char 'b', int4 '1'));  
 pg_column_size   
----------------  
             32  
(1 row)  
  
postgres=# select pg_column_size(row(char 'a', char 'b', int8 '1'));  
 pg_column_size   
----------------  
             40  
(1 row)

相关文档举例

https://blog.2ndquadrant.com/on-rocks-and-sand/

1、

SELECT pg_column_size(ROW()) AS empty,  
       pg_column_size(ROW(0::SMALLINT)) AS byte2,  
       pg_column_size(ROW(0::BIGINT)) AS byte8,  
       pg_column_size(ROW(0::SMALLINT, 0::BIGINT)) AS byte16;  -- 24,2,6(padding),8   
   
 empty | byte2 | byte8 | byte16   
-------+-------+-------+--------  
    24 |    26 |    32 |     40

2、包含padding的表

CREATE TABLE user_order (  
  is_shipped    BOOLEAN NOT NULL DEFAULT FALSE,  
  user_id       BIGINT NOT NULL,  
  order_total   NUMERIC NOT NULL,  
  order_dt      TIMESTAMPTZ NOT NULL,  
  order_type    SMALLINT NOT NULL,  
  ship_dt       TIMESTAMPTZ,  
  item_ct       INT NOT NULL,  
  ship_cost     NUMERIC,  
  receive_dt    TIMESTAMPTZ,  
  tracking_cd   TEXT,  
  id            BIGSERIAL PRIMARY KEY NOT NULL  
);

查看这个表的对齐规则

SELECT a.attname, t.typname, t.typalign, t.typlen  
  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 = 'user_order'  
   AND a.attnum >= 0  
 ORDER BY a.attnum;  
   
   attname   |   typname   | typalign | typlen   
-------------+-------------+----------+--------  
 is_shipped  | bool        | c        |      1  
 user_id     | int8        | d        |      8  
 order_total | NUMERIC     | i        |     -1  
 order_dt    | timestamptz | d        |      8  
 order_type  | int2        | s        |      2  
 ship_dt     | timestamptz | d        |      8  
 item_ct     | int4        | i        |      4  
 ship_cost   | NUMERIC     | i        |     -1  
 receive_dt  | timestamptz | d        |      8  
 tracking_cd | text        | i        |     -1  
 id          | int8        | d        |      8

3、插入测试数据

135MB

INSERT INTO user_order (  
    is_shipped, user_id, order_total, order_dt, order_type,  
    ship_dt, item_ct, ship_cost, receive_dt, tracking_cd  
)  
SELECT TRUE, 1000, 500.00, now() - INTERVAL '7 days',  
       3, now() - INTERVAL '5 days', 10, 4.99,  
       now() - INTERVAL '3 days', 'X5901324123479RROIENSTBKCV4'  
  FROM generate_series(1, 1000000);  
   
SELECT pg_relation_size('user_order') AS size_bytes,  
       pg_size_pretty(pg_relation_size('user_order')) AS size_pretty;  
   
 size_bytes | size_pretty   
------------+-------------  
  141246464 | 135 MB

4、调整字段顺序,可以把padding消除掉,基于前面说的规则即可。

SELECT pg_column_size(ROW()) AS empty_row,  
       pg_column_size(ROW(0::NUMERIC)) AS no_val,  
       pg_column_size(ROW(1::NUMERIC)) AS no_dec,  
       pg_column_size(ROW(9.9::NUMERIC)) AS with_dec,  
       pg_column_size(ROW(1::INT2, 1::NUMERIC)) AS col2,  
       pg_column_size(ROW(1::INT4, 1::NUMERIC)) AS col4,  
       pg_column_size(ROW(1::NUMERIC, 1::INT4)) AS round8;  
   
 empty_row | no_val | no_dec | with_dec | col2 | col4 | round8   
-----------+--------+--------+----------+------+------+--------  
        24 |     27 |     29 |       31 |   31 |   33 |     36
SELECT pg_column_size(ROW()) AS empty_row,  
       pg_column_size(ROW(''::TEXT)) AS no_text,  
       pg_column_size(ROW('a'::TEXT)) AS min_text,  
       pg_column_size(ROW(1::INT4, 'a'::TEXT)) AS two_col,  
       pg_column_size(ROW('a'::TEXT, 1::INT4)) AS round4;  
   
 empty_row | no_text | min_text | two_col | round4   
-----------+---------+----------+---------+--------  
        24 |      25 |       26 |      30 |     32
SELECT pg_column_size(ROW()) AS empty_row,  
       pg_column_size(ROW(1::SMALLINT)) AS int2,  
       pg_column_size(ROW(1::INT)) AS int4,  
       pg_column_size(ROW(1::BIGINT)) AS int8,  
       pg_column_size(ROW(1::SMALLINT, 1::BIGINT)) AS padded,  
       pg_column_size(ROW(1::INT, 1::INT, 1::BIGINT)) AS not_padded;  
   
 empty_row | int2 | int4 | int8 | padded | not_padded   
-----------+------+------+------+--------+------------  
        24 |   26 |   28 |   32 |     40 |         40

5、消除tuple padding的优化,字段顺序如下

5.1、定长字段(从大到小)

5.2、变长字段

DROP TABLE user_order;  
   
  
CREATE TABLE user_order (  
  id            BIGSERIAL PRIMARY KEY NOT NULL,  
  user_id       BIGINT NOT NULL,  
  order_dt      TIMESTAMPTZ NOT NULL,  
  ship_dt       TIMESTAMPTZ,  
  receive_dt    TIMESTAMPTZ,  
  item_ct       INT NOT NULL,  
  order_type    SMALLINT NOT NULL,  
  is_shipped    BOOLEAN NOT NULL DEFAULT FALSE,  
  tracking_cd   TEXT,  
  order_total   NUMERIC NOT NULL,  
  ship_cost     NUMERIC  
);  
  
  
INSERT INTO user_order (  
    is_shipped, user_id, order_total, order_dt, order_type,  
    ship_dt, item_ct, ship_cost, receive_dt, tracking_cd  
)  
SELECT TRUE, 1000, 500.00, now() - INTERVAL '7 days',  
       3, now() - INTERVAL '5 days', 10, 4.99,  
       now() - INTERVAL '3 days', 'X5901324123479RROIENSTBKCV4'  
  FROM generate_series(1, 1000000);  
  
postgres=# \dt+ user_order   
                       List of relations  
 Schema |    Name    | Type  |  Owner   |  Size  | Description   
--------+------------+-------+----------+--------+-------------  
 public | user_order | table | postgres | 112 MB |   
(1 row)

6、优化后的padding情况,可以看到已经消除了padding,空间降低到112MB。

SELECT a.attname, t.typname, t.typalign, t.typlen  
  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 = 'user_order'  
   AND a.attnum >= 0  
 ORDER BY t.typlen DESC;  
   
   attname   |   typname   | typalign | typlen   
-------------+-------------+----------+--------  
 id          | int8        | d        |      8  
 user_id     | int8        | d        |      8  
 order_dt    | timestamptz | d        |      8  
 ship_dt     | timestamptz | d        |      8  
 receive_dt  | timestamptz | d        |      8  
 item_ct     | int4        | i        |      4  
 order_type  | int2        | s        |      2  
 is_shipped  | bool        | c        |      1  
 tracking_cd | text        | i        |     -1  
 ship_cost   | NUMERIC     | i        |     -1  
 order_total | NUMERIC     | i        |     -1

小结

消除tuple PADDING, 字段顺序规则:

1、定长字段(从大到小)

2、变长字段

本文的例子简单的说明了padding引入的TUPLE变大的情况,使用以上规则调整字段顺序后,空间占用下降了10%左右。

参考

src/include/access/tupmacs.h

src/backend/access/common/heaptuple.c

https://blog.2ndquadrant.com/on-rocks-and-sand/

《Greenplum 优化CASE - 对齐JOIN字段类型,使用数组代替字符串,降低字符串处理开销,列存降低扫描开销》

《PostgreSQL 10.0 preview 性能增强 - pg_xact align(cacheline对齐)》

《未对齐(alignment)造成SSD 写放大一例》

https://yq.aliyun.com/articles/237

https://www.pgcon.org/2012/schedule/attachments/258_212_Internals%20Of%20PostgreSQL%20Wal.pdf

https://www.postgresql.org/docs/devel/static/runtime-config-wal.html#RUNTIME-CONFIG-WAL-SETTINGS

https://www.postgresql.org/docs/devel/static/wal-reliability.html

原文地址:https://github.com/digoal/blog/blob/master/201810/20181001_01.md

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23503672/viewspace-2215523/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/23503672/viewspace-2215523/

<think>好的,我现在需要帮助用户解决关于Dify循环实现的问题。首先,我需要明确用户的具体需求。他们提到“dify 循环实现”或“dify 如何处理循环”,但Dify本身可能没有直接称为“循环”的功能,所以需要结合现有文档中的信息来推断可能的解决方案。 根据用户提供的引用内容,Dify工作流中有迭代节点[^1]。这可能类似于编程中的循环结构,用于重复执行某个任务。例如,在处理列表数据或需要多次执行同一操作时,迭代节点可以派上用场。我需要详细解释迭代节点的配置和使用步骤,确保用户能够理解如何设置循环次数或遍历数据集。 另外,用户可能对如何处理循环中的错误或性能问题感兴趣。引用[2]提到在Dify中处理PEM文件的问题,这可能涉及在循环中正确处理数据格式,避免错误积累。需要提醒用户在循环中注意错误处理和资源管理,确保每次迭代的稳定性。 引用[3]中的例子展示了如何结合FastAPI和Selenium进行实时监控并生成GIF,这可能涉及循环截取浏览器内容。虽然Dify本身可能不直接控制这类循环,但可以通过外部脚本或定时任务触发工作流,模拟循环执行。这部分需要说明如何将外部循环机制与Dify结合使用。 接下来,我需要组织回答结构。首先介绍迭代节点的基本概念和配置步骤,然后讨论循环中的数据处理和错误处理,最后补充外部触发循环的方法。同时,生成相关问题以帮助用户进一步探索,比如优化循环性能或处理复杂数据结构。 需要确保回答符合格式要求,使用正确的中文表达,行内公式用$...$,独立公式用$$...$$,但当前问题不涉及数学公式,所以可能不需要使用。同时,引用标识要自然地添加到相关段落末尾,如、[^2]、[^3]。 最后,检查是否有遗漏点,比如Dify的其他循环相关功能或用户可能遇到的常见问题,确保回答全面且易于理解。</think>在Dify中处理循环逻辑主要通过**迭代节点**实现,以下是具体实现方式和应用场景的解析: ### 一、Dify循环实现机制 Dify通过**工作流设计器**中的迭代节点处理循环需求,其核心原理类似编程中的`for循环`。迭代节点可遍历以下数据类型: - 数组列表:`["A","B","C"]` - 字典集合:`{"key1":"value1", "key2":"value2"}` - 数值范围:通过`range()`函数生成序列 配置示例: ```python # 模拟迭代节点的数据输入 input_data = { "dataset": [1,2,3,4,5], "process_logic": "item * 2" # 对每个元素执行乘以2的操作 } ``` ### 二、迭代节点的关键配置步骤 1. **数据源绑定**:将数组/字典类型变量连接到迭代节点的输入端口 2. **循环变量命名**:设定当前元素的变量名(默认为`item`) 3. **子流程设计**:在迭代节点内部构建需要重复执行的逻辑模块 4. **结果聚合**:通过`outputs`收集所有迭代结果,支持数组或对象格式 $$ \text{总耗时} = \sum_{i=1}^{n}(单次迭代时间_i) + 系统开销 $$ ### 三、循环中的特殊处理 1. **错误中断控制**: - 启用`continueOnError`参数可跳过失败迭代 - 通过`try-catch`模块包裹敏感操作 2. **并行优化**: ```python # 伪代码示例 Parallel.forEach(dataset, lambda item: process(item)) ``` 3. **结果过滤**: ```python filtered = filter(lambda x: x%2==0, processed_results) ``` ### 四、应用场景案例 1. **批量文件处理**:遍历存储桶中的文件列表进行格式转换 2. **数据清洗**:对数据库查询结果集进行逐条校验 3. **API轮询**:定时循环调用第三方接口直到满足特定条件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值