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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:29:04+00:00 2026-05-24T16:29:04+00:00

In a trigger I want to see what sql query triggered this trigger. I

  • 0

In a trigger I want to see what sql query triggered this trigger. I used current_query() function of postgresql (8.4).

Everything is fine, but if trigger is executed by prepared statement I get placeholders ($1) instead of correct values. for example (logged query):

delete from some_table where id=$1

Is there a way to get/have this values/parameters?

Edited (example added):

--table for saving query
create table log_table (
query text
)

--table for trigger
create table some_table (
id text
)
--function itself
CREATE FUNCTION save_query() RETURNS trigger AS $$
    switch $TG_op {
    DELETE {
            spi_exec "INSERT INTO log_table (query) VALUES (current_query())"
        }
    default {
            return OK
        }
    }
    return OK
$$ LANGUAGE pltcl;

Creating a trigger:

create trigger test_trigger before delete on some_table for each row execute procedure save_query();

Prepared statement is executed from hibernate.

Edit again (java part added)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class DeleteUsingPreparedStmt {

    public static void main(String[] args) {
        try {
            String deleteString = "delete from floors where id = ? ";
            final int idToDelte = 1;

            Class.forName("org.postgresql.Driver");
            String url = "jdbc:postgresql://127.0.0.1:5432/YOUR_DATABASE";
            Connection conn = DriverManager.getConnection(url, "user", "password");

            PreparedStatement deleteStmt = conn.prepareStatement(deleteString);
            deleteStmt.setInt(1, idToDelte);
            deleteStmt.executeUpdate();
        } catch (Exception e) {
            //hide me :)
        }
    }
}

You need a jdbc driver – click.

  • 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-24T16:29:06+00:00Added an answer on May 24, 2026 at 4:29 pm

    Here is some working code (for Debian users: just install postgresql-pltcl-8.4 package and run CREATE LANGUAGE pltcl;)

    CREATE TABLE log_table (
        id serial,
        query text
    );
    
    CREATE TABLE floors (
        id serial,
        value text
    );
    
    INSERT INTO floors(value) VALUES ('aaa'), ('bbb');
    
    CREATE OR REPLACE FUNCTION save_query() RETURNS trigger AS $$
        switch $TG_op {
            DELETE {
                spi_exec "INSERT INTO log_table (query) VALUES (current_query())"
            }
            default {
                return OK
            }
        }
        return OK
    $$ LANGUAGE pltcl;
    
    CREATE TRIGGER test_trigger
        BEFORE DELETE ON floors
        FOR EACH ROW
        EXECUTE PROCEDURE save_query();
    

    However I can’t get placeholders in prepared statement (it returns EXECUTE deleteFromFloors(2);):

    TABLE log_table;
     id | query 
    ----+-------
    (0 rows)
    DELETE FROM floors WHERE id = 1;
    DELETE 1
    TABLE log_table;
     id |              query               
    ----+----------------------------------
      1 | DELETE FROM floors WHERE id = 1;
    (1 row)
    PREPARE deleteFromFloors(integer) AS
        DELETE FROM floors WHERE id = $1;
    PREPARE
    EXECUTE deleteFromFloors(2);
    DELETE 1
    TABLE log_table;
     id |              query               
    ----+----------------------------------
      1 | DELETE FROM floors WHERE id = 1;
      2 | EXECUTE deleteFromFloors(2);
    (2 rows)
    

    EDIT:

    As a workaround use OLD record (represented as array in Tcl), fetch id column from there and use replace function to put it instead of $1. Here you have two solutions: PL/pgSQL and PL/Tcl:

    CREATE OR REPLACE FUNCTION save_query() RETURNS TRIGGER AS $$
    BEGIN
        INSERT INTO log_table (query)
            VALUES (replace(current_query(), '$1', OLD.id::text));
        RETURN OLD;
    END;
    $$ LANGUAGE plpgsql;
    
    CREATE OR REPLACE FUNCTION save_query() RETURNS trigger AS $$
        switch $TG_op {
            DELETE {
                spi_exec "INSERT INTO log_table (query)
                    VALUES (replace(current_query(), '\$1', '$OLD(id)'))"
            }
            default {
                return OK
            }
        }
        return OK
    $$ LANGUAGE pltcl;
    

    Result:

    java -classpath '.:postgresql-8.4-702.jdbc4.jar' DeleteUsingPreparedStmt
    
    TABLE log_table;
     id |              query              
    ----+---------------------------------
      1 | delete from floors where id = 1
    

    (1 row)

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

Sidebar

Related Questions

I often want to trigger a certain function just once, but I need to
If I want to trigger an error in my interpreter I call this function:
I want to trigger on click of a button this: <asp:Button runat=server ID=submit Text=Submit
I have a trigger in SQL Server 2005 that is used to track audit
I need to alter a trigger in sql server 2005 and I want to
I want to trigger an event that causes the ScrollPane to start scrolling up
I want to trigger a hyperlink by hitting Enter using jQuery. I have the
I want to trigger a special action in the save() method of a Django
I want to trigger javascript alert using PHP. Is it possible I want to
So I want to trigger an event (pausing/unpausing some media) whenever the user presses

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.