名稱

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)