Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server crashes due to the lack of memory #33

Closed
laurenz opened this issue Jan 25, 2024 · 10 comments
Closed

Server crashes due to the lack of memory #33

laurenz opened this issue Jan 25, 2024 · 10 comments
Assignees
Labels
bug Something isn't working irreproducible The issued behavior is not reproducible

Comments

@laurenz
Copy link
Contributor

laurenz commented Jan 25, 2024

This was raised on the pgsql-bugs list:
https://www.postgresql.org/message-id/flat/18308-f55e8ec2ee192cb5%40postgresql.org

There seem to be crashes with PostgreSQL 15.4 and pg_shao_plans with the query

select tc.table_schema, tc.table_name, 
       tc.constraint_type,tc.constraint_name, tc.is_deferrable,
       tc2.table_name as child_table,
       rc.unique_constraint_name as refer_key,
       case when rc.unique_constraint_name is not null then
            (select tc1.table_name 
               from information_schema.table_constraints tc1
              where tc1.constraint_name = rc.unique_constraint_name)
       end as ref_table,
       cc.check_clause,
       kc.column_name,
       kc.ordinal_position
  from information_schema.table_constraints tc
  left join information_schema.referential_constraints rc
    on tc.constraint_name = rc.constraint_name
  left join information_schema.check_constraints cc
    on tc.constraint_name = cc.constraint_name
  left join information_schema.key_column_usage kc
    on tc.constraint_name = kc.constraint_name
  left join LATERAL (
       select tc2.table_name, rc1.unique_constraint_name 
              from information_schema.referential_constraints rc1
              join information_schema.table_constraints tc2
                on rc1.constraint_name = tc2.constraint_name 
             where tc.constraint_name = rc1.unique_constraint_name
             ) tc2 on true
 where tc.table_name = 't1';

I didn't try to reproduce the error.

@laurenz laurenz added the bug Something isn't working label Jan 25, 2024
@japinli
Copy link

japinli commented Jan 25, 2024

It seems space_left is less then zero, which leads later memcpy crash. Here we have no space for the plan. Maybe we should skip the plan if there has no space left.

diff --git a/pg_show_plans.c b/pg_show_plans.c
index 3eb2d3c..d560f71 100644
--- a/pg_show_plans.c
+++ b/pg_show_plans.c
@@ -283,6 +283,10 @@ append_query_plan(ExplainState *es)
 		offset += pgsp_cache->plan_len[i] + 1;
 	space_left = max_plan_length - offset;

+	/* skip the query plan if no space left */
+	if (space_left <= 0)
+		return;
+
 	if (pgsp->plan_format == EXPLAIN_FORMAT_TEXT)
 		new_plan->len--; /* Discard '\n'. */

@kovmir
Copy link
Collaborator

kovmir commented Jan 25, 2024

Thanks @japinli! Even if this code section is not the cause of the crashes, it is still error prone.

@kovmir kovmir self-assigned this Jan 26, 2024
@kovmir
Copy link
Collaborator

kovmir commented Jan 26, 2024

@laurenz, I cannot reproduce that. What about you?

@laurenz
Copy link
Contributor Author

laurenz commented Jan 26, 2024

I didn't try... did you try creating a table t1 with some constraints?

If you cannot reproduce it, just close it as not reproducible. Sorry for creating work for you.

@kovmir
Copy link
Collaborator

kovmir commented Jan 26, 2024

did you try creating a table t1 with some constraints?

Yes, I did. I followed all the steps specified on the mailing lists thread you had linked.

@kovmir
Copy link
Collaborator

kovmir commented Jan 26, 2024

@japinli, can you please try out the code from this branch and see whether it fixes your problem?

@japinli
Copy link

japinli commented Jan 26, 2024 via email

@japinli
Copy link

japinli commented Jan 26, 2024

@japinli, can you please try out the code from this branch and see whether it fixes your problem?

Thanks for working on this. It seems fix the crash.

After reading the patch, now it must hold all of the new plan, not partial, right? Is that expected?

@kovmir
Copy link
Collaborator

kovmir commented Jan 26, 2024

Prior to this patch the extension was supposed to snip the part of a plan that does not fit and store the leftover. With this patch applied, the extension ignores the plan that does not fit and emits a warning instead.

@kovmir kovmir mentioned this issue Jan 27, 2024
@kovmir
Copy link
Collaborator

kovmir commented Jan 27, 2024

Fixed in 572fa77.

@kovmir kovmir closed this as completed Jan 27, 2024
@kovmir kovmir changed the title Crashes with a metadata query Server crashes due to the lack of memory Jan 27, 2024
@kovmir kovmir added the irreproducible The issued behavior is not reproducible label Jan 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working irreproducible The issued behavior is not reproducible
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants