Skip to content

Instantly share code, notes, and snippets.

View jianhe-fun's full-sized avatar
🏠
Working from home

jianhe jianhe-fun

🏠
Working from home
View GitHub Profile
@jianhe-fun
jianhe-fun / backrest_pitr.sh
Created September 16, 2022 06:56
pitr pgbackrest demo
# https://pgbackrest.org/user-guide.html#pitr
/usr/local/pgsql15/bin/pg_ctl -D /usr/local/pgsql15/data_42091266 stop
#info check
sudo -u jian pgbackrest --stanza=pgsql15_3 --log-level-console=info check
sudo -u jian pgbackrest --stanza=pgsql15_3 --type=diff --log-level-console=info backup
/usr/local/pgsql15/bin/pg_ctl -D /usr/local/pgsql15/data_42091266 stop
@jianhe-fun
jianhe-fun / latest_row_per_user.sql
Created September 18, 2022 08:25
-- Optimize GROUP BY query to retrieve latest row per user
-- Optimize GROUP BY query to retrieve latest row per user
--https://dbfiddle.uk/men99GVd
begin;
create table users(
user_id bigint primary key generated always as identity
,username text not null
);
insert into users(username) select 'usr_'::text || g
from generate_series(1,1000) g;
@jianhe-fun
jianhe-fun / capture_n_row_in_a_group.sql
Created September 18, 2022 15:25
capture first n row in an group.
/*
https://dbfiddle.uk/DRmCu_7u
*/
begin;
create table observations(
id bigint primary key generated always as identity
,station_id int
,create_at timestamptz
,foo text
@jianhe-fun
jianhe-fun / lateral.sql
Created September 26, 2022 17:43
lateral function call on true
/*
https://stackoverflow.com/questions/38297935/split-function-returned-record-into-multiple-columns
*/
create or replace function hi_lo(a numeric,
b numeric,
c numeric,
OUT hi numeric,
OUT lo numeric)
as $$
@jianhe-fun
jianhe-fun / top_row_per_group.sql
Created September 28, 2022 07:10
top_row_per_group postgresql
/*
Find latest entries for a person_id by submission_date for specified
filter criteria type, plan, status.
There could be more such filters,
but the logic to return latest by submission date is the same regardless.
Two major uses one for paginated viewing in UI
and second for generating reports.
https://stackoverflow.com/questions/61160156/get-paginated-rows-and-total-count-in-single-query/61160817#61160817
*/
@jianhe-fun
jianhe-fun / tsrange constraint.sql
Last active April 2, 2023 11:10
for timestamp range, enforce no_adjacent, no_overlap, enforce_bounds constraints.
--using constraint make 2 timestamp range not overlap.
--using constraint make 2 timestamp range not adjacent.
--using constraint make rtimestamp range include range lower bound, exclude upper bound.
/*
https://dbfiddle.uk/KyOD2a58
Preventing adjacent/overlapping entries with EXCLUDE in PostgreSQL
https://stackoverflow.com/questions/19504727/preventing-adjacent-overlapping-entries-with-exclude-in-postgresql/19505869#19505869
*/
DROP TABLE test_gist_lap;
BEGIN;
@jianhe-fun
jianhe-fun / set_search_path.sql
Created April 2, 2023 13:14
properly set search_path in cluster, database, role, session, function level.
--https://stackoverflow.com/questions/9067335/how-does-the-search-path-influence-identifier-resolution-and-the-current-schema/9067777#9067777
--properly set search path.
--Set it in cluster level.
ALTER SYSTEM SET search_path TO 'public';
SELECT pg_reload_conf();
--Set it as default for database test16.
ALTER DATABASE test16 SET search_path = '$user', 'public';
-- Set it as default for the role (bob) you connect with (effective cluster-wide):
@jianhe-fun
jianhe-fun / passing_var_while_insert.sql
Created April 3, 2023 12:20
passing variable while do insert operation
/*
https://dba.stackexchange.com/questions/303502/pass-a-variable-with-insert-update-delete-statements/303546#303546
https://dbfiddle.uk/ZsFssnOW
*/
CREATE TABLE ins_tbl (
id int GENERATED ALWAYS AS IDENTITY,
name text
);
@jianhe-fun
jianhe-fun / escape_regular_expression.sql
Created April 4, 2023 06:31
escape_regular_expression.sql
/*
https://stackoverflow.com/questions/5144036/escape-function-for-regular-expression-or-like-patterns/45741630#45741630
escape regular expression.
*/
CREATE OR REPLACE FUNCTION public.f_regexp_escape(text)
RETURNS text
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT
@jianhe-fun
jianhe-fun / select_specific_column_to_json_agg.sql
Created April 7, 2023 12:11
select_specific_column_to_json_agg.sql
/*
https://dba.stackexchange.com/questions/69655/select-columns-inside-json-agg/69658#69658
*/
BEGIN;
SET local search_path = '';
CREATE temp TABLE tbl_a (
id bigint,
name text