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 died #236

Open
2 tasks done
riderx opened this issue Feb 7, 2024 · 2 comments
Open
2 tasks done

Server died #236

riderx opened this issue Feb 7, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@riderx
Copy link

riderx commented Feb 7, 2024

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

Sometimes the requested query is too long to run or too big i'm not sure, and then the CPU of supabase goes crazy, the only way to make it go down is to reboot the instance, can we have a way to limit the response time or the usage of the wrapper to not kill the db itself?

Expected behavior

Never make the db died

Screenshots

If applicable, add screenshots to help explain your problem.

System information

  • OS: [e.g. macOS, Windows]
  • Browser (if applies) [e.g. chrome, safari]
  • Version of supabase-js: [e.g. 6.0.2]
  • Version of Node.js: [e.g. 10.10.0]

Additional context

Add any other context about the problem here.

@riderx riderx added the bug Something isn't working label Feb 7, 2024
@imor
Copy link
Contributor

imor commented Feb 8, 2024

Can you please share the query that you were running? Which wrapper was this?

@WcaleNieWolny
Copy link

@imor I work with @riderx and after having to disable clickhouse FDW we concluded it's a very, very wierd behaviour of the push down where clause support in clickhouse. Here is what I am talking about:

CREATE OR REPLACE FUNCTION public.get_total_stats_v4(userid uuid)
RETURNS TABLE(mau bigint, bandwidth double precision, storage double precision)
LANGUAGE plpgsql SECURITY DEFINER
AS $$
DECLARE
    anchor_start date;
    anchor_end date;
    usage_table_name text;
BEGIN
    SELECT subscription_anchor_start, subscription_anchor_end INTO anchor_start, anchor_end
    FROM stripe_info
    WHERE customer_id=(SELECT customer_id from users where id=userid);

    RETURN QUERY SELECT 
            COALESCE(MAX(clickhouse_app_usage.mau), 0)::bigint AS mau,
            COALESCE(round(convert_bytes_to_gb(SUM(clickhouse_app_usage.bandwidth))::numeric,2), 0)::float AS bandwidth,
            COALESCE(round(convert_bytes_to_gb(SUM(clickhouse_app_usage.storage_added - clickhouse_app_usage.storage_deleted))::numeric,2), 0)::float AS storage
        FROM clickhouse_app_usage
        WHERE app_id IN (SELECT app_id from apps where user_id=userid)
        AND date >= anchor_start
        AND date <= anchor_end
        LIMIT 1;
END;  
$$;

CREATE OR REPLACE FUNCTION public.get_total_stats_v5(userid uuid)
RETURNS TABLE(mau bigint, bandwidth double precision, storage double precision)
LANGUAGE plpgsql SECURITY DEFINER
AS $$
DECLARE
    anchor_start date;
    anchor_end date;
    app_ids text[];
    usage_table_name text;
BEGIN
    SELECT subscription_anchor_start, subscription_anchor_end INTO anchor_start, anchor_end
    FROM stripe_info
    WHERE customer_id=(SELECT customer_id from users where id=userid);

    -- Retrieve the app_ids into the variable
    SELECT array_agg(app_id) INTO app_ids FROM apps WHERE user_id=userid;

    -- Use the app_ids variable in the query
    RETURN QUERY SELECT 
            COALESCE(MAX(clickhouse_app_usage.mau), 0)::bigint AS mau,
            COALESCE(round(convert_bytes_to_gb(SUM(clickhouse_app_usage.bandwidth))::numeric,2), 0)::float AS bandwidth,
            COALESCE(round(convert_bytes_to_gb(SUM(clickhouse_app_usage.storage_added - clickhouse_app_usage.storage_deleted))::numeric,2), 0)::float AS storage
        FROM clickhouse_app_usage
        WHERE app_id = any(app_ids)
        AND date >= anchor_start
        AND date <= anchor_end
        LIMIT 1;
END;  
$$;

As you can see, in v4 I do WHERE app_id IN (SELECT app_id from apps where user_id=userid) but in v5 I do WHERE app_id = any(app_ids). This is a huge difference, it causes the behavior of the FDW to fundamentally change
image

The issue we were experiencing was caused by a very high cpu usage. This function was called really often and it was incredibly expensive.

As a recommendation I would suggest changing the docs to inform about this behavior or fix the issue

kamyshdm pushed a commit to dymium-io/supabase-wrappers that referenced this issue Jun 17, 2024
[supabase#190-again] Fix for 190, and deletion of selected GD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants