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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / pgbackrest.conf
Last active April 7, 2021 14:49
Sample of pgbackrest.conf
[global]
repo1-path=/data/pgbackrest/backups/
log-level-console=info
log-level-file=detail
log-path=/data/pgbackrest/log/
start-fast=y
retention-diff=7
retention-full=4
compress-level=9
@ycku
ycku / listpk.sql
Last active February 9, 2021 05:38
列出資料庫中的 Primary key
SELECT s.schemaname,
s.indexname
FROM pg_index i
JOIN pg_indexes s
ON i.indexrelid = ( s.schemaname||'.'||s.indexname ) :: regclass :: oid
WHERE i.indisprimary = TRUE;
@ycku
ycku / column_comment.sql
Created February 2, 2021 09:41
PostgreSQL show comments of all column in the specified table by query
-- https://www.postgresql.org/docs/current/infoschema-columns.html
SELECT
cols.column_name,
(
SELECT
pg_catalog.col_description(c.oid, cols.ordinal_position::int)
FROM pg_catalog.pg_class c
WHERE
c.oid = ((cols.table_schema||'.'||cols.table_name)::regclass::oid) AND
c.relname = cols.table_name