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

  • Home
  • SEARCH
  • 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 713231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:56:17+00:00 2026-05-14T04:56:17+00:00

I want to know number of rows that will be affected by UPDATE query

  • 0

I want to know number of rows that will be affected by UPDATE query in BEFORE per statement trigger . Is that possible?

The problem is that i want to allow only queries that will update up to 4 rows. If affected rows count is 5 or more i want to raise error.

I don’t want to do this in code because i need this check on db level.
Is this at all possible?

Thanks in advance for any clues on that

  • 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-14T04:56:17+00:00Added an answer on May 14, 2026 at 4:56 am

    I’ve created something like this:

    begin;
    
    create table test (
        id integer
    );
    
    insert into test(id) select generate_series(1,100);
    
    
    create or replace function trg_check_max_4_updated_records() 
    returns trigger as $$
    declare
        counter_ integer := 0;
        tablename_ text := 'temptable';
    begin
        raise notice 'trigger fired';
        select count(42) into counter_ 
            from pg_catalog.pg_tables where tablename = tablename_;
        if counter_ = 0 then
            raise notice 'Creating table %', tablename_;
            execute 'create temporary table ' || tablename_ || ' (counter integer) on commit drop';
            execute 'insert into ' || tablename_ || ' (counter) values(1)';
    
            execute 'select counter from ' || tablename_ into counter_;
            raise notice 'Actual value for counter= [%]', counter_;
        else
            execute 'select counter from ' || tablename_ into counter_;
            execute 'update ' || tablename_ || ' set counter = counter + 1';
            raise notice 'updating';
            execute 'select counter from ' || tablename_ into counter_;
            raise notice 'Actual value for counter= [%]', counter_;
    
            if counter_ > 4 then
                raise exception 'Cannot change more than 4 rows in one trancation';
            end if;
    
        end if;
        return new;
    end; $$ language plpgsql;
    
    
    create trigger trg_bu_test before 
      update on test 
      for each row
      execute procedure trg_check_max_4_updated_records();
    
    update test set id = 10 where id <= 1;
    update test set id = 10 where id <= 2;
    update test set id = 10 where id <= 3;
    update test set id = 10 where id <= 4;
    update test set id = 10 where id <= 5;
    
    rollback;
    

    The main idea is to have a trigger on ‘before update for each row’ that creates (if necessary) a temporary table (that is dropped at the end of transaction). In this table there is just one row with one value, that is the number of updated rows in current transaction. For each update the value is incremented. If the value is bigger than 4, the transaction is stopped.

    But I think that this is a wrong solution for your problem. What’s a problem to run such wrong query that you’ve written about, twice, so you’ll have 8 rows changed. What about deletion rows or truncating them?

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

Sidebar

Related Questions

I don't know if this is possible. I want to save a number after
I want to know how to count the number of anchor tags present within
I want to know how can i format a number like 2000000 into standard
I want to know how to get a 4-digit random number in Java. I
i want know if is possible, to get a specific element value of a
Given that I want my string grid to have equal height rows ... I
I'm creating a layout for an iPad app that will consist of xx number
I have a SQL query that returns a number of results (unfortunately, the number
I don't know if this is possible or not. I have a workbook that
I want to create counters in MySQL database that stores the number of views

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.