Skip to content

Instantly share code, notes, and snippets.

View rodo's full-sized avatar

Rodolphe Quiédeville rodo

View GitHub Profile
@rodo
rodo / gist:66328023fdf07ddd34b888a5d3702604
Created March 20, 2024 20:37
Datadog grok parser for PostgreSQL checkpoint complete
checkpointComplete %{date("yyyy-MM-dd HH:mm:ss z"):date}\:\:@\:\[%{integer:group_id}\]\:%{notSpace:severity}\:\s+checkpoint complete: wrote %{integer:buffers.wrote} buffers \(%{number:buffers.percent}\%\); %{integer:walfile.added} WAL file\(s\) added, %{integer:walfile.removed} removed, %{integer:walfile.recycled} recycled; write=%{number:time.write} s, sync=%{number:time.sync} s, total=%{number:time.total}\ s; sync files=%{integer:sync.files}, longest=%{number:sync.longest} s,\s+average=%{number:sync.average} s;\s+distance=%{integer:distance}\s+kB,\s+estimate=%{integer:estimate}\s+kB;\slsn=%{notSpace:lsn},\sredo lsn=%{notSpace:redo_lsn}
@rodo
rodo / follow_stat.sql
Created March 18, 2024 10:35
Follow the user objects stats in postgresql
-- first create a schema to store your new table
CREATE SCHEMA dba;
-- create the table
CREATE TABLE dba.log_stat (LIKE pg_stat_user_tables);
--- add a column to timestamp the figures
ALTER TABLE dba.log_stat ADD COLUMN eventtime timestamp with time zone default now();
--- last action insert the stats in the table
@rodo
rodo / nextid.sql
Last active March 9, 2024 20:43
Want to fill the holes in a column previously defined with a serial in PostgreSQL
-- Want to file the holes in a column previously defined with a serial
-- Useful only with small tables with less than 10k rows, over the operation
-- max() and the join will cost too much
--
CREATE OR REPLACE FUNCTION nextid()
RETURNS integer
LANGUAGE plpgsql
AS $$
DECLARE
new_id integer;
@rodo
rodo / gist:320499c515e5c215e9b580a7ee28d564
Created December 9, 2021 09:51
PostgreSQL trigger to historize data
CREATE FUNCTION {{table_name}}_historization_update_trg()
RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
-- we increment the version number
NEW.version = OLD.version + 1;
INSERT INTO {{schema}}.{{table_name}}_log (id, version, data)
VALUES (NEW.id, NEW.version, to_json(NEW));
RETURN NEW;
@rodo
rodo / filter-recycling.sh
Last active March 11, 2021 14:44
Filter OSM file on amenity=recycling
osmosis --read-pbf france-latest.osm.pbf \
--node-key-value keyValueList="amenity.recycling" \
--write-xml france-latest.osm
@rodo
rodo / get_random_date.sql
Created February 19, 2021 14:47
get_random_date for PostgreSQL
[local]:5412 rodo@rodo=# SELECT get_random_date(),
get_random_date('3 week') as rand,
get_random_date('3 week', 'past') as past,
get_random_date('3 week', 'future') as future,
generate_series(1,7);
get_random_date | rand | past | future | generate_series
-----------------+------------+------------+------------+-----------------
2017-03-01 | 2021-02-27 | 2021-02-19 | 2021-03-04 | 1
2007-03-25 | 2021-03-06 | 2021-02-15 | 2021-02-25 | 2
2019-09-01 | 2021-02-21 | 2021-02-14 | 2021-03-02 | 3
[local]:5412 rodo@rodo=# SELECT trunc(random()*100);
trunc
-------
84
(1 row)
@rodo
rodo / gist:ffafe81b7dd4b41625278a740bf4ff13
Last active February 19, 2021 09:43
PostgreSQL random date
[local]:5412 rodo@rodo=# SELECT get_random_date('1 week'), generate_series(1,5);
get_random_date | generate_series
-----------------+-----------------
2021-02-22 | 1
2021-02-18 | 2
2021-02-17 | 3
2021-02-22 | 4
2021-02-17 | 5
(5 rows)
rmmod snd_seq_midi
rmmod snd_seq_midi_event
rmmod snd_soc_skl
rmmod snd_soc_core
rmmod snd_compress
rmmod snd_hda_intel
rmmod snd_hwdep
rmmod snd_seq
rmmod snd_hda_codec
rmmod snd_hda_ext_core
@rodo
rodo / gist:e2fff23da180390fbff0d34dbc72c460
Last active January 8, 2018 16:56
Manage postgresql template
# our template database will be called template2
# as user foobar
$ createdb template2
$ psql -d template2 -c 'create table test (id integer)'
$ psql -d template2 -c '\d' # you see that the table is owned by foobar
# create a new db based on the template
$ createdb new -T template2 -O newuser
$ psql -d new -c 'REASSIGN OWNED BY foobar TO newuser'
$ psql -d new -c '\d' # the table test is owned by newuser
$ psql -l # show that the owner of template2 is now newuser