Populate_Geometry_Columns — 确保几何列定义了类型修饰符或具有适当的空间约束。
text Populate_Geometry_Columns(boolean use_typmod=true);
int Populate_Geometry_Columns(oid relation_oid, boolean use_typmod=true);
确保几何列具有适当的类型修饰符或空间约束,以确保它们在 geometry_columns 视图中正确注册。默认情况下,会将所有没有类型修饰符的几何列转换为具有类型修饰符的列。
为了向后兼容以及为了满足表继承等空间需求(其中每个子表可能具有不同的几何类型),仍然支持旧的检查约束行为。如果需要旧的行为,则需要将新的可选参数传递为 false use_typmod=false。当这样做时,将创建没有类型修饰符的几何列,但会定义 3 个约束。特别是,这意味着属于表的每个几何列至少有三个约束
enforce_dims_geom - 确保每个几何图形具有相同的维度(请参阅 ST_NDims)
enforce_geotype_geom - 确保每个几何图形都是相同的类型(请参阅 GeometryType)
enforce_srid_geom - 确保每个几何图形都在相同的投影中(请参阅 ST_SRID)
如果提供了表 oid,则此函数会尝试确定表中所有几何列的 srid、维度和几何类型,并根据需要添加约束。如果成功,则会将相应的行插入到 geometry_columns 表中,否则,会捕获异常并引发描述问题的错误通知。
如果提供了视图的 oid,则与表 oid 一样,此函数会尝试确定视图中所有几何图形的 srid、维度和类型,并将相应的条目插入到 geometry_columns 表中,但不会采取任何措施来强制约束。
无参数变体是参数化变体的简单包装,它首先截断并重新填充数据库中每个空间表和视图的 geometry_columns 表,并在适当情况下向表添加空间约束。它返回数据库中检测到的几何列数量的摘要以及插入到 geometry_columns 表中的几何列数量。参数化版本仅返回插入到 geometry_columns 表中的行数。
可用性:1.4.0
更改:2.0.0 默认情况下,现在使用类型修饰符而不是检查约束来约束几何类型。您仍然可以通过使用新的 use_typmod 并将其设置为 false 来使用检查约束行为。
增强功能:2.0.0 引入了 use_typmod 可选参数,该参数允许控制是否使用类型修饰符或检查约束创建列。
CREATE TABLE public.myspatial_table(gid serial, geom geometry);
INSERT INTO myspatial_table(geom) VALUES(ST_GeomFromText('LINESTRING(1 2, 3 4)',4326) );
-- This will now use typ modifiers. For this to work, there must exist data
SELECT Populate_Geometry_Columns('public.myspatial_table'::regclass);
populate_geometry_columns
--------------------------
1
\d myspatial_table
Table "public.myspatial_table"
Column | Type | Modifiers
--------+---------------------------+---------------------------------------------------------------
gid | integer | not null default nextval('myspatial_table_gid_seq'::regclass)
geom | geometry(LineString,4326) |
-- This will change the geometry columns to use constraints if they are not typmod or have constraints already.
--For this to work, there must exist data
CREATE TABLE public.myspatial_table_cs(gid serial, geom geometry);
INSERT INTO myspatial_table_cs(geom) VALUES(ST_GeomFromText('LINESTRING(1 2, 3 4)',4326) );
SELECT Populate_Geometry_Columns('public.myspatial_table_cs'::regclass, false);
populate_geometry_columns
--------------------------
1
\d myspatial_table_cs
Table "public.myspatial_table_cs"
Column | Type | Modifiers
--------+----------+------------------------------------------------------------------
gid | integer | not null default nextval('myspatial_table_cs_gid_seq'::regclass)
geom | geometry |
Check constraints:
"enforce_dims_geom" CHECK (st_ndims(geom) = 2)
"enforce_geotype_geom" CHECK (geometrytype(geom) = 'LINESTRING'::text OR geom IS NULL)
"enforce_srid_geom" CHECK (st_srid(geom) = 4326)