Skip to content

Fix plans counter - #820

Open
artemgavrilov wants to merge 1 commit into
mainfrom
fix-plans
Open

Fix plans counter#820
artemgavrilov wants to merge 1 commit into
mainfrom
fix-plans

Conversation

@artemgavrilov

@artemgavrilov artemgavrilov commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

plans counter was incremented unconditionally, even when there was no plan at all (utility statements). Fix it so it will reflect how many times planning events happened. Also this fixes associated timers.

@artemgavrilov
artemgavrilov marked this pull request as ready for review August 7, 2026 17:14
@codecov

codecov Bot commented Aug 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.49%. Comparing base (0b6731e) to head (2cab212).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #820      +/-   ##
==========================================
+ Coverage   88.46%   88.49%   +0.02%     
==========================================
  Files           3        3              
  Lines        1318     1321       +3     
  Branches      187      188       +1     
==========================================
+ Hits         1166     1169       +3     
  Misses         77       77              
  Partials       75       75              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread regression/expected/planning.out Outdated

RESET plan_cache_mode;
-- the generic plan is built once and then reused, so a replan forced by a
-- cache invalidation still leaves fewer plans than calls

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this even true? Also how do you imagine a cache invalidation would force a replan? This looks like LLMed nonsense.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is indeed LLM slope. I haven't done proper review of that part. Reworked.

Comment thread regression/expected/planning.out Outdated
?column?
----------
42
(1 row)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 4 executions?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason. Changed to 2.

planned | fewer_plans_than_calls | min_plan_time_positive | calls
---------+------------------------+------------------------+-------
t | t | t | 4
(1 row)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you check for that minimum plan time is positive?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before fix time counter may be decremented down to zero. I reworked this test to check only plan counter, I think that's enough.

t
---
t
(1 row)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why we need to reset stats.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To not have collision with previous test case. Simple select and prepared are pretty much the same query and fills the same entry in PGSM.

Comment thread src/pg_stat_monitor.c Outdated
* src may carry more than one planning event (a statement can be planned
* several times before it is executed, e.g. by EXPLAIN); all of them are
* counted, but their accumulated time is still folded as one sample.
*/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite useless comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

Comment thread src/pg_stat_monitor.c Outdated
if (src->plancalls.calls > 0)
{
double old_mean = dst->plantime.mean_time;
bool first_sample = (dst->plancalls.calls == 0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just move incrementing .calls until later and check if calls is zero directly in the if?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread CHANGELOG.md Outdated
- Make sure that for prepared statements utility statement exec info read at the executor start hook, where data is not yet modified by query itself
- Do not acquire LWLock under spinlock
- Race condition where we could leak memory for the parent query
- The `plans` counter counts how many times a statement was actually planned.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be clarified a bit. Also it should not end in full stop.

plans counter was incremented uncondidionally, even when there was no
plan at all (utility statements). Fix it so it will reflect how many
times planning events happened. Also this fixes associated timers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants