I have a table:
CREATE TABLE field_data.soil_samples (
pgid SERIAL NOT NULL,
sample_id text,
project_id text,
utm_zone integer,
utm_easting integer,
utm_northing integer,
wgs84_longitude double precision,
wgs84_latitude double precision,
yt_albers_geom geometry(Point,3578),
CONSTRAINT soil_samples_pk PRIMARY KEY (pgid)
)
The PostGIS 2.0 geometry in yt_albers_geom is created using a trigger which fires on INSERTS against this table. If the record being inserted satisfies one of the following conditions, the geometry is generated:
- Both
wgs84_latitudeandwgs84_longitudefields are not null - Each of
utm_zone,utm_easting, andutm_northingare not null
Now, I am confused about how to do updates which achieve the following:
- When an update is done to
utm_zone,utm_easting, orutm_northing, thenwgs_84_latitude,wgs84_longitude, andyt_albers_geomare updated by a trigger - When an update is done to
wgs84_latitudeorwgs84_longitude, then all theutm_fields are updated, as well asyt_albers_geom. - When an update is done to
yt_albers_geom, all of the coordinate fields are updated.
It seems that any of these triggers would cause an infinite loop of trigger firing, correct?
You can do this with standard triggers
BEFORE UPDATE OF ... ON ....The manual on
CREATE TRIGGERinforms:And further down:
Bold emphasis mine. So no infinite loops, because the the updates inside the trigger do not invoke another trigger.
Test case
Create test table (simplified, without irrelevant rows):
Dummy trigger for your first requirement:
The
WHENclause is optional. Prevents the trigger from firing when no value has actually changed.Dummy trigger for your second requirement:
Trigger for third requirement along these lines …
Test
Trigger
upbef_utm: empty update, nothing happens:Update with actual change: The second trigger
upbef_wgs84will not fire onUPDATE OF utm_zone!Trigger
upbef_wgs84:-> SQLfiddle demo.