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 / global_search.sql
Last active April 15, 2023 02:22
search text global.
/*
input: search word, schemas[], verbose_is_true_or_false
output: ctids, tablename, column name.
global_search is a plpgsql function that search _srctext in all the visible tables(table,materialized view) of the public schema (default)
if you specified schemas, then search agaginst all the visible tables in an array schemas (param schema_names).
--for each table being searched, find only 10 rows that have the searched word.
-- skipped the partititoned table.
*/
CREATE OR REPLACE FUNCTION global_search (
@jianhe-fun
jianhe-fun / less_downtime_unique_to_primary_key.sql
Created April 10, 2023 11:16
convert unique index to primary key
--https://dba.stackexchange.com/questions/324803/add-composite-primary-key-on-existing-postgresql-table/324815#324815
CREATE TABLE demo (
a int,
b int,
CONSTRAINT a_b UNIQUE (a, b)
);
INSERT INTO demo VALUES (1, 2),(2, 3);
@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
@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 / 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 / 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 / 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 / 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 / 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 / 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