# Why are the queryid different?

**URL:** <https://forums.percona.com/t/why-are-the-queryid-different/35979>\
**Category:** pg\_stat\_monitor\
**Created:** [January 6, 2025, 7:44am UTC](https://forums.percona.com/t/why-are-the-queryid-different/35979 "2025-01-06T07:44:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ysh648](https://avatars.discourse-cdn.com/v4/letter/y/e19b73/32.png) [@ysh648](https://forums.percona.com/u/ysh648)\
**Post date:** [January 6, 2025, 7:44am UTC](https://forums.percona.com/t/why-are-the-queryid-different/35979/1 "2025-01-06T07:44:18Z")

</div>

Hello,

I am using pg\_stat\_statements and pg\_stat\_activity and pg\_stat\_monitor.  
I am curious why the query id is different in the three views.

---

<div class="post-metadata">

**Author:** ![mateusz.henicz](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/mateusz.henicz/32/6935_2.png) [@mateusz.henicz](https://forums.percona.com/u/mateusz.henicz)\
**Post date:** [January 7, 2025, 10:46am UTC](https://forums.percona.com/t/why-are-the-queryid-different/35979/2 "2025-01-07T10:46:12Z")

</div>

Hello,  
Can you provide some examples?  
If you are checking the same postgresql cluster for all the queries, then the query id should be the same as long as they are using the same execution plans. If plans are different it may mean that your data statistics are not up to date, and postgres is sometimes using non optimal execution plans, resulting in different query id, and after statistics are updated plan changes back to the good one.

---

<div class="post-metadata">

**Author:** ![ysh648](https://avatars.discourse-cdn.com/v4/letter/y/e19b73/32.png) [@ysh648](https://forums.percona.com/u/ysh648)\
**Post date:** [January 7, 2025, 11:27am UTC](https://forums.percona.com/t/why-are-the-queryid-different/35979/3 "2025-01-07T11:27:18Z")

</div>

This is a simple example.

```auto
CREATE TABLE customers (
    customer_id SERIAL PRIMARY KEY,
    customer_name VARCHAR(100),
    join_date DATE
);
CREATE TABLE orders (
    order_id SERIAL PRIMARY KEY,
    customer_id INT REFERENCES customers(customer_id),
    order_date DATE,
    amount DECIMAL(10, 2)
);
CREATE TABLE order_items (
    item_id SERIAL PRIMARY KEY,
    order_id INT REFERENCES orders(order_id),
    product_name VARCHAR(100),
    quantity INT,
    price DECIMAL(10, 2)
);

INSERT INTO customers (customer_name, join_date) 
SELECT md5(random()::text), date '2021-01-01' + (random() * 1000)::int
FROM generate_series(1, 10000);
-- orders 
INSERT INTO orders (customer_id, order_date, amount)
SELECT customer_id, date '2021-01-01' + (random() * 1000)::int, random() * 1000
FROM customers
ORDER BY random()
LIMIT 100000;
-- order_items 
INSERT INTO order_items (order_id, product_name, quantity, price)
SELECT order_id, md5(random()::text), (random() * 10)::int, random() * 100
FROM orders
ORDER BY random()
LIMIT 1000000;

```

```auto
SELECT
    c.customer_id,
    c.customer_name,
    o.order_id,
    o.order_date,
    (SELECT SUM(oi.price * oi.quantity) FROM order_items oi WHERE oi.order_id = o.order_id) AS order_total,
    (SELECT AVG(amount) FROM orders o2 WHERE o2.customer_id = c.customer_id) AS avg_order_amount
FROM
    customers c
    JOIN orders o ON c.customer_id = o.customer_id
WHERE
    o.amount > 50
ORDER BY
    c.customer_id, o.order_date;

```

The above query seems to have a long execution time. While executing the above query, if you compare the query ID of pg\_stat\_activity view and the query ID of pg\_stat\_monitor collected after completing the query, you can see that they are different. I would like to know why this is different.

---

<div class="post-metadata">

**Author:** ![mateusz.henicz](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/mateusz.henicz/32/6935_2.png) [@mateusz.henicz](https://forums.percona.com/u/mateusz.henicz)\
**Post date:** [January 8, 2025, 7:55am UTC](https://forums.percona.com/t/why-are-the-queryid-different/35979/4 "2025-01-08T07:55:51Z")

</div>

I tried to reproduce this with your provided script.  
In my example both query ids are exactly the same: -7907767505760499297  
Please have a look at attached screen shots.

 ![image](https://us1.discourse-cdn.com/flex019/uploads/percona1/original/3X/c/5/c5b4d4b0fd0d1d30ce65d40602fb113fc8d45d63.png)  
 ![image](https://us1.discourse-cdn.com/flex019/uploads/percona1/original/3X/2/0/2037ab3bc4a66dea11ca3eee31d54f459d42dd97.png)

Are you sure you are comparing query\_id to queryid, and not pgsm\_query\_id?

---

<div class="post-metadata">

**Author:** ![ysh648](https://avatars.discourse-cdn.com/v4/letter/y/e19b73/32.png) [@ysh648](https://forums.percona.com/u/ysh648)\
**Post date:** [January 8, 2025, 8:05am UTC](https://forums.percona.com/t/why-are-the-queryid-different/35979/5 "2025-01-08T08:05:45Z")

</div>

Fortunately, I wasn’t confused.

What does the above result look like after the query is finished?

I tested it like this.

1. Run a query and check pg\_stat\_activity.
2. Force terminate a long-running query and check pg\_stat\_monitor.

At this time, I confirmed that the query id of pg\_stat\_activity and the query id of pg\_stat\_monitor are different.

I have a question here. I am not a DBA, so this may be a confusing problem.

1. Is pg\_stat\_monitor supposed to collect after a query ends?
2. Since there is no pid, there is no way to check if it is the same query. In this case, is it right to rely only on the query id?

---

<div class="post-metadata">

**Author:** ![mateusz.henicz](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/mateusz.henicz/32/6935_2.png) [@mateusz.henicz](https://forums.percona.com/u/mateusz.henicz)\
**Post date:** [January 8, 2025, 8:22am UTC](https://forums.percona.com/t/why-are-the-queryid-different/35979/6 "2025-01-08T08:22:44Z")

</div>

It is a different story then, if you terminate the query it may get different query\_id, but you will see additional values in pg\_stat\_monitor, in columns: elevel, sqlcode and message, that the query was not finished properly. See below:

 ![image](https://us1.discourse-cdn.com/flex019/uploads/percona1/original/3X/5/e/5eb51fc62a194668f74a00f1a70e392aa7c0fc02.png)

You can query all queries that are using the same relations, are of the same type (SELECT, UPDATE, etc.) and maybe have some part of query is also the same, using like ‘%o.amount \>50%’, or even use full query if you need to check if those are exact matches. Query\_ids are different, because one is for query that finished properly, and other that were interrupted for some reason. Their execution was different, so they have different query\_id.

---

<div class="post-metadata">

**Author:** ![ysh648](https://avatars.discourse-cdn.com/v4/letter/y/e19b73/32.png) [@ysh648](https://forums.percona.com/u/ysh648)\
**Post date:** [January 8, 2025, 8:43am UTC](https://forums.percona.com/t/why-are-the-queryid-different/35979/7 "2025-01-08T08:43:41Z")

</div>

Thank you. I understood it well.
