【问题标题】:Postgres: how does a default value get populated?Postgres:如何填充默认值?
【发布时间】:2019-01-30 19:07:51
【问题描述】:

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

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

【问题讨论】:

  • 除非这只是对 PG 内部的好奇,否则我认为您会得到更好的回复,说明为什么您想知道。
  • 是的,只是想对何时以及如何完成有更高层次的了解。
  • 您可以浏览源代码。 (记住:PG 是开源的)
  • 这正是我想要避免的——不太了解 C。我认为有人会知道并愿意提供帮助,因此将其发布在 stackoverflow 上。对太多细节不感兴趣,只对谷歌未能提供帮助的高层次理解感兴趣。
  • “高级”图是,每当你要求服务器执行一个语句时,它通过运行一些代码来完成,而当你执行一个INSERT 语句时,一些代码会填充在缺少默认值的列中。不涉及触发器,也没有任何你可能称之为“机制”的东西;就是这么写的。

标签: postgresql column-defaults


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-09-26
    • 2023-03-23
    • 2017-04-23
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 2020-05-26
    相关资源
    最近更新 更多