Skip to content

Instantly share code, notes, and snippets.

View ycku's full-sized avatar
😈
Be evil

Yung-Chung Ku ycku

😈
Be evil
View GitHub Profile
@ycku
ycku / HAProxy_pgbench.md
Last active May 25, 2021 07:09
HAProxy pgbench benchmark
include exclude overhead
1 3279 5010 34.55%
2 3531 5375 34.31%
3 3274 4998 34.49%
4 3382 5168 34.56%
5 3649 5559 34.36%
6 3449 5253 34.34%
7 3404 5180 34.29%
8 3717 5661 34.34%
@ycku
ycku / pgbouncer_pgbench.md
Last active May 25, 2021 07:19
pgbouncer pgbench benchmark
include exclude overhead
1 3847 3965 2.98%
2 4004 4123 2.89%
3 3640 3744 2.78%
4 4147 4267 2.81%
5 4072 4175 2.47%
6 3922 4020 2.44%
7 3846 3962 2.93%
8 4142 4244 2.40%
@ycku
ycku / uuid_timestamp.sql
Created July 25, 2021 16:21
Extract timestamp from uuid
-- Source from: https://stackoverflow.com/questions/24178485/cast-or-extract-timestamp-from-v1-uuid-in-postgresql/61508178
CREATE OR REPLACE FUNCTION uuid_timestamp(uuid UUID) RETURNS TIMESTAMPTZ AS $$
DECLARE
bytes bytea;
BEGIN
bytes := uuid_send(uuid);
RETURN to_timestamp(
(
(
(get_byte(bytes, 0)::bigint << 24) |
@ycku
ycku / pg_connstr_regex.sh
Last active July 29, 2021 05:38
Retrieve connection info from PostgreSQL connection string
#!/bin/bash
str='postgresql://dbri@cxlitstaget01:5432/cxlimq_bm'
str='postgresql://dbir@cxlitstage01/cxlimq_bm'
str='postgresql://cxlitstage01:5432/cxlimq_bm'
regex='postgresql://(.*)@(.*):([0-9]*)/(.*)'
[[ $str =~ $regex ]]
if [ "$str" = "${BASH_REMATCH[0]}" ]; then
@ycku
ycku / fake_twid.sql
Created November 4, 2021 04:55
Generate Taiwan ID in PostgreSQL
create or replace function fake_twid()
returns text
language plpgsql
as
$$
declare
ret text;
check_num int;
check_index int[];
digit int;
@ycku
ycku / hyper-v.bat
Created October 4, 2022 01:59
To enable Hyper-V in Windows 11 Home
pushd "%~dp0"
dir /b %SystemRoot%\servicing\Packages\*Hyper-V*.mum >hv.txt
for /f %%i in ('findstr /i . hv.txt 2^>nul') do dism /online /norestart /add-package:"%SystemRoot%\servicing\Packages\%%i"
del hv.txt
Dism /online /enable-feature /featurename:Microsoft-Hyper-V -All /LimitAccess /ALL
pause
@ycku
ycku / NOT IN-100000.sql
Created November 9, 2022 14:05
你又不小心 IN 了嗎?
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
Aggregate (cost=4404.56..4404.57 rows=1 width=8) (actual time=65.949..65.950 rows=1 loops=1)
-> Seq Scan on s (cost=2137.12..4274.25 rows=52125 width=0) (actual time=39.009..64.162 rows=37156 loops=1)
Filter: (NOT (hashed SubPlan 1))
Rows Removed by Filter: 62844
SubPlan 1
-> Seq Scan on t (cost=0.00..1876.50 rows=104250 width=4) (actual time=0.029..13.914 rows=100000 loops=1)
Planning Time: 5.089 ms
Execution Time: 66.337 ms
@ycku
ycku / NOT EXIST-100000.sql
Created November 9, 2022 14:07
你又不小心 IN 了嗎?
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=6135.53..6135.54 rows=1 width=8) (actual time=35.063..36.716 rows=1 loops=1)
-> Gather (cost=3630.09..6135.53 rows=1 width=0) (actual time=19.535..35.008 rows=37156 loops=1)
Workers Planned: 2
Workers Launched: 2
-> Parallel Hash Anti Join (cost=2630.09..5135.43 rows=1 width=0) (actual time=11.705..23.096 rows=12385 loops=3)
Hash Cond: (s.key = t.key)
-> Parallel Index Only Scan using s_key_idx on s (cost=0.29..2088.96 rows=41667 width=4) (actual time=0.069..4.765 rows=33333 loops=3)
Heap Fetches: 0
@ycku
ycku / NOT IN-200000.sql
Last active November 9, 2022 15:53
你又不小心 IN 了嗎?
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
Finalize Aggregate (cost=247880438.47..247880438.48 rows=1 width=8) (actual time=1219349.866..1219350.085 rows=1 loops=1)
-> Gather (cost=247880438.25..247880438.46 rows=2 width=8) (actual time=1219349.805..1219350.074 rows=3 loops=1)
Workers Planned: 2
Workers Launched: 2
-> Partial Aggregate (cost=247879438.25..247879438.26 rows=1 width=8) (actual time=1218624.458..1218624.460 rows=1 loops=3)
-> Parallel Index Only Scan using s_key_idx on s (cost=0.42..247879334.09 rows=41667 width=0) (actual time=326.082..1218608.814 rows=9061 loops=3)
Filter: (NOT (SubPlan 1))
Rows Removed by Filter: 57606
@ycku
ycku / NOT EXISTS-200000.sql
Created November 9, 2022 14:21
你又不小心 IN 了嗎?
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------
Finalize Aggregate (cost=10449.10..10449.11 rows=1 width=8) (actual time=101.312..104.828 rows=1 loops=1)
-> Gather (cost=10448.99..10449.10 rows=1 width=8) (actual time=100.990..104.822 rows=2 loops=1)
Workers Planned: 1
Workers Launched: 1
-> Partial Aggregate (cost=9448.99..9449.00 rows=1 width=8) (actual time=90.238..90.241 rows=1 loops=2)
-> Parallel Hash Anti Join (cost=4774.06..9446.73 rows=904 width=0) (actual time=64.249..89.494 rows=13592 loops=2)
Hash Cond: (s.key = t.key)
-> Parallel Seq Scan on s (cost=0.00..2843.47 rows=117647 width=4) (actual time=0.007..11.334 rows=100000 loops=2)