--
-- PostgreSQL database dump
--

\restrict M18FnaokuZFKQZjWu9az6dzOO6o2cSlpJgAocs2PXfRbmzHVcXEXaiZZcaOPZko

-- Dumped from database version 16.0
-- Dumped by pg_dump version 17.6

-- Started on 2025-10-24 14:30:46

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET transaction_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

--
-- TOC entry 6 (class 2615 OID 1891106)
-- Name: def; Type: SCHEMA; Schema: -; Owner: dbo
--

CREATE SCHEMA def;


ALTER SCHEMA def OWNER TO dbo;

--
-- TOC entry 7 (class 2615 OID 1891160)
-- Name: ngcloud_ru; Type: SCHEMA; Schema: -; Owner: vc_billing
--

CREATE SCHEMA ngcloud_ru;


ALTER SCHEMA ngcloud_ru OWNER TO vc_billing;

--
-- TOC entry 8 (class 2615 OID 2160630)
-- Name: s3billing; Type: SCHEMA; Schema: -; Owner: s3billing
--

CREATE SCHEMA s3billing;


ALTER SCHEMA s3billing OWNER TO s3billing;

--
-- TOC entry 246 (class 1255 OID 2426393)
-- Name: billing(timestamp with time zone, timestamp with time zone, character varying); Type: FUNCTION; Schema: s3billing; Owner: dsorokin
--

CREATE FUNCTION s3billing.billing(_ds timestamp with time zone, _df timestamp with time zone, _user character varying) RETURNS TABLE(bucket_name character varying, placement character varying, size numeric, put numeric, get numeric, bytes_sent numeric)
    LANGUAGE plpgsql
    AS $$
BEGIN
SELECT  
  s3billing.bucket_info.name AS bucket_name
 ,s3billing.placement.name AS placement
 ,coalesce(round(sum(usage_rgw_main_size_actual)/(24*(SELECT DATE_PART('day', (_ds-_df))))),0) AS size
 ,coalesce(s2.get, 0) AS get
 ,coalesce(s2.put, 0) AS put
 ,coalesce(s2.bytes_sent, 0) as bytes_sent
FROM s3billing.bucket_stat
INNER JOIN s3billing.placement ON s3billing.bucket_stat.placement_id=s3billing.placement.id
INNER JOIN s3billing.bucket_info ON s3billing.bucket_stat.bucket_id=s3billing.bucket_info.id
LEFT JOIN (
 SELECT
   s1.bucket_id AS bucket_id
  ,s1.bucket_name AS bucket_name
  ,s1.placement AS placement
  ,coalesce(sum(s1.get) ,0) AS get
  ,coalesce(sum(s1.put) ,0) AS put
  ,coalesce(sum(s1.bytes_sent), 0) AS bytes_sent
 FROM (
  SELECT
    s3billing.bucket_info.id AS bucket_id
   ,s3billing.bucket_info.name AS bucket_name
   ,s3billing.placement.name AS placement
   ,(CASE
     WHEN (  s3billing.category.name LIKE 'get%'
          OR s3billing.category.name LIKE 'head%'
          OR s3billing.category.name LIKE 'options%'
          ) THEN coalesce(sum(ops), 0)
     END) AS get
   ,(CASE
     WHEN (  s3billing.category.name LIKE 'put%'
          OR s3billing.category.name LIKE 'post%'
          OR s3billing.category.name LIKE 'patch%'
          OR s3billing.category.name LIKE 'list%'
          ) THEN coalesce(sum(ops), 0)
     END) AS put
   ,coalesce(sum(usage_bucket_by_user.bytes_sent), 0) AS bytes_sent
  FROM
   s3billing.usage_bucket_by_user
  INNER JOIN s3billing.bucket_info ON s3billing.usage_bucket_by_user.bucketid    = s3billing.bucket_info.id
  INNER JOIN s3billing.placement   ON s3billing.bucket_info.placement_id         = s3billing.placement.id
  INNER JOIN s3billing.user_info   ON s3billing.usage_bucket_by_user.user_id     = s3billing.user_info.id
  INNER JOIN s3billing.category    ON s3billing.usage_bucket_by_user.category_id = s3billing.category.id

  WHERE
       s3billing.user_info.name=_user
   AND s3billing.usage_bucket_by_user.time >= _ds
   AND s3billing.usage_bucket_by_user.time < _df
  GROUP BY
   s3billing.bucket_info.name
  ,s3billing.placement.name
  ,s3billing.category.name
  ,s3billing.bucket_info.id
 ) AS s1
 GROUP BY
   s1.bucket_name
  ,s1.bucket_id
  ,s1.placement
 ORDER BY
   s1.placement
  ,s1.bucket_name
) AS s2 ON s2.bucket_id  = s3billing.bucket_stat.bucket_id
WHERE owner=_user
  AND timestamp_addition >= _ds
  AND timestamp_addition <  _df
GROUP BY
  s3billing.bucket_info.name
 ,s3billing.placement.name
 ,s3billing.bucket_stat.bucket_id
 ,s2.get
 ,s2.put
 ,s2.bytes_sent
ORDER BY
 s3billing.placement.name
,s3billing.bucket_info.name
;
END
$$;


ALTER FUNCTION s3billing.billing(_ds timestamp with time zone, _df timestamp with time zone, _user character varying) OWNER TO dsorokin;

--
-- TOC entry 260 (class 1255 OID 2434574)
-- Name: billing_per_user(character varying, character varying, character varying); Type: FUNCTION; Schema: s3billing; Owner: dsorokin
--

CREATE FUNCTION s3billing.billing_per_user(_ds character varying, _df character varying, _user character varying) RETURNS TABLE(bucket_name character varying, placement character varying, size numeric, get numeric, put numeric, bytes_sent numeric)
    LANGUAGE plpgsql
    AS $$
BEGIN
RETURN QUERY 

SELECT  
  s3billing.bucket_info.name AS bucket_name
 ,s3billing.placement.name AS placement
 ,coalesce(round(sum(usage_rgw_main_size_actual)/
 (SELECT (DATE_PART('day', 
        ((TO_TIMESTAMP(_df, 'YYYY-MM-DD HH24:MI:SS'))
         -TO_TIMESTAMP(_ds, 'YYYY-MM-DD HH24:MI:SS'))
       )*24+
       DATE_PART('hour', 
        ((TO_TIMESTAMP(_df, 'YYYY-MM-DD HH24:MI:SS'))
         -TO_TIMESTAMP(_ds, 'YYYY-MM-DD HH24:MI:SS'))
       )))
 
 )::numeric,0) AS size
 ,coalesce(s2.get, 0) AS get
 ,coalesce(s2.put, 0) AS put
 ,coalesce(s2.bytes_sent, 0) as bytes_sent
FROM s3billing.bucket_stat
INNER JOIN s3billing.placement ON s3billing.bucket_stat.placement_id=s3billing.placement.id
INNER JOIN s3billing.bucket_info ON s3billing.bucket_stat.bucket_id=s3billing.bucket_info.id
LEFT JOIN (
 SELECT
   s1.bucket_id AS bucket_id
  ,s1.bucket_name AS bucket_name
  ,s1.placement AS placement
  ,coalesce(sum(s1.get) ,0) AS get
  ,coalesce(sum(s1.put) ,0) AS put
  ,coalesce(sum(s1.bytes_sent), 0) AS bytes_sent
 FROM (
  SELECT
    s3billing.bucket_info.id AS bucket_id
   ,s3billing.bucket_info.name AS bucket_name
   ,s3billing.placement.name AS placement
   ,(CASE
     WHEN (  s3billing.category.name LIKE 'get%'
          OR s3billing.category.name LIKE 'head%'
          OR s3billing.category.name LIKE 'options%'
          ) THEN coalesce(sum(ops), 0)
     END) AS get
   ,(CASE
     WHEN (  s3billing.category.name LIKE 'put%'
          OR s3billing.category.name LIKE 'post%'
          OR s3billing.category.name LIKE 'patch%'
          OR s3billing.category.name LIKE 'list%'
          ) THEN coalesce(sum(ops), 0)
     END) AS put
   ,coalesce(sum(usage_bucket_by_user.bytes_sent), 0) AS bytes_sent

  FROM
   s3billing.usage_bucket_by_user
  INNER JOIN s3billing.bucket_info ON s3billing.usage_bucket_by_user.bucketid    = s3billing.bucket_info.id
  INNER JOIN s3billing.placement   ON s3billing.bucket_info.placement_id         = s3billing.placement.id
  INNER JOIN s3billing.user_info   ON s3billing.usage_bucket_by_user.user_id     = s3billing.user_info.id
  INNER JOIN s3billing.category    ON s3billing.usage_bucket_by_user.category_id = s3billing.category.id

  WHERE
       s3billing.user_info.name=_user
   AND s3billing.usage_bucket_by_user.time >=TO_TIMESTAMP(_ds, 'YYYY-MM-DD HH24:MI:SS')
   AND s3billing.usage_bucket_by_user.time < TO_TIMESTAMP(_df, 'YYYY-MM-DD HH24:MI:SS')
  GROUP BY
   s3billing.bucket_info.name
  ,s3billing.placement.name
  ,s3billing.category.name
  ,s3billing.bucket_info.id
 ) AS s1

 GROUP BY
   s1.bucket_name
  ,s1.bucket_id
  ,s1.placement

 ORDER BY
   s1.placement
  ,s1.bucket_name

) AS s2 ON s2.bucket_id  = s3billing.bucket_stat.bucket_id
WHERE owner=_user
  AND timestamp_addition >= TO_TIMESTAMP(_ds, 'YYYY-MM-DD HH24:MI:SS')
  AND timestamp_addition <  TO_TIMESTAMP(_df, 'YYYY-MM-DD HH24:MI:SS')
