Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8935817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:04:45+00:00 2026-06-15T10:04:45+00:00

I have a table: CREATE TABLE field_data.soil_samples ( pgid SERIAL NOT NULL, sample_id text,

  • 0

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:

  1. Both wgs84_latitude and wgs84_longitude fields are not null
  2. Each of utm_zone, utm_easting, and utm_northing are not null

Now, I am confused about how to do updates which achieve the following:

  1. When an update is done to utm_zone, utm_easting, or utm_northing, then wgs_84_latitude, wgs84_longitude, and yt_albers_geom are updated by a trigger
  2. When an update is done to wgs84_latitude or wgs84_longitude, then all the utm_ fields are updated, as well as yt_albers_geom.
  3. 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:04:46+00:00Added an answer on June 15, 2026 at 10:04 am

    You can do this with standard triggers BEFORE UPDATE OF ... ON ....
    The manual on CREATE TRIGGER informs:

    The trigger will only fire if at least one of the listed columns is
    mentioned as a target of the UPDATE command.

    And further down:

    A column-specific trigger (one defined using the UPDATE OF column_name
    syntax) will fire when any of its columns are listed as targets in the
    UPDATE command’s SET list. It is possible for a column’s value to
    change even when the trigger is not fired, because changes made to the
    row’s contents by BEFORE UPDATE triggers are not considered.

    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):

    CREATE TABLE soil_samples (
      pgid SERIAL PRIMARY KEY
    
     ,utm_zone integer
     ,utm_easting integer
     ,utm_northing integer
    
     ,wgs84_longitude double precision
     ,wgs84_latitude double precision
    
     ,yt_albers_geom double precision
    );
    

    Dummy trigger for your first requirement:

    When an update is done to utm_zone, utm_easting, or utm_northing, then
    wgs_84_latitude, wgs84_longitude, and yt_albers_geom are updated by a trigger.

    CREATE OR REPLACE FUNCTION trg_upbef_utm()  RETURNS trigger AS
    $func$
    BEGIN
       NEW.wgs84_latitude  := NEW.wgs84_latitude + 10;
       NEW.wgs84_longitude := NEW.wgs84_longitude + 10;
       NEW.yt_albers_geom  := NEW.yt_albers_geom + 10;
    
       RETURN NEW;
    END
    $func$ LANGUAGE plpgsql;
    
    CREATE TRIGGER upbef_utm
    BEFORE UPDATE OF utm_zone, utm_easting, utm_northing ON soil_samples
    FOR EACH ROW
    WHEN (NEW.utm_zone     IS DISTINCT FROM OLD.utm_zone    OR
          NEW.utm_easting  IS DISTINCT FROM OLD.utm_easting OR
          NEW.utm_northing IS DISTINCT FROM OLD.utm_northing)  -- optional
    EXECUTE PROCEDURE trg_upbef_utm();
    

    The WHEN clause is optional. Prevents the trigger from firing when no value has actually changed.

    Dummy trigger for your second requirement:

    When an update is done to wgs84_latitude or wgs84_longitude, then all
    the utm_ fields are updated, as well as yt_albers_geom.

    CREATE OR REPLACE FUNCTION trg_upbef_wgs84()  RETURNS trigger AS
    $func$
    BEGIN
       NEW.utm_zone       := NEW.utm_zone + 100;
       NEW.utm_easting    := NEW.utm_easting + 100;
       NEW.utm_northing   := NEW.utm_northing + 100;
       NEW.yt_albers_geom := NEW.yt_albers_geom + 100;
    
       RETURN NEW;
    END
    $func$ LANGUAGE plpgsql;
    
    CREATE TRIGGER upbef_wgs84
     BEFORE UPDATE OF wgs84_latitude, wgs84_longitude ON soil_samples
     FOR EACH ROW
     WHEN (NEW.wgs84_latitude  IS DISTINCT FROM OLD.wgs84_latitude OR
           NEW.wgs84_longitude IS DISTINCT FROM OLD.wgs84_longitude)  -- optional
     EXECUTE PROCEDURE trg_upbef_wgs84();
    

    Trigger for third requirement along these lines …

    Test

    INSERT INTO soil_samples VALUES (1, 1,1,1, 2,2, 3) RETURNING *;
    

    Trigger upbef_utm: empty update, nothing happens:

    UPDATE soil_samples SET utm_zone = 1 RETURNING *;
    

    Update with actual change: The second trigger upbef_wgs84 will not fire on UPDATE OF utm_zone!

    UPDATE soil_samples SET utm_zone = 0 RETURNING *;
    

    Trigger upbef_wgs84:

    UPDATE soil_samples SET wgs84_latitude = 0 RETURNING *;
    

    -> SQLfiddle demo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table: CREATE TABLE siteConfig ( settingName VARCHAR(64) NOT NULL, value VARCHAR(255),
I have a table CREATE TABLE `messages` ( `uid` BIGINT NOT NULL , `mid`
I have this table CREATE TABLE [dbo].[friend_blocked_list]( [subdomain] [varchar](50) NOT NULL, [un] [nvarchar](50) NOT
I have this table CREATE TABLE `codes` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
I have a table: CREATE TABLE `data_table` ( `data_id` INT NOT NULL AUTO_INCREMENT PRIMARY
I Have the following table: CREATE TABLE `tmp_table` ( `id` int(11) NOT NULL AUTO_INCREMENT,
This is the table I have: CREATE TABLE `person` ( `id` bigint(10) NOT NULL
I have a table similar to this: CREATE TABLE example ( id integer primary
I have two tables: CREATE TABLE [dbo].[Context] ( [Identity] int IDENTITY (1, 1) NOT
I have a very simple table CREATE TABLE IF NOT EXISTS `largecache` ( `id`

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.