@@ -91,58 +91,55 @@ $$ LANGUAGE plpgsql;
9191`
9292}
9393
94- //TODO: check index exists before running alter table, it will throw an error if exists
95- // This will two things:
96- // 1- An unique index by block_id and service_id to enable upsert operation
97- // 2- A function that receives the height (id) and upsert the values of that block
98- // for the table staked_apps_by_block_and_services
94+ export const upsertAppsByBlockAndServicesFnName = 'upsert_staked_apps_by_block_and_services'
95+
96+ // Here we are creating a table outside SubQuery to avoid unnecessary indexes
9997export function upsertAppsByBlockAndServiceFn ( dbSchema : string ) : string {
100- return `DO $$
101- BEGIN
102- IF NOT EXISTS (
103- SELECT 1
104- FROM pg_constraint
105- WHERE conname = 'staked_apps_by_block_and_services_block_service_key'
106- AND conrelid = '${ dbSchema } .staked_apps_by_block_and_services'::regclass
107- ) THEN
108- ALTER TABLE ${ dbSchema } .staked_apps_by_block_and_services
109- ADD CONSTRAINT staked_apps_by_block_and_services_block_service_key
110- UNIQUE (block_id, service_id);
111- END IF;
112- END;
113- $$;
98+ return `
99+ CREATE TABLE IF NOT EXISTS ${ dbSchema } .staked_apps_by_block_and_services
100+ (
101+ tokens numeric NOT NULL,
102+ amount integer NOT NULL,
103+ block_id numeric NOT NULL,
104+ service_id text NOT NULL,
105+ _id uuid NOT NULL,
106+ CONSTRAINT apps_by_block_and_services_pkey PRIMARY KEY (_id)
107+ );
114108
115- CREATE OR REPLACE FUNCTION ${ dbSchema } .upsert_staked_apps_by_block_and_services(p_block_id bigint)
109+ COMMENT ON TABLE ${ dbSchema } .staked_apps_by_block_and_services
110+ IS '@foreignKey (block_id) REFERENCES blocks (id)';
111+
112+ CREATE INDEX IF NOT EXISTS idx_apps_services_block_id
113+ ON ${ dbSchema } .staked_apps_by_block_and_services USING btree
114+ (block_id ASC NULLS LAST)
115+ TABLESPACE pg_default;
116+
117+ CREATE OR REPLACE FUNCTION ${ dbSchema } .${ upsertAppsByBlockAndServicesFnName } (p_block_id bigint)
116118RETURNS void AS $$
117119BEGIN
120+ -- Delete existing rows for this block
121+ DELETE FROM ${ dbSchema } .staked_apps_by_block_and_services
122+ WHERE block_id = p_block_id;
123+
118124 INSERT INTO ${ dbSchema } .staked_apps_by_block_and_services (
119125 _id,
120- id,
121126 block_id,
122127 service_id,
123128 amount,
124- tokens,
125- _block_range
129+ tokens
126130 )
127131 SELECT
128132 uuid_generate_v4(), -- _id (UUID)
129- CONCAT(p_block_id::text, '-', ss.service_id) AS id,
130133 p_block_id,
131134 ss.service_id,
132135 COUNT(*) AS amount,
133- SUM(s.stake_amount) AS tokens,
134- int8range(p_block_id, NULL) AS _block_range -- open-ended: [block_id,)
136+ SUM(s.stake_amount) AS tokens
135137 FROM ${ dbSchema } .applications s
136138 INNER JOIN ${ dbSchema } .application_services ss ON ss.application_id = s.id
137139 WHERE s.stake_status = 'Staked'
138140 AND s._block_range @> p_block_id
139141 AND ss._block_range @> p_block_id
140- GROUP BY ss.service_id
141- ON CONFLICT (block_id, service_id) DO UPDATE
142- SET
143- amount = EXCLUDED.amount,
144- tokens = EXCLUDED.tokens,
145- _block_range = EXCLUDED._block_range;
142+ GROUP BY ss.service_id;
146143END;
147144$$ LANGUAGE plpgsql;
148145`
0 commit comments