GROUP BY
  s3billing.bucket_info.name
 ,s3billing.placement.name
 ,s3billing.bucket_stat.bucket_id
 ,s2.get
 ,s2.put
 ,s2.bytes_sent
ORDER BY
 s3billing.placement.name
,s3billing.bucket_info.name
;
END;
$$;


ALTER FUNCTION s3billing.billing_per_user(_ds character varying, _df character varying, _user character varying) OWNER TO dsorokin;

--
-- TOC entry 261 (class 1255 OID 2435525)
-- Name: billing_per_user_fix_pl(character varying, character varying, character varying); Type: FUNCTION; Schema: s3billing; Owner: dsorokin
--

CREATE FUNCTION s3billing.billing_per_user_fix_pl(_ds character varying, _df character varying, _user character varying) RETURNS TABLE(bucket_name character varying, placement character varying, size numeric, get numeric, put numeric, bytes_sent numeric)
    LANGUAGE plpgsql
    AS $$
BEGIN
RETURN QUERY 

SELECT  
  s3billing.bucket_info.name AS bucket_name,
 (case 
    when (  s3billing.placement.name = 'default-placement') then 'HOT'
  WHEN (  not s3billing.placement.name = 'default-placement' ) THEN s3billing.placement.name
  end
 ) as placement,
 coalesce(round(sum(usage_rgw_main_size_actual)/
 (SELECT (DATE_PART('day', 
        ((TO_TIMESTAMP(_df, 'YYYY-MM-DD HH24:MI:SS'))
         -TO_TIMESTAMP(_ds, 'YYYY-MM-DD HH24:MI:SS'))
       )*24+
       DATE_PART('hour', 
        ((TO_TIMESTAMP(_df, 'YYYY-MM-DD HH24:MI:SS'))
         -TO_TIMESTAMP(_ds, 'YYYY-MM-DD HH24:MI:SS'))
       )))
 
 )::numeric,0) AS size
 ,coalesce(s2.get, 0) AS get
 ,coalesce(s2.put, 0) AS put
 ,coalesce(s2.bytes_sent, 0) as bytes_sent
FROM s3billing.bucket_stat
INNER JOIN s3billing.placement ON s3billing.bucket_stat.placement_id=s3billing.placement.id
INNER JOIN s3billing.bucket_info ON s3billing.bucket_stat.bucket_id=s3billing.bucket_info.id
LEFT JOIN (
 SELECT
   s1.bucket_id AS bucket_id
  ,s1.bucket_name AS bucket_name,
    (case 
    when (  s1.placement = 'default-placement') then 'HOT'
    WHEN (  not s1.placement = 'default-placement' ) THEN s1.placement
    end
    ) as placement
  ,coalesce(sum(s1.get) ,0) AS get
  ,coalesce(sum(s1.put) ,0) AS put
  ,coalesce(sum(s1.bytes_sent), 0) AS bytes_sent
 FROM (
  SELECT
    s3billing.bucket_info.id AS bucket_id
   ,s3billing.bucket_info.name AS bucket_name
   ,(case 
    when (  s3billing.placement.name = 'default-placement') then 'HOT'
    WHEN (  not s3billing.placement.name = 'default-placement' ) THEN s3billing.placement.name
     end
    ) as placement
   ,(CASE
     WHEN (  s3billing.category.name LIKE 'get%'
          OR s3billing.category.name LIKE 'head%'
          OR s3billing.category.name LIKE 'options%'
          ) THEN coalesce(sum(ops), 0)
     END) AS get
   ,(CASE
     WHEN (  s3billing.category.name LIKE 'put%'
          OR s3billing.category.name LIKE 'post%'
          OR s3billing.category.name LIKE 'patch%'
          OR s3billing.category.name LIKE 'list%'
          ) THEN coalesce(sum(ops), 0)
     END) AS put
   ,coalesce(sum(usage_bucket_by_user.bytes_sent), 0) AS bytes_sent

  FROM
   s3billing.usage_bucket_by_user
  INNER JOIN s3billing.bucket_info ON s3billing.usage_bucket_by_user.bucketid    = s3billing.bucket_info.id
  INNER JOIN s3billing.placement   ON s3billing.bucket_info.placement_id         = s3billing.placement.id
  INNER JOIN s3billing.user_info   ON s3billing.usage_bucket_by_user.user_id     = s3billing.user_info.id
  INNER JOIN s3billing.category    ON s3billing.usage_bucket_by_user.category_id = s3billing.category.id

  WHERE
       s3billing.user_info.name=_user
   AND s3billing.usage_bucket_by_user.time >=TO_TIMESTAMP(_ds, 'YYYY-MM-DD HH24:MI:SS')
   AND s3billing.usage_bucket_by_user.time < TO_TIMESTAMP(_df, 'YYYY-MM-DD HH24:MI:SS')
  GROUP BY
   s3billing.bucket_info.name
  ,s3billing.placement.name
  ,s3billing.category.name
  ,s3billing.bucket_info.id
 ) AS s1

 GROUP BY
   s1.bucket_name
  ,s1.bucket_id
  ,s1.placement

 ORDER BY
   s1.placement
  ,s1.bucket_name

) AS s2 ON s2.bucket_id  = s3billing.bucket_stat.bucket_id
WHERE owner=_user
  AND timestamp_addition >= TO_TIMESTAMP(_ds, 'YYYY-MM-DD HH24:MI:SS')
  AND timestamp_addition <  TO_TIMESTAMP(_df, 'YYYY-MM-DD HH24:MI:SS')
GROUP BY
  s3billing.bucket_info.name
 ,s3billing.placement.name
 ,s3billing.bucket_stat.bucket_id
 ,s2.get
 ,s2.put
 ,s2.bytes_sent
ORDER BY
 s3billing.placement.name
,s3billing.bucket_info.name
;
END;
$$;


ALTER FUNCTION s3billing.billing_per_user_fix_pl(_ds character varying, _df character varying, _user character varying) OWNER TO dsorokin;

--
-- TOC entry 263 (class 1255 OID 2437728)
-- Name: billing_per_user_fix_pl_sv(character varying, character varying, character varying); Type: FUNCTION; Schema: s3billing; Owner: postgres
--

CREATE FUNCTION s3billing.billing_per_user_fix_pl_sv(ds character varying, df character varying, _user character varying) RETURNS TABLE(bucket_name character varying, placement character varying, size numeric, get numeric, put numeric, bytes_sent numeric)
    LANGUAGE plpgsql
    AS $$

DECLARE
   _ds    TIMESTAMP;
   _df    TIMESTAMP;
   _days  INT;
   _hours INT;

BEGIN
SELECT TO_TIMESTAMP(ds, 'YYYY-MM-DD HH24:MI:SS') INTO _ds;
SELECT TO_TIMESTAMP(df, 'YYYY-MM-DD HH24:MI:SS') INTO _df;
SELECT DATE_PART('day', _df - _ds) INTO _days;
SELECT CEIL(EXTRACT(EPOCH FROM (_df - _ds))/3600) INTO _hours;

RETURN QUERY 

SELECT  
  s3billing.bucket_info.name AS bucket_name,
  (CASE 
    WHEN ( s3billing.placement.name = 'default-placement') THEN 'HOT'
    WHEN ( not s3billing.placement.name = 'default-placement' ) THEN s3billing.placement.name
    END
  ) AS placement
 ,coalesce(ceil(sum(usage_rgw_main_size_actual)/_days)::numeric, 0) AS size
 ,coalesce(s2.get, 0) AS get
 ,coalesce(s2.put, 0) AS put
 ,coalesce(s2.bytes_sent, 0) AS bytes_sent
FROM s3billing.bucket_stat
INNER JOIN s3billing.placement ON s3billing.bucket_stat.placement_id=s3billing.placement.id
INNER JOIN s3billing.bucket_info ON s3billing.bucket_stat.bucket_id=s3billing.bucket_info.id
LEFT JOIN (
 SELECT
   s1.bucket_id AS bucket_id
  ,s1.bucket_name AS bucket_name
  ,(CASE 
     WHEN ( s1.placement = 'default-placement') THEN 'HOT'
     WHEN ( not s1.placement = 'default-placement' ) THEN s1.placement
     END
   ) AS placement
  ,coalesce(sum(s1.get) ,0) AS get
  ,coalesce(sum(s1.put) ,0) AS put
  ,coalesce(sum(s1.bytes_sent), 0) AS bytes_sent
 FROM (
  SELECT
    s3billing.bucket_info.id AS bucket_id
   ,s3billing.bucket_info.name AS bucket_name
   ,(CASE 
      WHEN ( s3billing.placement.name = 'default-placement') THEN 'HOT'
      WHEN ( not s3billing.placement.name = 'default-placement' ) THEN s3billing.placement.name
      END
    ) AS placement
   ,(CASE
      WHEN (  s3billing.category.name LIKE 'get%'
           OR s3billing.category.name LIKE 'head%'
           OR s3billing.category.name LIKE 'options%'
           ) THEN coalesce(sum(ops), 0)
      END
    ) AS get
   ,(CASE
      WHEN (  s3billing.category.name LIKE 'put%'
           OR s3billing.category.name LIKE 'post%'
           OR s3billing.category.name LIKE 'patch%'
           OR s3billing.category.name LIKE 'list%'
           ) THEN coalesce(sum(ops), 0)
      END
    ) AS put
   ,coalesce(sum(usage_bucket_by_user.bytes_sent), 0) AS bytes_sent

  FROM
   s3billing.usage_bucket_by_user
  INNER JOIN s3billing.bucket_info ON s3billing.usage_bucket_by_user.bucketid    = s3billing.bucket_info.id
  INNER JOIN s3billing.placement   ON s3billing.bucket_info.placement_id         = s3billing.placement.id
  INNER JOIN s3billing.user_info   ON s3billing.usage_bucket_by_user.user_id     = s3billing.user_info.id
  INNER JOIN s3billing.category    ON s3billing.usage_bucket_by_user.category_id = s3billing.category.id

  WHERE
       s3billing.user_info.name=_user
   AND s3billing.usage_bucket_by_user.time >= _ds
   AND s3billing.usage_bucket_by_user.time <  _df
  GROUP BY
   s3billing.bucket_info.name
  ,s3billing.placement.name
  ,s3billing.category.name
  ,s3billing.bucket_info.id
 ) AS s1

 GROUP BY
   s1.bucket_name
  ,s1.bucket_id
  ,s1.placement

 ORDER BY
   s1.placement
  ,s1.bucket_name

) AS s2 ON s2.bucket_id  = s3billing.bucket_stat.bucket_id
WHERE owner=_user
  AND timestamp_addition >= _ds
  AND timestamp_addition <  _df
GROUP BY
  s3billing.bucket_info.name
 ,s3billing.placement.name
 ,s3billing.bucket_stat.bucket_id
 ,s2.get
 ,s2.put
 ,s2.bytes_sent
ORDER BY
 s3billing.placement.name
,s3billing.bucket_info.name
;
END;
$$;


ALTER FUNCTION s3billing.billing_per_user_fix_pl_sv(ds character varying, df character varying, _user character varying) OWNER TO postgres;

--
-- TOC entry 262 (class 1255 OID 2436167)
-- Name: billing_per_user_summary(character varying, character varying, character varying); Type: FUNCTION; Schema: s3billing; Owner: dsorokin
--

CREATE FUNCTION s3billing.billing_per_user_summary(_ds character varying, _df character varying, _user character varying) RETURNS TABLE(placement character varying, size_gb numeric, get_10k numeric, put_10k numeric, bytes_sent_gb numeric)
    LANGUAGE plpgsql
    AS $$
BEGIN
RETURN QUERY 
(
select 
	billing_per_user_fix_pl.placement as placement,
    (CASE
     WHEN (  billing_per_user_fix_pl.placement = 'HOT'
          ) THEN greatest(ceil(sum(size)/1024/1024/1024)-1,0)
     WHEN (  not billing_per_user_fix_pl.placement = 'HOT'
          ) THEN ceil(sum(size)/1024/1024/1024)
    END) AS size_GB,
    (CASE
     WHEN (  billing_per_user_fix_pl.placement = 'HOT'
          ) THEN greatest(ceil(sum(get)/10000)-10,0)
     WHEN (  not billing_per_user_fix_pl.placement = 'HOT'
          ) THEN ceil(sum(get)/10000)
    END) AS get_10k,
    (CASE
     WHEN (  billing_per_user_fix_pl.placement = 'HOT'
          ) THEN greatest(ceil(sum(put)/10000)-1,0)
     WHEN (  not billing_per_user_fix_pl.placement = 'HOT'
          ) THEN ceil(sum(put)/10000)
    END) AS put_10k,
    (CASE
     WHEN (  billing_per_user_fix_pl.placement = 'HOT'
          ) then greatest(ceil(sum(bytes_sent)/1024/1024/1024)-100,0)
     WHEN (  not billing_per_user_fix_pl.placement = 'HOT'
          ) THEN ceil(sum(bytes_sent)/1024/1024/1024)
    END) AS bytes_sent_GB
    from s3billing.billing_per_user_fix_pl(_ds,_df,_user)
  group by billing_per_user_fix_pl.placement
UNION all
select 
	'HOT_FREE_LIMIT' as placement,
	'1' AS size_GB,
	'10' AS get_10k,
	'1' AS put_10k,
	'100' AS bytes_sent_GB
	);
END;
$$;


ALTER FUNCTION s3billing.billing_per_user_summary(_ds character varying, _df character varying, _user character varying) OWNER TO dsorokin;

--
-- TOC entry 259 (class 1255 OID 2426971)
-- Name: billing_varchar(character varying, character varying, character varying); Type: FUNCTION; Schema: s3billing; Owner: dsorokin
--

CREATE FUNCTION s3billing.billing_varchar(_ds character varying, _df character varying, _user character varying) RETURNS TABLE(bucket_name character varying, placement character varying, size numeric, get numeric, put numeric, bytes_sent numeric)
    LANGUAGE plpgsql
    AS $$
BEGIN
RETURN QUERY 

SELECT  
  s3billing.bucket_info.name AS bucket_name
 ,s3billing.placement.name AS placement
 ,coalesce(round(sum(usage_rgw_main_size_actual)/
 (SELECT (DATE_PART('day', 
        ((TO_TIMESTAMP(_df, 'YYYY-MM-DD HH24:MI:SS'))
         -TO_TIMESTAMP(_ds, 'YYYY-MM-DD HH24:MI:SS'))
       )*24+
       DATE_PART('hour', 
        ((TO_TIMESTAMP(_df, 'YYYY-MM-DD HH24:MI:SS'))
         -TO_TIMESTAMP(_ds, 'YYYY-MM-DD HH24:MI:SS'))
       )))
 
 )::numeric,0) AS size
 ,coalesce(s2.get, 0) AS get
 ,coalesce(s2.put, 0) AS put
 ,coalesce(s2.bytes_sent, 0) as bytes_sent
