Populate_Geometry_Columns — 确保几何列由类型修饰符定义或具有适当的空间约束。
text Populate_Geometry_Columns(boolean use_typmod=true);
int Populate_Geometry_Columns(oid relation_oid, boolean use_typmod=true);
确保几何列具有适当的类型修饰符或空间约束,以确保它们在geometry_columns视图中正确注册。默认情况下,会将所有没有类型修饰符的几何列转换为具有类型修饰符的几何列。
为了向后兼容和空间需求(例如表继承,其中每个子表可能具有不同的几何类型),仍然支持旧的检查约束行为。如果您需要旧的行为,则需要将新的可选参数作为 falseuse_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可选参数,允许控制是否使用typmodifiers或检查约束创建列。
This example populates geometry column metadata using typmod behavior. For this to work, there must be existing data.
CREATE TABLE public.myspatial_table(gid serial, geom geometry);
INSERT INTO myspatial_table(geom) VALUES(ST_GeomFromText('LINESTRING(1 2,3 4)', 4326) );
SELECT Populate_Geometry_Columns('public.myspatial_table'::regclass);
1
Describe the table to see the typmod-based geometry column.
\d myspatial_table
Table "public.myspatial_table"
Columns:
gid integer not null default nextval('myspatial_table_gid_seq'::regclass)
geom geometry(LineString,4326)
This example changes the geometry columns to use constraints if they are not typmod or already constrained. For this to work, there must be existing 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);
1
Describe the table to see the constraint-based geometry column.
\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)