Skip to content

Avoid using view_queue to retrieve next command in PostgreSQL#227

Open
roperzh wants to merge 1 commit intomicromdm:mainfrom
roperzh:postgres-view-queue
Open

Avoid using view_queue to retrieve next command in PostgreSQL#227
roperzh wants to merge 1 commit intomicromdm:mainfrom
roperzh:postgres-view-queue

Conversation

@roperzh
Copy link
Copy Markdown

@roperzh roperzh commented Dec 30, 2025

This change addresses high CPU usage in PostgreSQL when retrieving commands from large queues.

This follows commit c771e92 which implemented a similar optimization for MySQL by:

  • Removing view_queue in favor of a direct table query + optimized indexes
  • Using parameterized query instead of string concatenation which enables prepared statement plan reuse

This change addresses high CPU usage in PostgreSQL when retrieving
commands from large queues.

This follows commit c771e92 which implemented a similar optimization for MySQL by:

- Removing `view_queue` in favor of a direct table query + optimized indexes
- Using parameterized query instead of string concatenation which enables prepared statement plan reuse.
@roperzh roperzh force-pushed the postgres-view-queue branch from 0cf42f2 to 4fa9c5a Compare December 30, 2025 15:54
@@ -0,0 +1,7 @@
CREATE INDEX idx_enrollment_queue_active_retrieval
ON enrollment_queue (id, priority DESC, created_at)
WHERE active = TRUE;
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

note: I saw that the MySQL PR also updated the schema.sql file, let me know if that's necessary and I'll do it!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes we the primary schema.sql should represent the "current" complete schema. A new user wouldn't run through the migrations.


CREATE INDEX idx_command_results_lookup
ON command_results (id, command_uuid)
INCLUDE (status, updated_at);
Copy link
Copy Markdown
Author

@roperzh roperzh Dec 30, 2025

Choose a reason for hiding this comment

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

we already have a primary key on (id, command_uuid), but the INCLUDE here allows us to store this info in the index.

having that said, I have no problems removing if you think it's redundant

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Interesting. What's rationale/reason here?

@roperzh roperzh marked this pull request as ready for review December 30, 2025 16:00
@roperzh roperzh changed the title Avoid using view_queue to retrieve next command Avoid using view_queue to retrieve next command in PostgreSQL Dec 30, 2025
Copy link
Copy Markdown
Member

@jessepeterson jessepeterson left a comment

Choose a reason for hiding this comment

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

Hey there, sorry for the delay, and thanks!! Yeah we'll want to include the changes in the main schema. Also just an FYI for #213 which is probably destined for the psql backend, too.

@@ -0,0 +1,7 @@
CREATE INDEX idx_enrollment_queue_active_retrieval
ON enrollment_queue (id, priority DESC, created_at)
WHERE active = TRUE;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes we the primary schema.sql should represent the "current" complete schema. A new user wouldn't run through the migrations.


CREATE INDEX idx_command_results_lookup
ON command_results (id, command_uuid)
INCLUDE (status, updated_at);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Interesting. What's rationale/reason here?

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