FROM s3billing.bucket_stat
INNER JOIN s3billing.placement ON s3billing.bucket_stat.placement_id=s3billing.placement.id
INNER JOIN s3billing.bucket_info ON s3billing.bucket_stat.bucket_id=s3billing.bucket_info.id
LEFT JOIN (
 SELECT
   s1.bucket_id AS bucket_id
  ,s1.bucket_name AS bucket_name
  ,s1.placement AS placement
  ,coalesce(sum(s1.get) ,0) AS get
  ,coalesce(sum(s1.put) ,0) AS put
  ,coalesce(sum(s1.bytes_sent), 0) AS bytes_sent
 FROM (
  SELECT
    s3billing.bucket_info.id AS bucket_id
   ,s3billing.bucket_info.name AS bucket_name
   ,s3billing.placement.name AS placement
   ,(CASE
     WHEN (  s3billing.category.name LIKE 'get%'
          OR s3billing.category.name LIKE 'head%'
          OR s3billing.category.name LIKE 'options%'
          ) THEN coalesce(sum(ops), 0)
     END) AS get
   ,(CASE
     WHEN (  s3billing.category.name LIKE 'put%'
          OR s3billing.category.name LIKE 'post%'
          OR s3billing.category.name LIKE 'patch%'
          OR s3billing.category.name LIKE 'list%'
          ) THEN coalesce(sum(ops), 0)
     END) AS put
   ,coalesce(sum(usage_bucket_by_user.bytes_sent), 0) AS bytes_sent

  FROM
   s3billing.usage_bucket_by_user
  INNER JOIN s3billing.bucket_info ON s3billing.usage_bucket_by_user.bucketid    = s3billing.bucket_info.id
  INNER JOIN s3billing.placement   ON s3billing.bucket_info.placement_id         = s3billing.placement.id
  INNER JOIN s3billing.user_info   ON s3billing.usage_bucket_by_user.user_id     = s3billing.user_info.id
  INNER JOIN s3billing.category    ON s3billing.usage_bucket_by_user.category_id = s3billing.category.id

  WHERE
       s3billing.user_info.name=_user
   AND s3billing.usage_bucket_by_user.time >=TO_TIMESTAMP(_ds, 'YYYY-MM-DD HH24:MI:SS')
   AND s3billing.usage_bucket_by_user.time < TO_TIMESTAMP(_df, 'YYYY-MM-DD HH24:MI:SS')
  GROUP BY
   s3billing.bucket_info.name
  ,s3billing.placement.name
  ,s3billing.category.name
  ,s3billing.bucket_info.id
 ) AS s1

 GROUP BY
   s1.bucket_name
  ,s1.bucket_id
  ,s1.placement

 ORDER BY
   s1.placement
  ,s1.bucket_name

) AS s2 ON s2.bucket_id  = s3billing.bucket_stat.bucket_id
WHERE owner=_user
  AND timestamp_addition >= TO_TIMESTAMP(_ds, 'YYYY-MM-DD HH24:MI:SS')
  AND timestamp_addition <  TO_TIMESTAMP(_df, 'YYYY-MM-DD HH24:MI:SS')
GROUP BY
  s3billing.bucket_info.name
 ,s3billing.placement.name
 ,s3billing.bucket_stat.bucket_id
 ,s2.get
 ,s2.put
 ,s2.bytes_sent
ORDER BY
 s3billing.placement.name
,s3billing.bucket_info.name
;
END;
$$;


ALTER FUNCTION s3billing.billing_varchar(_ds character varying, _df character varying, _user character varying) OWNER TO dsorokin;

--
-- TOC entry 258 (class 1255 OID 5083308)
-- Name: buckets_current_stat(character varying); Type: FUNCTION; Schema: s3billing; Owner: dsorokin
--

CREATE FUNCTION s3billing.buckets_current_stat(_user character varying) RETURNS TABLE(name character varying, placement character varying, bucket_quota_enabled boolean, bucket_quota_max_size bigint, bucket_quota_max_objects bigint, usage_rgw_main_size_actual bigint, usage_rgw_main_num_objects bigint)
    LANGUAGE plpgsql
    AS $$
BEGIN
RETURN QUERY 
select 
	s3billing.bucket_info.name
 	,(case 
	when (  s3billing.placement.name = 'default-placement') then 'HOT'
	WHEN (  not s3billing.placement.name = 'default-placement' ) THEN s3billing.placement.name
 	end
 	) as placement
	,s3billing.bucket_stat.bucket_quota_enabled
	,s3billing.bucket_stat.bucket_quota_max_size
	,s3billing.bucket_stat.bucket_quota_max_objects 
	,s3billing.bucket_stat.usage_rgw_main_size_actual 
	,s3billing.bucket_stat.usage_rgw_main_num_objects 
	from s3billing.bucket_stat 
	inner join s3billing.bucket_info on s3billing.bucket_stat.bucket_id= s3billing.bucket_info.id
	inner join s3billing.placement on s3billing.bucket_info.placement_id = s3billing.placement.id
	where owner=_user
	and s3billing.bucket_stat.timestamp_addition=(
		select max(s3billing.bucket_stat.timestamp_addition) from s3billing.bucket_stat
		)
	order by s3billing.bucket_info.name, s3billing.bucket_stat.timestamp_addition 
;
END;
$$;


ALTER FUNCTION s3billing.buckets_current_stat(_user character varying) OWNER TO dsorokin;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- TOC entry 222 (class 1259 OID 1893983)
-- Name: capacity_resource; Type: TABLE; Schema: ngcloud_ru; Owner: vc_billing
--

CREATE TABLE ngcloud_ru.capacity_resource (
    vdc_id uuid NOT NULL,
    "timestamp" timestamp with time zone NOT NULL,
    cpu_allocated integer NOT NULL,
    cpu_limit integer NOT NULL,
    cpu_reserved integer NOT NULL,
    cpu_used integer NOT NULL,
    cpu_speed integer NOT NULL,
    mem_allocated integer NOT NULL,
    mem_limit integer NOT NULL,
    mem_reserved integer NOT NULL,
    mem_used integer NOT NULL
);


ALTER TABLE ngcloud_ru.capacity_resource OWNER TO vc_billing;

--
-- TOC entry 223 (class 1259 OID 1893993)
-- Name: capacity_storage; Type: TABLE; Schema: ngcloud_ru; Owner: vc_billing
--

CREATE TABLE ngcloud_ru.capacity_storage (
    vdc_id uuid NOT NULL,
    storage_profile_types_id integer NOT NULL,
    "timestamp" timestamp with time zone NOT NULL,
    id uuid NOT NULL,
    "limit" integer NOT NULL,
    used integer NOT NULL
);


ALTER TABLE ngcloud_ru.capacity_storage OWNER TO vc_billing;

--
-- TOC entry 220 (class 1259 OID 1893965)
-- Name: storage_profile_types; Type: TABLE; Schema: ngcloud_ru; Owner: vc_billing
--

CREATE TABLE ngcloud_ru.storage_profile_types (
    id integer NOT NULL,
    name character varying(255) NOT NULL
);


ALTER TABLE ngcloud_ru.storage_profile_types OWNER TO vc_billing;

--
-- TOC entry 219 (class 1259 OID 1893964)
-- Name: storage_profile_types_id_seq; Type: SEQUENCE; Schema: ngcloud_ru; Owner: vc_billing
--

CREATE SEQUENCE ngcloud_ru.storage_profile_types_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER SEQUENCE ngcloud_ru.storage_profile_types_id_seq OWNER TO vc_billing;

--
-- TOC entry 3486 (class 0 OID 0)
-- Dependencies: 219
-- Name: storage_profile_types_id_seq; Type: SEQUENCE OWNED BY; Schema: ngcloud_ru; Owner: vc_billing
--

ALTER SEQUENCE ngcloud_ru.storage_profile_types_id_seq OWNED BY ngcloud_ru.storage_profile_types.id;


--
-- TOC entry 218 (class 1259 OID 1893959)
-- Name: tenant; Type: TABLE; Schema: ngcloud_ru; Owner: vc_billing
--

CREATE TABLE ngcloud_ru.tenant (
    id uuid NOT NULL,
    name character varying(255) NOT NULL,
    wzcode bigint
);


ALTER TABLE ngcloud_ru.tenant OWNER TO vc_billing;

--
-- TOC entry 221 (class 1259 OID 1893973)
-- Name: vdc; Type: TABLE; Schema: ngcloud_ru; Owner: vc_billing
--

CREATE TABLE ngcloud_ru.vdc (
    id uuid NOT NULL,
    tenant_id uuid NOT NULL,
    name character varying(255) NOT NULL
);


ALTER TABLE ngcloud_ru.vdc OWNER TO vc_billing;

--
-- TOC entry 230 (class 1259 OID 2160668)
-- Name: bucket_info; Type: TABLE; Schema: s3billing; Owner: s3billing
--

CREATE TABLE s3billing.bucket_info (
    id integer NOT NULL,
    bucket_id character varying(255) NOT NULL,
    name character varying(255) NOT NULL,
    marker character varying(255) NOT NULL,
    placement_id bigint DEFAULT 0 NOT NULL
);


ALTER TABLE s3billing.bucket_info OWNER TO s3billing;

--
-- TOC entry 229 (class 1259 OID 2160667)
-- Name: bucket_info_id_seq; Type: SEQUENCE; Schema: s3billing; Owner: s3billing
--

CREATE SEQUENCE s3billing.bucket_info_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER SEQUENCE s3billing.bucket_info_id_seq OWNER TO s3billing;

--
-- TOC entry 3490 (class 0 OID 0)
-- Dependencies: 229
-- Name: bucket_info_id_seq; Type: SEQUENCE OWNED BY; Schema: s3billing; Owner: s3billing
--

ALTER SEQUENCE s3billing.bucket_info_id_seq OWNED BY s3billing.bucket_info.id;


--
-- TOC entry 226 (class 1259 OID 2160642)
-- Name: bucket_stat; Type: TABLE; Schema: s3billing; Owner: s3billing
--

CREATE TABLE s3billing.bucket_stat (
    bucket_id bigint,
    num_shards bigint NOT NULL,
    tenant character varying(255) DEFAULT NULL::character varying,
    zone_group bigint NOT NULL,
    placement_id bigint NOT NULL,
    explicit_placement_data_pool character varying(255) DEFAULT NULL::character varying,
    explicit_placement_data_extra_pool character varying(255) DEFAULT NULL::character varying,
    explicit_placement_index_pool character varying(255) DEFAULT NULL::character varying,
    index_type_id bigint NOT NULL,
    versioned boolean DEFAULT false,
    versioning_enabled boolean DEFAULT false,
    object_lock_enabled boolean DEFAULT false,
    mfa_enabled boolean DEFAULT false,
    owner character varying(255) NOT NULL,
    bucket_quota_enabled boolean DEFAULT false,
    bucket_quota_check_on_raw boolean DEFAULT false,
    bucket_quota_max_size bigint NOT NULL,
    bucket_quota_max_size_kb bigint NOT NULL,
    bucket_quota_max_objects bigint NOT NULL,
    usage_rgw_main_size bigint NOT NULL,
    usage_rgw_main_size_actual bigint NOT NULL,
    usage_rgw_main_size_utilized bigint NOT NULL,
    usage_rgw_main_size_kb bigint NOT NULL,
    usage_rgw_main_size_kb_actual bigint NOT NULL,
    usage_rgw_main_size_kb_utilized bigint NOT NULL,
    usage_rgw_main_num_objects bigint NOT NULL,
    usage_rgw_multimeta_size bigint,
    usage_rgw_multimeta_size_actual bigint,
    usage_rgw_multimeta_size_utilized bigint,
    usage_rgw_multimeta_size_kb bigint,
    usage_rgw_multimeta_size_kb_actual bigint,
    usage_rgw_multimeta_size_kb_utilized bigint,
    usage_rgw_multimeta_num_objects bigint,
    creation_time timestamp without time zone NOT NULL,
    mtime timestamp without time zone NOT NULL,
    timestamp_addition timestamp without time zone DEFAULT date_trunc('hour'::text, CURRENT_TIMESTAMP)
);


ALTER TABLE s3billing.bucket_stat OWNER TO s3billing;

--
-- TOC entry 245 (class 1259 OID 2160763)
-- Name: category; Type: TABLE; Schema: s3billing; Owner: s3billing
--

CREATE TABLE s3billing.category (
    id integer NOT NULL,
    name character varying(255) NOT NULL
);


ALTER TABLE s3billing.category OWNER TO s3billing;

--
-- TOC entry 244 (class 1259 OID 2160762)
-- Name: category_id_seq; Type: SEQUENCE; Schema: s3billing; Owner: s3billing
--

CREATE SEQUENCE s3billing.category_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER SEQUENCE s3billing.category_id_seq OWNER TO s3billing;

--
-- TOC entry 3493 (class 0 OID 0)
-- Dependencies: 244
-- Name: category_id_seq; Type: SEQUENCE OWNED BY; Schema: s3billing; Owner: s3billing
--

ALTER SEQUENCE s3billing.category_id_seq OWNED BY s3billing.category.id;


--
-- TOC entry 225 (class 1259 OID 2160635)
-- Name: goose_db_version; Type: TABLE; Schema: s3billing; Owner: s3billing
--

CREATE TABLE s3billing.goose_db_version (
    id integer NOT NULL,
    version_id bigint NOT NULL,
    is_applied boolean NOT NULL,
    tstamp timestamp without time zone DEFAULT now() NOT NULL
);


ALTER TABLE s3billing.goose_db_version OWNER TO s3billing;

--
-- TOC entry 224 (class 1259 OID 2160634)
-- Name: goose_db_version_id_seq; Type: SEQUENCE; Schema: s3billing; Owner: s3billing
--

ALTER TABLE s3billing.goose_db_version ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (
    SEQUENCE NAME s3billing.goose_db_version_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);


--
-- TOC entry 234 (class 1259 OID 2160684)
-- Name: index_type; Type: TABLE; Schema: s3billing; Owner: s3billing
--

CREATE TABLE s3billing.index_type (
    id integer NOT NULL,
    name character varying(255) NOT NULL
);


ALTER TABLE s3billing.index_type OWNER TO s3billing;

--
-- TOC entry 233 (class 1259 OID 2160683)
-- Name: index_type_id_seq; Type: SEQUENCE; Schema: s3billing; Owner: s3billing
--

CREATE SEQUENCE s3billing.index_type_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER SEQUENCE s3billing.index_type_id_seq OWNER TO s3billing;

--
-- TOC entry 3496 (class 0 OID 0)
-- Dependencies: 233
-- Name: index_type_id_seq; Type: SEQUENCE OWNED BY; Schema: s3billing; Owner: s3billing
--

ALTER SEQUENCE s3billing.index_type_id_seq OWNED BY s3billing.index_type.id;


--
-- TOC entry 232 (class 1259 OID 2160677)
-- Name: placement; Type: TABLE; Schema: s3billing; Owner: s3billing
--

CREATE TABLE s3billing.placement (
    id integer NOT NULL,
    name character varying(255) NOT NULL
);


ALTER TABLE s3billing.placement OWNER TO s3billing;

--
-- TOC entry 231 (class 1259 OID 2160676)
-- Name: placement_id_seq; Type: SEQUENCE; Schema: s3billing; Owner: s3billing
--

CREATE SEQUENCE s3billing.placement_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER SEQUENCE s3billing.placement_id_seq OWNER TO s3billing;

--
-- TOC entry 3498 (class 0 OID 0)
-- Dependencies: 231
-- Name: placement_id_seq; Type: SEQUENCE OWNED BY; Schema: s3billing; Owner: s3billing
--

ALTER SEQUENCE s3billing.placement_id_seq OWNED BY s3billing.placement.id;


--
-- TOC entry 239 (class 1259 OID 2160705)
-- Name: usage_bucket_by_user; Type: TABLE; Schema: s3billing; Owner: s3billing
--

CREATE TABLE s3billing.usage_bucket_by_user (
    bucketid bigint,
    "time" timestamp without time zone NOT NULL,
    bytes_sent bigint NOT NULL,
    bytes_received bigint NOT NULL,
    ops bigint NOT NULL,
    successful_ops bigint NOT NULL,
    date_start timestamp without time zone NOT NULL,
    date_end timestamp without time zone NOT NULL,
    user_id bigint NOT NULL,
    category_id bigint NOT NULL
);


ALTER TABLE s3billing.usage_bucket_by_user OWNER TO s3billing;

