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 7023255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:41:17+00:00 2026-05-27T23:41:17+00:00

Is this possible? I’m interested in finding out which columns were specified in the

  • 0

Is this possible? I’m interested in finding out which columns were specified in the UPDATE request regardless of the fact that the new value that is being sent may or may not be what is stored in the database already.

The reason I want to do this is because we have a table that can receive updates from multiple sources. Previously, we weren’t recording which source the update originated from. Now the table stores which source has performed the most recent update. We can change some of the sources to send an identifier, but that isn’t an option for everything. So I’d like to be able to recognize when an UPDATE request doesn’t have an identifier so I can substitute in a default value.

  • 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-05-27T23:41:17+00:00Added an answer on May 27, 2026 at 11:41 pm

    If a "source" doesn’t "send an identifier", the column will be unchanged. Then you cannot detect whether the current UPDATE was done by the same source as the last one or by a source that did not change the column at all. In other words: this does not work properly.

    If the "source" is identifiable by any session information function, you can work with that. Like:

    NEW.column = session_user;
    

    Unconditionally for every update.

    General Solution

    I found a way to solve the original problem.

    Set the column to a default value if it’s not targeted in an UPDATE (not in the SET list). Key element is a per-column trigger introduced with PostgreSQL 9.0 – a column-specific trigger using the UPDATE OFcolumn_name clause. The manual:

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

    That’s the only simple way I found to distinguish whether a column was updated with a new value identical to the old, versus not updated at all.

    One could also parse the text returned by current_query(). But that seems cumbersome, tricky and unreliable.

    Trigger functions

    I assume a column source defined NOT NULL.

    Step 1: Set source to NULL if unchanged:

    CREATE OR REPLACE FUNCTION trg_tbl_upbef_step1()
      RETURNS trigger
      LANGUAGE plpgsql AS
    $func$
    BEGIN
       IF NEW.source = OLD.source THEN
          NEW.source := NULL;      -- "impossible" value (source is NOT NULL)
       END IF;
    
       RETURN NEW;
    END
    $func$;
    

    Step 2: Revert to old value. Trigger will only be fired if the value was actually updated (see below):

    CREATE OR REPLACE FUNCTION trg_tbl_upbef_step2()
      RETURNS trigger
      LANGUAGE plpgsql AS
    $func$
    BEGIN
       IF NEW.source IS NULL THEN
          NEW.source := OLD.source;
       END IF;
    
       RETURN NEW;
    END
    $func$;
    

    Step 3: Now we can identify the lacking update and set a default value instead:

    CREATE OR REPLACE FUNCTION trg_tbl_upbef_step3()
      RETURNS trigger
      LANGUAGE plpgsql AS
    $func$
    BEGIN
       IF NEW.source IS NULL THEN
          NEW.source := 'UPDATE default source';  -- optionally same as column default
       END IF;
    
       RETURN NEW;
    END
    $func$;
    

    Triggers

    The trigger for Step 2 is fired per column!

    CREATE TRIGGER upbef_step1
      BEFORE UPDATE ON tbl
      FOR EACH ROW
      EXECUTE PROCEDURE trg_tbl_upbef_step1();
    
    CREATE TRIGGER upbef_step2
      BEFORE UPDATE OF source ON tbl             -- key element!
      FOR EACH ROW
      EXECUTE PROCEDURE trg_tbl_upbef_step2();
        
    CREATE TRIGGER upbef_step3
      BEFORE UPDATE ON tbl
      FOR EACH ROW
      EXECUTE PROCEDURE trg_tbl_upbef_step3();

    db<>fiddle here

    Trigger names are relevant, because they are fired in alphabetical order (all being BEFORE UPDATE)!

    The procedure could be simplified with something like "per-not-column triggers" or any other way to check the target-list of an UPDATE in a trigger. But I see no handle for this, currently (unchanged as of Postgres 16).

    If source can be NULL, use any other "impossible" intermediate value and check for NULL additionally in trigger function 1:

    IF OLD.source IS NOT DISTINCT FROM NEW.source THEN
        NEW.source := '#impossible_value#';
    END IF;
    

    Adapt the rest accordingly.

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

Sidebar

Related Questions

Is this possible? I am looking forward to a tutorial which explains the steps
Is this possible? I have 4 pages that load dynamically with PHP/AJAX when corresponding
Is this possible? I tried with EasyMock.expectLastCall().times(0); but EasyMock complains that times must be
Is this possible in mysql? update table1 set column1 = (select column1 from table2
Is this possible? I can't work out how to do it.
Is this possible to return all columns in query as empty column (not null)
Is this possible.. for example, imagine I respond to a request with a 302
Is this possible or do I have to use WPF. I am new to
Is this possible!!?!?!?!? I'm trying to make a set of classes that model a
Is this possible at all and how? Update: I need this because I create

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.