Hi,
I am pretty sure that, there will be a right heading for this post. For now, i am going with this. If you could suggest me proper heading, i will update it :-)
OK. let me explain the situation. Then will let you know what i am trying to do here, and how i did it.
Situation here is,
We have a table, which we need to run update on “R” no.of records. The update query is using some joins to get the desired result, and do update the table.
To process these “R” no.of records, it is taking “H” no.of hours. That too, it’s giving load on the production server. So, we planned to run this UPDATE as batch process.
Per a batch process, we took “N” no.or records. To process this batch UPDATE, it is taking “S” no.of seconds.With the above batch process, production server is pretty stable, and doing great. So, we planned to run these Batch updates parallel.
I mean, “K” sessions, running different record UPDATEs. Of-course, we can also increase the Batch size here.
But we want to use much cpus to complete all these UPDATES as soon as possible.Problem here is, So, as i said, we need to run multiple UPDATEs on multiple records in parallel. But, how can one session is going to communicate with other sessions on this batch records.
I mean, If one session is running updates on 1 to 1000, how could the second session knows that the other session was processing from 1 to 1000.
If the second session knows this information, this will start from 1001 to 2000 in parallel. This is the problem i am trying to solve here.
I am not sure whether this is the optimal solution, but as per my requirement it’s working. :-) Let me know if you see any problems in it.
Object Definitions
Table "public.test" Column | Type | Modifiers --------+---------+---------------------------------------------------- t | text | i | boolean | seq | bigint | not null default nextval('test_seq_seq'::regclass) postgres=# INSERT INTO test VALUES(generate_series(1, 9000), false, generate_series(1, 9000)); INSERT 0 9000 postgres=# \ds testing List of relations Schema | Name | Type | Owner --------+---------+----------+---------- public | testing | sequence | postgres (1 row) CREATE OR REPLACE FUNCTION public.update_test_parallel(batch integer) RETURNS void LANGUAGE plpgsql AS $function$ DECLARE VAR BIGINT; DUMMY TEXT; BEGIN -- Adding this for Demo -- SELECT pg_sleep(10) INTO DUMMY; SELECT pg_advisory_lock(-1234) INTO DUMMY; SELECT nextval('testing') INTO VAR; EXECUTE E'SELECT nextval(\'testing\') FROM generate_series('||VAR||','||VAR+BATCH||')'; -- We need to decrease the sequence value by one, since we executed nextval expression once -- Otherwise, it will affect the other session''s execution. -- SELECT setval('testing', currval('testing')-1) INTO DUMMY; SELECT pg_advisory_unlock(-1234) INTO DUMMY; -- I want to update the test table of the column "I" with value "true". -- UPDATE test SET I=true WHERE SEQ BETWEEN VAR AND (VAR+BATCH); RAISE NOTICE 'VAR IS %, VAR+BATCH IS %', VAR, (VAR+BATCH); RAISE NOTICE 'CURRENT SEQ VALUE IS %', currval('testing'); EXCEPTION WHEN OTHERS THEN -- If there is an exception, we need to reset the sequence to it''s start position again. -- So that, the other sessions, will try with the same sequence numbers. -- SELECT setval('testing', VAR-1) INTO DUMMY; SELECT pg_advisory_unlock(-1234) INTO DUMMY; RAISE EXCEPTION '%', SQLERRM; END; $function$;Session 1
postgres=# SELECT public.update_test_parallel(3000); NOTICE: VAR IS 1, VAR+BATCH IS 3001 NOTICE: CURRENT SEQ VALUE IS 3000 update_test_parallel ---------------------- (1 row)Session 2
postgres=# SELECT public.update_test_parallel(3000); NOTICE: VAR IS 3001, VAR+BATCH IS 6001 NOTICE: CURRENT SEQ VALUE IS 6000 update_test_parallel ---------------------- (1 row)Session 3
postgres=# SELECT public.update_test_parallel(3000); NOTICE: VAR IS 6001, VAR+BATCH IS 9001 NOTICE: CURRENT SEQ VALUE IS 9000 update_test_parallel ---------------------- (1 row)Desired result
postgres=# SELECT COUNT(*) FROM test WHERE i is true; count ------- 9000 (1 row)
In the above implementation, i took "sequence" for the session's parallel execution with the help of advisory locks. Hope this helps to others as well.
Thanks as always for reading it, and welcome your inputs.
--Dinesh Kumar
Essentially what you're looking for is a Queuing mechanism in PostgreSQL. In the future (PostgreSQL 9.5+) this could be easily implemented using the SKIP LOCKED feature:
ReplyDeletehttp://michael.otacoo.com/postgresql-2/postgres-9-5-feature-highlight-skip-locked-row-level/
Yes True. I was trying this in PG <9.5 versions.
DeleteSuch a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
ReplyDeleteDevOps Training | Certification in Chennai | DevOps Training | Certification in anna nagar | DevOps Training | Certification in omr | DevOps Training | Certification in porur | DevOps Training | Certification in tambaram | DevOps Training | Certification in velachery
his brilliant site really has the entirety of the data I needed concerning this subject and didn't have a clue who to inquire. best interiors
ReplyDeleteThis could be a problem with my internet browser because I’ve had this happen before. Thank you 무료야설
ReplyDeleteExcellent Blog! I would like to thank you for the efforts you have made in writing this post. 토토
ReplyDeletei found your this call despite the fact that searching for a few related reference concerning blog search...Its a nice publicize..store posting and update the mention. VPN Crack For Pc
ReplyDeleteinstagram takipçi satın al
ReplyDeletecasino siteleri
QWGQ8
kralbet
ReplyDeletebetpark
tipobet
slot siteleri
kibris bahis siteleri
poker siteleri
bonus veren siteler
mobil ödeme bahis
betmatik
CCC5ZL
تتمتع مصانع عبوات بلاستيك في مصر بقدرة عالية على التوافق مع المعايير الدولية للجودة والسلامة والصحة المهنية حيث تضع هذه المصانع أهمية كبيرة على الالتزام بالمعايير الدولية والمحلية وتطبيق الإجراءات الوقائية اللازمة للحفاظ على صحة وسلامة العمال والبيئة.
ReplyDeleteيعتبر الدكتور محمود ناصر افضل دكتور متخصص في علاج غرغرينا القدم السكري في مصر يأتي اليه المرضي من مختلف محافظات مصر نظرا لكفائته وخبرته الطريلة في المجال , فمن المعروف عن الغرغرينا انها من الامراض الخطيرة التي تأتي نتيجة مرض السكر.
ReplyDeletebayrampaşa
ReplyDeletegüngören
hakkari
izmit
kumluca
3LV
salt likit
ReplyDeletesalt likit
XDVE7
yurtdışı kargo
ReplyDeleteresimli magnet
instagram takipçi satın al
yurtdışı kargo
sms onay
dijital kartvizit
dijital kartvizit
https://nobetci-eczane.org/
ZVY0O
resimli magnet
ReplyDeleteresimli magnet
çerkezköy çatı ustası
silivri çatı ustası
dijital kartvizit
0KWRCD
karabük evden eve nakliyat
ReplyDeletebartın evden eve nakliyat
maraş evden eve nakliyat
mersin evden eve nakliyat
aksaray evden eve nakliyat
JLW
düzce evden eve nakliyat
ReplyDeletedenizli evden eve nakliyat
kırşehir evden eve nakliyat
çorum evden eve nakliyat
afyon evden eve nakliyat
3LQ
33F3A
ReplyDeleteYobit Güvenilir mi
Çerkezköy Halı Yıkama
Aksaray Parça Eşya Taşıma
Etimesgut Parke Ustası
Karabük Lojistik
Kütahya Evden Eve Nakliyat
Aksaray Şehirler Arası Nakliyat
Iğdır Parça Eşya Taşıma
Siirt Şehir İçi Nakliyat
6C518
ReplyDeleteBitmart Güvenilir mi
Kırklareli Parça Eşya Taşıma
Etlik Parke Ustası
Ağrı Evden Eve Nakliyat
Osmaniye Şehirler Arası Nakliyat
Hakkari Şehirler Arası Nakliyat
Çerkezköy Bulaşık Makinesi Tamircisi
Mardin Evden Eve Nakliyat
Siirt Parça Eşya Taşıma
AEF20
ReplyDeleteMuğla Parça Eşya Taşıma
Binance Güvenilir mi
Siirt Lojistik
Kastamonu Parça Eşya Taşıma
Sivas Evden Eve Nakliyat
Urfa Parça Eşya Taşıma
Amasya Şehirler Arası Nakliyat
Tekirdağ Parça Eşya Taşıma
Aydın Evden Eve Nakliyat
08D5C
ReplyDeleteAksaray Evden Eve Nakliyat
Balıkesir Parça Eşya Taşıma
Kayseri Parça Eşya Taşıma
Karabük Parça Eşya Taşıma
Keçiören Boya Ustası
Erzincan Şehir İçi Nakliyat
Hatay Şehir İçi Nakliyat
Çerkezköy Ekspertiz
Iğdır Evden Eve Nakliyat
5592B
ReplyDeleteKırklareli Parça Eşya Taşıma
Bitmart Güvenilir mi
Niğde Lojistik
Bolu Şehirler Arası Nakliyat
Bilecik Şehirler Arası Nakliyat
Şırnak Şehir İçi Nakliyat
Keçiören Parke Ustası
Ankara Parke Ustası
Yobit Güvenilir mi
34A6C
ReplyDeleteİzmir Şehirler Arası Nakliyat
boldenone for sale
parabolan
Mersin Evden Eve Nakliyat
Tekirdağ Çatı Ustası
Eryaman Parke Ustası
Bingöl Lojistik
Karabük Lojistik
Çerkezköy Çamaşır Makinesi Tamircisi
8A6A3
ReplyDeleteCointiger Güvenilir mi
Adıyaman Evden Eve Nakliyat
Samsun Evden Eve Nakliyat
Çerkezköy Cam Balkon
Altındağ Parke Ustası
Isparta Evden Eve Nakliyat
Elazığ Evden Eve Nakliyat
Paribu Güvenilir mi
Referans Kimliği Nedir
çeşme transfer
ReplyDeletekralbet
bor yağı filtre kağıdı
yağ süzme filtre kağıdı
CT1EC
05E63
ReplyDeletehuobi
bitcoin nasıl oynanır
binance
btcturk
bingx
en iyi kripto grupları telegram
paribu
bitget
paribu
تتميز شركة كلين الرياض في الرياض بتوفير خدمات شاملة
ReplyDeleteشركة تنظيف منازل بالرياض
شركة سبيد واي تبرز كواحدة من أبرز الخيارات المتاحة للعملاء الذين يبحثون عن خدمة نقل الاثاث
ReplyDeleteنقل عفش بالرياض 200 ريال
شركة كشف تسربات المياه بالاحساء
ReplyDeleteشركة كشف تسربات المياه
شركة كشف تسربات المياه بالاحساء KDFup5FJmg
ReplyDeleteشركة كشف تسربات المياه بالاحساء yJgimA3LGU
ReplyDeleteشركة كشف تسربات المياه بالاحساء QRfVKpUBPi
ReplyDeleteشركة كشف تسربات المياه بالاحساء QEMCQCIDfv
ReplyDeleteشركة مكافحة حشرات 9ptvMLO7rS
ReplyDeleteشركة تسليك مجاري بالاحساء iVKPxPy0pV
ReplyDeleteشركة مكافحة النمل الابيض بالاحساء zkv2sxQhVq
ReplyDeleteشركة تسليك مجاري بالاحساء YBvnSEoa24
ReplyDelete