--
-- TOC entry 238 (class 1259 OID 2160698)
-- Name: usage_stat_summary; Type: TABLE; Schema: s3billing; Owner: s3billing
--

CREATE TABLE s3billing.usage_stat_summary (
    id integer NOT NULL,
    userid character varying(255) NOT NULL,
    category character varying(255) NOT NULL,
    bytes_sent bigint NOT NULL,
    bytes_received bigint NOT NULL,
    ops bigint NOT NULL,
    successful_ops bigint NOT NULL,
    date_start timestamp without time zone NOT NULL,
    date_end timestamp without time zone NOT NULL
);


ALTER TABLE s3billing.usage_stat_summary OWNER TO s3billing;

--
-- TOC entry 237 (class 1259 OID 2160697)
-- Name: usage_stat_summary_id_seq; Type: SEQUENCE; Schema: s3billing; Owner: s3billing
--

CREATE SEQUENCE s3billing.usage_stat_summary_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER SEQUENCE s3billing.usage_stat_summary_id_seq OWNER TO s3billing;

--
-- TOC entry 3501 (class 0 OID 0)
-- Dependencies: 237
-- Name: usage_stat_summary_id_seq; Type: SEQUENCE OWNED BY; Schema: s3billing; Owner: s3billing
--

ALTER SEQUENCE s3billing.usage_stat_summary_id_seq OWNED BY s3billing.usage_stat_summary.id;


--
-- TOC entry 236 (class 1259 OID 2160691)
-- Name: usage_stat_total; Type: TABLE; Schema: s3billing; Owner: s3billing
--

CREATE TABLE s3billing.usage_stat_total (
    id integer NOT NULL,
    userid character varying(255) NOT NULL,
    bytes_sent bigint NOT NULL,
    bytes_received bigint NOT NULL,
    ops bigint NOT NULL,
    successful_ops bigint NOT NULL,
    date_start timestamp without time zone NOT NULL,
    date_end timestamp without time zone NOT NULL
);


ALTER TABLE s3billing.usage_stat_total OWNER TO s3billing;

--
-- TOC entry 235 (class 1259 OID 2160690)
-- Name: usage_stat_total_id_seq; Type: SEQUENCE; Schema: s3billing; Owner: s3billing
--

CREATE SEQUENCE s3billing.usage_stat_total_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER SEQUENCE s3billing.usage_stat_total_id_seq OWNER TO s3billing;

--
-- TOC entry 3503 (class 0 OID 0)
-- Dependencies: 235
-- Name: usage_stat_total_id_seq; Type: SEQUENCE OWNED BY; Schema: s3billing; Owner: s3billing
--

ALTER SEQUENCE s3billing.usage_stat_total_id_seq OWNED BY s3billing.usage_stat_total.id;


--
-- TOC entry 243 (class 1259 OID 2160751)
-- Name: user_info; Type: TABLE; Schema: s3billing; Owner: s3billing
--

CREATE TABLE s3billing.user_info (
    id integer NOT NULL,
    name character varying(255) NOT NULL,
    owner character varying(255) NOT NULL
);


ALTER TABLE s3billing.user_info OWNER TO s3billing;

--
-- TOC entry 242 (class 1259 OID 2160750)
-- Name: user_info_id_seq; Type: SEQUENCE; Schema: s3billing; Owner: s3billing
--

CREATE SEQUENCE s3billing.user_info_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER SEQUENCE s3billing.user_info_id_seq OWNER TO s3billing;

--
-- TOC entry 3505 (class 0 OID 0)
-- Dependencies: 242
-- Name: user_info_id_seq; Type: SEQUENCE OWNED BY; Schema: s3billing; Owner: s3billing
--

ALTER SEQUENCE s3billing.user_info_id_seq OWNED BY s3billing.user_info.id;


--
-- TOC entry 241 (class 1259 OID 2160712)
-- Name: user_quota_bucket; Type: TABLE; Schema: s3billing; Owner: s3billing
--

CREATE TABLE s3billing.user_quota_bucket (
    id integer NOT NULL,
    userid character varying(255) NOT NULL,
    enabled boolean DEFAULT false,
    check_on_raw boolean DEFAULT false,
    max_size bigint NOT NULL,
    max_size_kb bigint NOT NULL,
    max_objects bigint NOT NULL
);


ALTER TABLE s3billing.user_quota_bucket OWNER TO s3billing;

--
-- TOC entry 240 (class 1259 OID 2160711)
-- Name: user_quota_bucket_id_seq; Type: SEQUENCE; Schema: s3billing; Owner: s3billing
--

CREATE SEQUENCE s3billing.user_quota_bucket_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER SEQUENCE s3billing.user_quota_bucket_id_seq OWNER TO s3billing;

--
-- TOC entry 3507 (class 0 OID 0)
-- Dependencies: 240
-- Name: user_quota_bucket_id_seq; Type: SEQUENCE OWNED BY; Schema: s3billing; Owner: s3billing
--

ALTER SEQUENCE s3billing.user_quota_bucket_id_seq OWNED BY s3billing.user_quota_bucket.id;


--
-- TOC entry 228 (class 1259 OID 2160661)
-- Name: zone_group; Type: TABLE; Schema: s3billing; Owner: s3billing
--

CREATE TABLE s3billing.zone_group (
    id integer NOT NULL,
    name uuid NOT NULL
);


ALTER TABLE s3billing.zone_group OWNER TO s3billing;

--
-- TOC entry 227 (class 1259 OID 2160660)
-- Name: zone_group_id_seq; Type: SEQUENCE; Schema: s3billing; Owner: s3billing
--

CREATE SEQUENCE s3billing.zone_group_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER SEQUENCE s3billing.zone_group_id_seq OWNER TO s3billing;

--
-- TOC entry 3509 (class 0 OID 0)
-- Dependencies: 227
-- Name: zone_group_id_seq; Type: SEQUENCE OWNED BY; Schema: s3billing; Owner: s3billing
--

ALTER SEQUENCE s3billing.zone_group_id_seq OWNED BY s3billing.zone_group.id;


--
-- TOC entry 3262 (class 2604 OID 1893968)
-- Name: storage_profile_types id; Type: DEFAULT; Schema: ngcloud_ru; Owner: vc_billing
--

ALTER TABLE ONLY ngcloud_ru.storage_profile_types ALTER COLUMN id SET DEFAULT nextval('ngcloud_ru.storage_profile_types_id_seq'::regclass);


--
-- TOC entry 3276 (class 2604 OID 2160671)
-- Name: bucket_info id; Type: DEFAULT; Schema: s3billing; Owner: s3billing
--

ALTER TABLE ONLY s3billing.bucket_info ALTER COLUMN id SET DEFAULT nextval('s3billing.bucket_info_id_seq'::regclass);


--
-- TOC entry 3286 (class 2604 OID 2160766)
-- Name: category id; Type: DEFAULT; Schema: s3billing; Owner: s3billing
--

ALTER TABLE ONLY s3billing.category ALTER COLUMN id SET DEFAULT nextval('s3billing.category_id_seq'::regclass);


--
-- TOC entry 3279 (class 2604 OID 2160687)
-- Name: index_type id; Type: DEFAULT; Schema: s3billing; Owner: s3billing
--

ALTER TABLE ONLY s3billing.index_type ALTER COLUMN id SET DEFAULT nextval('s3billing.index_type_id_seq'::regclass);


--
-- TOC entry 3278 (class 2604 OID 2160680)
-- Name: placement id; Type: DEFAULT; Schema: s3billing; Owner: s3billing
--

ALTER TABLE ONLY s3billing.placement ALTER COLUMN id SET DEFAULT nextval('s3billing.placement_id_seq'::regclass);


--
-- TOC entry 3281 (class 2604 OID 2160701)
-- Name: usage_stat_summary id; Type: DEFAULT; Schema: s3billing; Owner: s3billing
--

ALTER TABLE ONLY s3billing.usage_stat_summary ALTER COLUMN id SET DEFAULT nextval('s3billing.usage_stat_summary_id_seq'::regclass);


--
-- TOC entry 3280 (class 2604 OID 2160694)
-- Name: usage_stat_total id; Type: DEFAULT; Schema: s3billing; Owner: s3billing
--

