首页 > 其他分享 >[code notes] the implementation of alter table

[code notes] the implementation of alter table

时间:2024-03-23 18:44:36浏览次数:24  
标签:code implementation notes column add new table alter

Overview

In this article, I will inspect the postgresql code to find out the implementation of alter table command, specifically, the add column subcommand of the alter table command. The code for this article is from postgresql commit hash 21e3a8bc3544a1cfcff85933bc9c0664af32a8b8.

usage

alter table myt add col1 int;
alter table myt alter col2 type text;
alter table myt add constraint col2_min_length CHECK (lenght(col2) > 6);
alter table myt set unlogged;

syntax

![[Pasted image 20240323143948.png]]
image

![[Pasted image 20240323144141.png]]
image

Each alter table command has a AlterTableStmt data structure. An AlterTableStmt includes one or more AlterTableCmd commands. subcommands are add, drop, rename, set, etc.

execution

code path

exec_simple_query
PortalRun
ProcessUtility
ProcessUtilitySlow
AlterTable
ATController
ATRewriteCatalogs
ATExecCmd
ATExecAddColumn
ATParseTransformCmd
transformAlterTableStmt

Inside function ProcessUtilitySlow, before function AlterTable, a function AlterTableGetLockLevel is called to lock the being-altered table.

transformAlterTableStmt

transformAlterTableStmt transform original AlterTableCmd commands into more new AlterTableCmd commands.

  • open the being-altered table without any lock
  • set up pstate
  • set up CreateStmtContext
  • for sql alter table add col1 int, it needs invoke transformColumnDefinition which invokes transformColumnType. For this simple sql, transformColumnType only verifies the existence of the column type.
  • Postprocess constraints, include index constraints, foreign key constraints, check constraints
  • if any constraint exists, create a new AlterTableCmd with type AT_AddConstraint.
  • close the table
  • record the new list of AlterTableCmd commands in AlterTableStmt

ATExecAddColumn

  • since this function recurses, it could be driven to stack overflow, hence check_stack_depth now.
  • At top level, permission check was done in ATPrepCmd, else do it
  • open pg_attribute table with RowExclusiveLock
  • check for column name collision
  • do parse transformation through ATParseTransformCmd
  • update information in pg_attribute table, through InsertPgAttributeTuples
  • update information in pg_class table, through CatalogTupleUpdate
  • run post creation hook for new attribute
  • Make the attribute's catalog entry visible, through CommandCounterIncrement
  • Store the DEFAULT, if any, in the catalogs
  • Add needed dependency entries for the new column.
  • find any inherited tables by inspecting pg_class.relhassubclass
  • if found, then recursively invokes ATExecAddColumn
  • returns the ObjectAddress which represents the newly added column

标签:code,implementation,notes,column,add,new,table,alter
From: https://www.cnblogs.com/lddcool/p/18091533

相关文章

  • iptables-save 命令使用总结
    转载请注明出处:iptables-save 命令在Linux系统中用于将当前运行的 iptables 防火墙规则导出到一个文件中。这对于备份规则、迁移规则或在不同系统间共享规则配置非常有用。基本用法基本用法是将当前的 iptables 规则保存到文件中,如下所示:iptables-save>/pat......
  • R语言--06文件读写read.table()、read.csv()
    1、读取-read.table()#文件读写部分#1.读取ex1.txtex1<-read.table("ex1.txt")ex3<-read.table("ex1.txt",header=T)看看有没有header的区别,以下是第一行代码的运行结果: 以下是第二行代码运行的结果:所以header=T的作用就是原本的文件已经给出了列名,不用重新再......
  • innodb_undo_tablespaces导致Mysql启动报错
    1.问题MySQL5.7设置innodb_undo_tablespaces=2报错如下:2020-06-09T04:40:07.800321-05:000[ERROR]InnoDB:Expectedtoopen2undotablespacesbutwasabletofindonly0undotablespaces.Settheinnodb_undo_tablespacesparametertothecorrectvalueandret......
  • 【Coursera GenAI with LLM】 Week 3 LLM-powered applications Class Notes
    ModeloptimizationstoimproveapplicationperformanceDistillation:usesalargermodel,theteachermodel,totrainasmallermodel,thestudentmodel,wefreezeteacher'sweightsandgeneratecompletions,alsogeneratestudentmodel'scompl......
  • D. Yarik and Musical Notes
    一道有趣的思维题经过推理,最后的答案只有两种构成:1.1的数目*2的数目2.所有相同的数n,进行C(2,n)然后相加像这样,然后把比值设成k,可以知道只能枚举如上两类https://codeforces.com/problemset/problem/1899/D#include<iostream>#include<vector>#include<algorithm>#includ......
  • 用免费GPU部署自己的stable-diffusion项目(AI生成图片)
    2021年时出现了openAI的DALL,但是不开源。2022年一开年,DALL-E2发布,依然不开源。同年7月,Google公布其Text-to-Image模型Imagen,并且几乎在同一时间段AI图像生成平台Midjourney也进行公测。同年8月,StableDiffusion的发布将AIGC和AI绘画彻底带出了圈。StableDiffusion是......
  • vue2/3 - element表格组件el-table实现懒加载树型(上下级)数据、默认展开和隐藏层级,支
    效果图在vue2、vue3项目开发中,使用element饿了么组件库,实现Table表格组件动态懒加载表格数据,可以决定是否自动展开所有2级或3级,也可以点击加载下级数据,可搭配表格的增删改查,数据变化后自动更新列表不会破坏树状的展开和折叠结构。提供详细示例代码,一键复制运行查看效果,稍......
  • element plus table将复选框更改为只能选一条数据后,有部分数据无法选中
    只有前两个可以选中,下面三条数据都无法选中,一开始以为是代码写的有问题,还看了半天逻辑最后发现,这个表格的数据有一个字段名是children,而table组件识别到这个字段会认为表格是一个树形表格,前两条数据可以选中是因为children:[]给table添加了row-key="id",表格实际是下图所示 ......
  • 使用 Keras 的 Stable Diffusion 实现高性能文生图
    前言在本文中,我们将使用基于KerasCV实现的StableDiffusion模型进行图像生成,这是由stable.ai开发的文本生成图像的多模态模型。StableDiffusion是一种功能强大的开源的文本到图像生成模型。虽然市场上存在多种开源实现可以让用户根据文本提示轻松创建图像,但Keras......
  • 什么是AI绘画工具Stable Diffusion?如何安装Stable Diffusion?
    StableDiffusion秋叶整合包,一键安装StableDiffusion,门槛极低,完全免费,支持Nvidia全系列显卡。来自B站up主秋葉aaaki近期推出的StableDiffusion整合包v4.6版本,能够让零基础用户轻松在本地部署StableDiffusion,适合希望使用AI绘画的朋友。StableDiffusion(SD)是什么?Stab......