Postgres:如何填充默认值?

Postgres: how does a default value get populated?

明确地说,我想知道用于填充默认值的机制是什么,而不是在 table 上创建默认值约束所需的 SQL 语法。

Postgres 是否使用某种触发器来更新默认值(如果缺少或其他原因)?我找不到关于 official website.

的解释

这发生在 src/backend/rewrite/rewriteHandler.c 中的 rewriteTargetListIU 函数中。评论说明了一切:

/*
 * rewriteTargetListIU - rewrite INSERT/UPDATE targetlist into standard form
 *
 * This has the following responsibilities:
 *
 * 1. For an INSERT, add tlist entries to compute default values for any
 * attributes that have defaults and are not assigned to in the given tlist.
 * (We do not insert anything for default-less attributes, however.  The
 * planner will later insert NULLs for them, but there's no reason to slow
 * down rewriter processing with extra tlist nodes.)  Also, for both INSERT
 * and UPDATE, replace explicit DEFAULT specifications with column default
 * expressions.

所以这发生在 查询重写 期间,这是解析 SQL 字符串和优化它之间的步骤。