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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:15:14+00:00 2026-06-01T00:15:14+00:00

I realize there are is no TRIGGER ON SELECT in Postgres. Given a table

  • 0

I realize there are is no TRIGGER ON SELECT in Postgres. Given a table like so

CREATE TABLE t (
    a INTEGER PRIMARY KEY, 
    b TEXT, 
    entered_by INTEGER, 
    qry_count INTEGER
);

I want to increment “qry_count” for every SELECT for every “entered_by”, essentially keeping track of how many times any record is queried for every “enterer”. For example,

SELECT * a, b FROM t WHERE <condition>;

might return “n” rows entered by different enterers. For each enterer, I want to qry_count++. Pseudocode ahead

FOR EVERY entered_by IN SELECT
    UPDATE t
    SET qry_count = qry_count + 1
    WHERE entered_by = <entered_by>

I could do this most easily in my application, but I am wondering if doing this in the database itself might be best. I found an example of where I think I want to go, but it is for PL/SQL. What is the best way to accomplish this with Pg?

Update: In Perl I would do it like so

$sth_sel = $dbh->prepare( .. complicated SELECT includes "entered_by" ..);
$sth_upd = $dbh->prepare("UPDATE t SET qry_count = qry_count + 1 WHERE entered_by = ?");

$sth_sel->execute( .. bind params ..);
while (my $r = $sth_sel->fetchrow_arrayref) {
    my $entered_by = $r->[ 7 ]; # or whatever
    $sth_upd->execute($entered_by);

    .. do other things with $sth_sel, perhaps build a JSON obj to return ..
}

That does seem the easiest, but it would be nice to build this functionality right as a core part of the data schema.

UPDATE2: Most examples (including suggestions below) rely on creating a PL/PgSQL function. The problem with this approach is that my query is hard-coded in the function. Even if it can take input params, it is still a pre-declared query. This means I have to create a separate function for each query. In reality, in my application I construct the queries dynamically based on what is requested by the user (via the web). The requested columns can change, the supplied params can change. I think I am looking for a SQL analogous to the Perl pseudocode above without a predeclared function (SQL pseudocode below)

BEGIN
    FOR row IN
        SELECT to my hearts content
        FROM whatever tables I want JOINed horrendously
        WHERE any arbitrary param
    LOOP
        eb := row[entered_by]
        UPDATE t SET qry_count = qry_count + 1 WHERE entered_by = eb
        RETURN NEXT row;
    END LOOP;
    RETURN;
END

Hope this makes my objective clearer.

  • 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-01T00:15:15+00:00Added an answer on June 1, 2026 at 12:15 am

    Notice the default value for the qry_count column:

    CREATE TABLE t (
        a INTEGER PRIMARY KEY, 
        b TEXT, 
        entered_by INTEGER, 
        qry_count INTEGER default 0
    );
    
    create function select_and_update(parameter text)
    returns setof t as $$
        update t
        set qry_count = qry_count + 1
        from (
            select a
            from t
            where b = $1
            ) s
        where t.a = s.a
        ;
        select *
        from t
        where b = $1
        ;
    $$ language sql;
    

    Now query the table using the above function:

    select * from select_and_update('a');
    

    Update according to comment:

    You can build it dynamically and in instead of a function just wrap the sql code, whatever it is, in a transaction. No need for cursors.

    begin;
        update t
        set qry_count = qry_count + 1
        from (
            select a
            from t
            where b = 'a'
            ) s
        where t.a = s.a
        ;
        select *
        from t
        where b = 'a'
        ;
    commit;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to quickly send email from the command line. I realize there
I realize there's already been several questions like this, but I think my case
I realize there is a somewhat related thread on this here: Loading assemblies and
I realize there's no definitely right answer to this question, but when people talk
I realize there's a way in Vim to hide/fold lines, but what I'm looking
I realize there are similar questions on this topic, but I still cannot find
I realize there are various kinds of software projects: commercial (for John Doe) industrial
I realize that since UNIX sockets are platform-specific, there has to be some non-Java
In modern versions of Oracle, is there some standard (stored procedure, additional CREATE syntax,
I realize there are similar questions but only regarding saving files on Linux servers

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.