ALTER TABLE ONLY s3billing.usage_stat_total ALTER COLUMN id SET DEFAULT nextval('s3billing.usage_stat_total_id_seq'::regclass);


--
-- TOC entry 3285 (class 2604 OID 2160754)
-- Name: user_info id; Type: DEFAULT; Schema: s3billing; Owner: s3billing
--

ALTER TABLE ONLY s3billing.user_info ALTER COLUMN id SET DEFAULT nextval('s3billing.user_info_id_seq'::regclass);


--
-- TOC entry 3282 (class 2604 OID 2160715)
-- Name: user_quota_bucket id; Type: DEFAULT; Schema: s3billing; Owner: s3billing
--

ALTER TABLE ONLY s3billing.user_quota_bucket ALTER COLUMN id SET DEFAULT nextval('s3billing.user_quota_bucket_id_seq'::regclass);


--
-- TOC entry 3275 (class 2604 OID 2160664)
-- Name: zone_group id; Type: DEFAULT; Schema: s3billing; Owner: s3billing
--

ALTER TABLE ONLY s3billing.zone_group ALTER COLUMN id SET DEFAULT nextval('s3billing.zone_group_id_seq'::regclass);


--
-- TOC entry 3296 (class 2606 OID 1893987)
-- Name: capacity_resource capacity_resource_pkey; Type: CONSTRAINT; Schema: ngcloud_ru; Owner: vc_billing
--

ALTER TABLE ONLY ngcloud_ru.capacity_resource
    ADD CONSTRAINT capacity_resource_pkey PRIMARY KEY (vdc_id, "timestamp");


--
-- TOC entry 3298 (class 2606 OID 1893997)
-- Name: capacity_storage capacity_storage_pkey; Type: CONSTRAINT; Schema: ngcloud_ru; Owner: vc_billing
--

ALTER TABLE ONLY ngcloud_ru.capacity_storage
    ADD CONSTRAINT capacity_storage_pkey PRIMARY KEY (vdc_id, id, "timestamp");


--
-- TOC entry 3290 (class 2606 OID 1893972)
-- Name: storage_profile_types storage_profile_types_name_key; Type: CONSTRAINT; Schema: ngcloud_ru; Owner: vc_billing
--

ALTER TABLE ONLY ngcloud_ru.storage_profile_types
    ADD CONSTRAINT storage_profile_types_name_key UNIQUE (name);


--
-- TOC entry 3292 (class 2606 OID 1893970)
-- Name: storage_profile_types storage_profile_types_pkey; Type: CONSTRAINT; Schema: ngcloud_ru; Owner: vc_billing
--

ALTER TABLE ONLY ngcloud_ru.storage_profile_types
    ADD CONSTRAINT storage_profile_types_pkey PRIMARY KEY (id);


--
-- TOC entry 3288 (class 2606 OID 1893963)
-- Name: tenant tenant_pkey; Type: CONSTRAINT; Schema: ngcloud_ru; Owner: vc_billing
--

ALTER TABLE ONLY ngcloud_ru.tenant
    ADD CONSTRAINT tenant_pkey PRIMARY KEY (id);


--
-- TOC entry 3294 (class 2606 OID 1893977)
-- Name: vdc vdc_pkey; Type: CONSTRAINT; Schema: ngcloud_ru; Owner: vc_billing
--

ALTER TABLE ONLY ngcloud_ru.vdc
    ADD CONSTRAINT vdc_pkey PRIMARY KEY (id);


--
-- TOC entry 3308 (class 2606 OID 2160675)
-- Name: bucket_info bucket_info_pkey; Type: CONSTRAINT; Schema: s3billing; Owner: s3billing
--

ALTER TABLE ONLY s3billing.bucket_info
    ADD CONSTRAINT bucket_info_pkey PRIMARY KEY (id);


--
-- TOC entry 3303 (class 2606 OID 2476484)
-- Name: bucket_stat bucket_stat_unique; Type: CONSTRAINT; Schema: s3billing; Owner: s3billing
--

ALTER TABLE ONLY s3billing.bucket_stat
    ADD CONSTRAINT bucket_stat_unique UNIQUE (bucket_id, placement_id, timestamp_addition);


--
-- TOC entry 3328 (class 2606 OID 2160768)
-- Name: category category_pkey; Type: CONSTRAINT; Schema: s3billing; Owner: s3billing
--

ALTER TABLE ONLY s3billing.category
    ADD CONSTRAINT category_pkey PRIMARY KEY (id);


--
-- TOC entry 3300 (class 2606 OID 2160640)
-- Name: goose_db_version goose_db_version_pkey; Type: CONSTRAINT; Schema: s3billing; Owner: s3billing
--

ALTER TABLE ONLY s3billing.goose_db_version
    ADD CONSTRAINT goose_db_version_pkey PRIMARY KEY (id);


--
-- TOC entry 3314 (class 2606 OID 2160689)
-- Name: index_type index_type_pkey; Type: CONSTRAINT; Schema: s3billing; Owner: s3billing
--

ALTER TABLE ONLY s3billing.index_type
    ADD CONSTRAINT index_type_pkey PRIMARY KEY (id);


--
-- TOC entry 3312 (class 2606 OID 2160682)
-- Name: placement placement_pkey; Type: CONSTRAINT; Schema: s3billing; Owner: s3billing
--

ALTER TABLE ONLY s3billing.placement
    ADD CONSTRAINT placement_pkey PRIMARY KEY (id);


--
-- TOC entry 3320 (class 2606 OID 2476705)
-- Name: usage_bucket_by_user usage_bucket_by_user_unique; Type: CONSTRAINT; Schema: s3billing; Owner: s3billing
--

ALTER TABLE ONLY s3billing.usage_bucket_by_user
    ADD CONSTRAINT usage_bucket_by_user_unique UNIQUE (bucketid, date_start, date_end, user_id, category_id);


--
-- TOC entry 3318 (class 2606 OID 2160703)
-- Name: usage_stat_summary usage_stat_summary_pkey; Type: CONSTRAINT; Schema: s3billing; Owner: s3billing
--

ALTER TABLE ONLY s3billing.usage_stat_summary
    ADD CONSTRAINT usage_stat_summary_pkey PRIMARY KEY (id);


--
-- TOC entry 3316 (class 2606 OID 2160696)
-- Name: usage_stat_total usage_stat_total_pkey; Type: CONSTRAINT; Schema: s3billing; Owner: s3billing
--

ALTER TABLE ONLY s3billing.usage_stat_total
    ADD CONSTRAINT usage_stat_total_pkey PRIMARY KEY (id);


--
-- TOC entry 3325 (class 2606 OID 2160758)
-- Name: user_info user_info_pkey; Type: CONSTRAINT; Schema: s3billing; Owner: s3billing
--

ALTER TABLE ONLY s3billing.user_info
    ADD CONSTRAINT user_info_pkey PRIMARY KEY (id);


--
-- TOC entry 3322 (class 2606 OID 2160719)
-- Name: user_quota_bucket user_quota_bucket_pkey; Type: CONSTRAINT; Schema: s3billing; Owner: s3billing
--

ALTER TABLE ONLY s3billing.user_quota_bucket
    ADD CONSTRAINT user_quota_bucket_pkey PRIMARY KEY (id);


--
-- TOC entry 3305 (class 2606 OID 2160666)
-- Name: zone_group zone_group_pkey; Type: CONSTRAINT; Schema: s3billing; Owner: s3billing
--

ALTER TABLE ONLY s3billing.zone_group
    ADD CONSTRAINT zone_group_pkey PRIMARY KEY (id);


--
-- TOC entry 3306 (class 1259 OID 2474077)
-- Name: bucket_info_name_idx; Type: INDEX; Schema: s3billing; Owner: s3billing
--

CREATE INDEX bucket_info_name_idx ON s3billing.bucket_info USING btree (name);


--
-- TOC entry 3309 (class 1259 OID 2474078)
-- Name: bucket_info_placement_id_idx; Type: INDEX; Schema: s3billing; Owner: s3billing
--

CREATE INDEX bucket_info_placement_id_idx ON s3billing.bucket_info USING btree (placement_id);


--
-- TOC entry 3301 (class 1259 OID 2474094)
-- Name: bucket_stat_owner_idx; Type: INDEX; Schema: s3billing; Owner: s3billing
--

CREATE INDEX bucket_stat_owner_idx ON s3billing.bucket_stat USING btree (owner);


