Skip to content

Instantly share code, notes, and snippets.

View ringerc's full-sized avatar

Craig Ringer ringerc

View GitHub Profile
CREATE SCHEMA myschema;
CREATE TABLE myschema.zzafter(id integer);
CREATE OR REPLACE FUNCTION myschema.aabefore()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
somefield myschema.zzafter.id%TYPE;
From 0319a7ecbab5c1e85e300d93f674087786be144a Mon Sep 17 00:00:00 2001
From: Craig Ringer <craig@2ndquadrant.com>
Date: Wed, 1 Apr 2015 10:46:29 +0800
Subject: [PATCH] pg_restore -t should select views, matviews, and foreign
tables
Currently pg_restore's '-t' option selects only tables, not other
relations. It should be able to match anything that behaves like
a relation in the relation namespace, anything that's interchangable
with a table, including:
@ringerc
ringerc / disconnect.py
Created October 3, 2015 13:40
Compare disconnect/reconnect and pooled times
#!/usr/bin/env python
#
# Disconnect/reconnect each query
#
# Takes 0.45s to run here
#
import time
import psycopg2
start_t = time.time()
@ringerc
ringerc / jpegify.py
Last active December 10, 2015 04:13
#!python
# coding=utf-8
#
# usage: python jpegify.py *.pdf
#
# Written for Python 2 or python 3
from __future__ import print_function
import os
@ringerc
ringerc / gist:5742942
Created June 9, 2013 09:33
Backtrace of execution of simple PL/PgSQL array access
craig=> DO
$$
BEGIN
PERFORM (ARRAY[1])[1];
END;
$$ LANGUAGE plpgsql;
with a breakpoint set at ExecEvalArrayRef results in:
(gdb) bt
@ringerc
ringerc / gist:7413509
Last active December 28, 2015 00:29
PostgreSQL parse, rewrite, and plan trees for a `delete ... using statement` in Pg 9.3; see https://wiki.postgresql.org/wiki/Automatically_updatable_security_barrier_views#How_does_updating_a_join_work.3F
regress=> DELETE FROM t USING t2 WHERE t.id = t2.id;
LOG: parse tree:
DETAIL: {QUERY
:commandType 4
:querySource 0
:canSetTag true
:utilityStmt <>
:resultRelation 1
:hasAggs false
:hasWindowFuncs false
regress=> DELETE FROM t;
LOG: parse tree:
DETAIL: {QUERY
:commandType 4
:querySource 0
:canSetTag true
:utilityStmt <>
:resultRelation 1
:hasAggs false
:hasWindowFuncs false
@ringerc
ringerc / gist:7423819
Last active December 28, 2015 01:49
Diff of parse, rewritten and plan trees from "DELETE FROM t;" against "DELETE FROM t USING t2 WHERE t.id = t2.id;"
--- /tmp/simple-delete 2013-11-12 09:26:55.077406915 +0800
+++ /tmp/join-delete 2013-11-12 09:49:06.172045074 +0800
@@ -1,4 +1,4 @@
-regress=> DELETE FROM t;
+regress=> DELETE FROM t USING t2 WHERE t.id = t2.id;
LOG: parse tree:
DETAIL: {QUERY
:commandType 4
@@ -28,9 +28,27 @@
:lateral false
@ringerc
ringerc / gist:7429182
Created November 12, 2013 11:04
Parse, rewrite and plan trees of a simple SELECT from a view, showing subquery expansion
regress=> SELECT * FROM t_even;
LOG: parse tree:
DETAIL: {QUERY
:commandType 1
:querySource 0
:canSetTag true
:utilityStmt <>
:resultRelation 0
:hasAggs false
:hasWindowFuncs false
@ringerc
ringerc / pg_elephant.c
Created May 18, 2016 13:16
pg_elephant()
#include "postgres.h"
#include "fmgr.h"
PG_MODULE_MAGIC;
PGDLLEXPORT Datum pg_elephant(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pg_elephant);
static const char * const elephant =
"............MMMMMMMMMMM8..........MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM ...........\n"