Skip to content

Instantly share code, notes, and snippets.

View fabriziomello's full-sized avatar
💭
Idle in transaction

Fabrízio de Royes Mello fabriziomello

💭
Idle in transaction
View GitHub Profile
#!/bin/bash
#
# O pgbadger no modo incremental gera um indice (arquivos .bin)
# para fazer o parser do log, e não possui um mecanismo interno
# de limpeza de arquivos que não estão mais em uso.
#
# Este script efetua a limpeza dos .bin obsoletos, ou seja,
# aqueles que são de dias anteriores aos correspondentes a
# semana corrente.
@fabriziomello
fabriziomello / gist:c3cfcee97e9c2945aa5e
Created January 8, 2016 02:15
ALTER TABLE ... SET LOGGED, SET TABLESPACE ...
fabrizio=# \db+
List of tablespaces
Name | Owner | Location | Access privileges | Options | Size | Description
------------+----------+----------+-------------------+---------+--------+-------------
pg_default | fabrizio | | | | 27 MB |
pg_global | fabrizio | | | | 496 kB |
(2 rows)
fabrizio=# create tablespace tb1 location '/tmp/tablespace';
CREATE TABLESPACE
@fabriziomello
fabriziomello / array_remove.sql
Last active February 24, 2016 22:18
Hacking PostgreSQL: exemplo array_remove(anyarray, anyarray) em PL/SQL
CREATE OR REPLACE FUNCTION array_remove (anyarray, anyarray) RETURNS anyarray
AS $$
SELECT array(
SELECT e
FROM unnest($1) AS s(e)
WHERE NOT e = ANY($2)
);
$$
LANGUAGE sql;
ssl = off
maintenance_work_mem = 2GB
work_mem = 2GB
fsync = off
synchronous_commit = off
wal_level = minimal
max_wal_senders = 0
archive_mode = off
wal_buffers = -1
wal_writer_delay = 20ms
@fabriziomello
fabriziomello / gapless_sequence.sql
Last active August 23, 2023 03:09
Gapless sequence example in PostgreSQL
CREATE TABLE public.gapless_sequence (
sequence_schema TEXT,
sequence_name TEXT,
last_value BIGINT DEFAULT 0,
step INTEGER DEFAULT 1
);
ALTER TABLE public.gapless_sequence
ADD CONSTRAINT gapless_sequence_pk PRIMARY KEY (sequence_schema, sequence_name);
@fabriziomello
fabriziomello / pgbouncer-show-dblink-fdw.sql
Last active October 5, 2022 12:40
pgbouncer-show-dblink-fdw
@fabriziomello
fabriziomello / get_memory_info.sql
Last active January 25, 2022 21:35
Get memory info from a certain process
-- https://psutil.readthedocs.io/en/latest/#psutil.Process.memory_full_info
-- Linux: 0=rss, 1=vms, 2=shared, 3=text, 4=lib, 5=data, 6=dirty, 7=uss, 8=pss, 9=swap
CREATE OR REPLACE FUNCTION get_memory_info(pid INTEGER DEFAULT NULL, memtype INTEGER DEFAULT 0)
RETURNS BIGINT
AS
$$
import psutil
p = psutil.Process(pid)
return p.memory_full_info()[memtype]
$$
CREATE OR REPLACE FUNCTION bytea_import(filename text) RETURNS bytea AS
$$
import os
with open(filename, 'r') as f:
s = f.read()
return s
$$
LANGUAGE 'plpythonu' VOLATILE;
@fabriziomello
fabriziomello / desafio-hb.sql
Created December 7, 2019 14:15
Desafio proposto por Henrique Bastos no Instagram
/*
*
* Desafio do Henrique Bastos ()
*
* Dado uma lista de números e um número k, retorne se quaisquer dois números da lista
* somados resultam no número k.
*
* Por exemplo, dado [10, 15, 3, 7] e k = 17, retorne verdadeiro, já que 10 + 7 = 17.
*
* Passe pelos números da lista uma única vez.
@fabriziomello
fabriziomello / search_in_tables.sql
Last active March 23, 2022 20:23
search_in_tables
/*
* search_in_tables
*
* Usage example:
* SELECT * FROM search_in_tables('public', 'foo', '%pattern%');
*
*/
CREATE OR REPLACE FUNCTION search_in_tables(
IN ptable_schema TEXT,
IN ptable_name TEXT,