--
-- TOC entry 3326 (class 1259 OID 2474445)
-- Name: category_name_idx; Type: INDEX; Schema: s3billing; Owner: s3billing
--

CREATE UNIQUE INDEX category_name_idx ON s3billing.category USING btree (name);


--
-- TOC entry 3310 (class 1259 OID 2474468)
-- Name: placement_name_idx; Type: INDEX; Schema: s3billing; Owner: s3billing
--

CREATE UNIQUE INDEX placement_name_idx ON s3billing.placement USING btree (name);


--
-- TOC entry 3323 (class 1259 OID 2474438)
-- Name: user_info_name_idx; Type: INDEX; Schema: s3billing; Owner: s3billing
--

CREATE INDEX user_info_name_idx ON s3billing.user_info USING btree (name);


--
-- TOC entry 3330 (class 2606 OID 1893988)
-- Name: capacity_resource capacity_resource_vdc_id_fkey; Type: FK CONSTRAINT; Schema: ngcloud_ru; Owner: vc_billing
--

ALTER TABLE ONLY ngcloud_ru.capacity_resource
    ADD CONSTRAINT capacity_resource_vdc_id_fkey FOREIGN KEY (vdc_id) REFERENCES ngcloud_ru.vdc(id) ON DELETE CASCADE;


--
-- TOC entry 3331 (class 2606 OID 1894003)
-- Name: capacity_storage capacity_storage_storage_profile_types_id_fkey; Type: FK CONSTRAINT; Schema: ngcloud_ru; Owner: vc_billing
--

ALTER TABLE ONLY ngcloud_ru.capacity_storage
    ADD CONSTRAINT capacity_storage_storage_profile_types_id_fkey FOREIGN KEY (storage_profile_types_id) REFERENCES ngcloud_ru.storage_profile_types(id);


--
-- TOC entry 3332 (class 2606 OID 1893998)
-- Name: capacity_storage capacity_storage_vdc_id_fkey; Type: FK CONSTRAINT; Schema: ngcloud_ru; Owner: vc_billing
--

ALTER TABLE ONLY ngcloud_ru.capacity_storage
    ADD CONSTRAINT capacity_storage_vdc_id_fkey FOREIGN KEY (vdc_id) REFERENCES ngcloud_ru.vdc(id) ON DELETE CASCADE;


--
-- TOC entry 3329 (class 2606 OID 1893978)
-- Name: vdc vdc_tenant_id_fkey; Type: FK CONSTRAINT; Schema: ngcloud_ru; Owner: vc_billing
--

ALTER TABLE ONLY ngcloud_ru.vdc
    ADD CONSTRAINT vdc_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES ngcloud_ru.tenant(id);


--
-- TOC entry 3481 (class 0 OID 0)
-- Dependencies: 7
-- Name: SCHEMA ngcloud_ru; Type: ACL; Schema: -; Owner: vc_billing
--

GRANT USAGE ON SCHEMA ngcloud_ru TO grafanabilling;


--
-- TOC entry 3482 (class 0 OID 0)
-- Dependencies: 8
-- Name: SCHEMA s3billing; Type: ACL; Schema: -; Owner: s3billing
--

GRANT USAGE ON SCHEMA s3billing TO grafanabilling;


--
-- TOC entry 3483 (class 0 OID 0)
-- Dependencies: 222
-- Name: TABLE capacity_resource; Type: ACL; Schema: ngcloud_ru; Owner: vc_billing
--

GRANT SELECT ON TABLE ngcloud_ru.capacity_resource TO grafanabilling;


--
-- TOC entry 3484 (class 0 OID 0)
-- Dependencies: 223
-- Name: TABLE capacity_storage; Type: ACL; Schema: ngcloud_ru; Owner: vc_billing
--

GRANT SELECT ON TABLE ngcloud_ru.capacity_storage TO grafanabilling;


--
-- TOC entry 3485 (class 0 OID 0)
-- Dependencies: 220
-- Name: TABLE storage_profile_types; Type: ACL; Schema: ngcloud_ru; Owner: vc_billing
--

GRANT SELECT ON TABLE ngcloud_ru.storage_profile_types TO grafanabilling;


--
-- TOC entry 3487 (class 0 OID 0)
-- Dependencies: 218
-- Name: TABLE tenant; Type: ACL; Schema: ngcloud_ru; Owner: vc_billing
--

GRANT SELECT ON TABLE ngcloud_ru.tenant TO grafanabilling;


--
-- TOC entry 3488 (class 0 OID 0)
-- Dependencies: 221
-- Name: TABLE vdc; Type: ACL; Schema: ngcloud_ru; Owner: vc_billing
--

GRANT SELECT ON TABLE ngcloud_ru.vdc TO grafanabilling;


--
-- TOC entry 3489 (class 0 OID 0)
-- Dependencies: 230
-- Name: TABLE bucket_info; Type: ACL; Schema: s3billing; Owner: s3billing
--

GRANT SELECT ON TABLE s3billing.bucket_info TO grafanabilling;


--
-- TOC entry 3491 (class 0 OID 0)
-- Dependencies: 226
-- Name: TABLE bucket_stat; Type: ACL; Schema: s3billing; Owner: s3billing
--

GRANT SELECT ON TABLE s3billing.bucket_stat TO grafanabilling;


--
-- TOC entry 3492 (class 0 OID 0)
-- Dependencies: 245
-- Name: TABLE category; Type: ACL; Schema: s3billing; Owner: s3billing
--

GRANT SELECT ON TABLE s3billing.category TO grafanabilling;


--
-- TOC entry 3494 (class 0 OID 0)
-- Dependencies: 225
-- Name: TABLE goose_db_version; Type: ACL; Schema: s3billing; Owner: s3billing
--

GRANT SELECT ON TABLE s3billing.goose_db_version TO grafanabilling;


--
-- TOC entry 3495 (class 0 OID 0)
-- Dependencies: 234
-- Name: TABLE index_type; Type: ACL; Schema: s3billing; Owner: s3billing
--

GRANT SELECT ON TABLE s3billing.index_type TO grafanabilling;


--
-- TOC entry 3497 (class 0 OID 0)
-- Dependencies: 232
-- Name: TABLE placement; Type: ACL; Schema: s3billing; Owner: s3billing
--

GRANT SELECT ON TABLE s3billing.placement TO grafanabilling;


--
-- TOC entry 3499 (class 0 OID 0)
-- Dependencies: 239
-- Name: TABLE usage_bucket_by_user; Type: ACL; Schema: s3billing; Owner: s3billing
--

GRANT SELECT ON TABLE s3billing.usage_bucket_by_user TO grafanabilling;


--
-- TOC entry 3500 (class 0 OID 0)
-- Dependencies: 238
-- Name: TABLE usage_stat_summary; Type: ACL; Schema: s3billing; Owner: s3billing
--

GRANT SELECT ON TABLE s3billing.usage_stat_summary TO grafanabilling;


--
-- TOC entry 3502 (class 0 OID 0)
-- Dependencies: 236
-- Name: TABLE usage_stat_total; Type: ACL; Schema: s3billing; Owner: s3billing
--

GRANT SELECT ON TABLE s3billing.usage_stat_total TO grafanabilling;


--
-- TOC entry 3504 (class 0 OID 0)
-- Dependencies: 243
-- Name: TABLE user_info; Type: ACL; Schema: s3billing; Owner: s3billing
--

GRANT SELECT ON TABLE s3billing.user_info TO grafanabilling;


--
-- TOC entry 3506 (class 0 OID 0)
-- Dependencies: 241
-- Name: TABLE user_quota_bucket; Type: ACL; Schema: s3billing; Owner: s3billing
--

GRANT SELECT ON TABLE s3billing.user_quota_bucket TO grafanabilling;


--
-- TOC entry 3508 (class 0 OID 0)
-- Dependencies: 228
-- Name: TABLE zone_group; Type: ACL; Schema: s3billing; Owner: s3billing
--

GRANT SELECT ON TABLE s3billing.zone_group TO grafanabilling;


-- Completed on 2025-10-24 14:30:47

--
-- PostgreSQL database dump complete
--

\unrestrict M18FnaokuZFKQZjWu9az6dzOO6o2cSlpJgAocs2PXfRbmzHVcXEXaiZZcaOPZko

