Postgres:如何填充默认值?

时间:2019-01-30 19:07:51

标签: postgresql column-defaults

要清楚,我想知道用于填充默认值的机制是什么,而不是在表上创建默认值约束所需的SQL语法。

Postgres是否使用某种触发器来更新默认值(如果缺少默认值或其他值)?我在official website上找不到解释。

1 个答案:

答案 0 :(得分:2)

这发生在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字符串和对其进行优化之间的步骤。