Skip to content

Instantly share code, notes, and snippets.

@adunstan
adunstan / 20mirror-outside-files
Created November 25, 2015 14:06
etckeeper script for mirroring outside files
#!/bin/sh
set -e
# Based on nealmcb's + ErebusBat's script from http://serverfault.com/questions/211425/
# put this file in etckeeeper's commit.d directory
# in the config file set MIRROR_ROOT (somewhere under /etc) MIRROR_FILES and MIRROR_DIRS
# the latter two can contain wildcards
# all three symbols must be exported or they won't be seen by this script
with recursive
dag (depended_on,dependent) as
(
values
('b2'::text,'d1'::text),
('d1','d2'),
('d2','d3'),
('d3','d4'),
('b1','a1'),
('b2','a1'),
@adunstan
adunstan / sql_json_comparison_ops.sql
Last active April 18, 2023 10:26
JSON comparison operations in SQL for PostgreSQL
CREATE OR REPLACE FUNCTION json_cmp(left json, right json)
RETURNS integer AS $$
select bttextcmp($1::text, $2::text)
$$ LANGUAGE sql IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION json_eq(json, json)
RETURNS BOOLEAN LANGUAGE SQL STRICT IMMUTABLE AS $$
SELECT json_cmp($1, $2) = 0;
$$;
@adunstan
adunstan / vacuum_schema.sql
Last active December 24, 2024 16:16
Vacuum a PostgreSQL schema's tables via dblink
-- requires dblink extension the be installed
-- vacuum a schema via dblink
create or replace function vacuum_schema(schemaname text)
returns void
language plpgsql
as
$func$
declare
@adunstan
adunstan / explain_url.sql
Last active December 16, 2015 11:49
exlain_url sql function
CREATE OR REPLACE FUNCTION public.explain_url(query text)
RETURNS text
LANGUAGE plperlu
AS $function$
my $query = shift;
elog(ERROR,"not an explain query") unless $query =~ /^\s*explain\s/i;
my $rv = spi_exec_query($query);
my $nrows = $rv->{processed};
my $text = $query . "\n QUERY PLAN\n-------------------\n";
@adunstan
adunstan / Dist.pm
Created November 22, 2011 13:43
example Buildfarm module to run "make dist" and collect the results
package PGBuild::Modules::Dist;
use PGBuild::Options;
use PGBuild::SCM;
use strict;
use vars qw($VERSION); $VERSION = 'REL_4.6';
my $hooks = {
@adunstan
adunstan / dumpToc.pl
Created October 3, 2011 01:18
Dump header and full TOC from a custom PostgreSQL dump file
#!/usr/bin/perl
use strict;
use YAML;
my @formats = qw(Unknown Custom Files Tar Null Directory);
my @sections = qw(None PreData Data PostData);
my $result = [];
my $toc = [];
my %globs;
@adunstan
adunstan / viewtables.sql
Created September 1, 2011 19:11
See which tables are called in a view
CREATE OR REPLACE FUNCTION public.viewtables(viewname text)
RETURNS SETOF text
LANGUAGE plperl
AS $function$
my $viewname = shift;
my $rv = spi_exec_query("explain (format yaml, verbose) SELECT * FROM $viewname");
my $resp = $rv->{rows}[0]->{'QUERY PLAN'};
my %tbls;
while ($resp =~ /Relation Name: (".*")\n\s+Schema: (".*")\n/g)
{
@adunstan
adunstan / setcluster.sql
Created August 7, 2011 17:14
set all tables clustered by PK if there is one and no clustering index is set
do $$
declare
rec record;
cmd text;
begin
for rec in
select n.nspname, rc.relname as tbl, ic.relname as idx
from pg_index i
join pg_class ic
@adunstan
adunstan / make_type_list.sql
Created February 9, 2011 13:47
make a type list for a given type for use in with things like set returning functions, with optional alias name
create or replace function make_type_list(tablename text, typealias text default null) returns text
language plpgsql as
$$
declare
rec record;
crec record;
firstrow boolean := true;
talias text := '';