Skip to content

Instantly share code, notes, and snippets.

WITH RECURSIVE search_graph(id, link, data, depth, path, cycle) AS (
SELECT g.id, g.link, g.data, 1,
ARRAY[g.id],
false
FROM graph g
UNION ALL
SELECT g.id, g.link, g.data, sg.depth + 1,
path || g.id,
g.id = ANY(path)
FROM graph g, search_graph sg
BEGIN;
LOCK TABLE new_table IN ACCESS EXCLUSIVE MODE;
CREATE TABLE new_table_clean AS
(SELECT DISTINCT ON(column1, column2) * FROM new_table);
TRUNCATE TABLE new_table;
INSERT INTO new_table SELECT * FROM new_table_clean;
END;
CREATE OR REPLACE FUNCTION some_function(some_ids UUID[])
RETURNS TABLE (id INTEGER)
LANGUAGE SQL
AS $$
-- ERROR: function some_function(record[]) does not exist
CREATE OR REPLACE FUNCTION some_function(VARIADIC some_ids UUID[])
RETURNS TABLE (id INTEGER)
LANGUAGE SQL
-- Source: https://www.cybertec-postgresql.com/
SELECT
relname, seq_scan, seq_tup_read,
idx_scan, idx_tup_fetch,
seq_tup_read / seq_scan
FROM pg_stat_user_tables
WHERE seq_scan > 0 AND schemaname='myschema'
ORDER BY seq_tup_read DESC;
-- Richtige Einfügereihenfolge: https://www.cybertec-postgresql.com/en/postgresql-foreign-keys-and-insertion-order-in-sql/
List<Integer> nodeIds = session.createSQLQuery("SELECT get_graph(3)").list();
MultiIdentifierLoadAccess<NodesEntity> multiLoadAccess = session.byMultipleIds(NodesEntity.class);
List<NodesEntity> nodes = multiLoadAccess.multiLoad(nodeIds);
for (NodesEntity node:nodes) {
System.out.println(node.getNodeName());
for(EdgesEntity edge : node.edges) {
System.out.println(String.format(" %s (%d) -> %s (%d) %s",
CREATE INDEX edges_from_node_id_to_node_id_idx ON playground.edges(from_node_id,to_node_id);
INSERT INTO playground.nodes (node_name) VALUES(chr(generate_series(65,75))); -- A..K
INSERT INTO playground.edges (from_node_id, to_node_id, type)
VALUES
(1,2,'parent'),(2,3,'parent'),(3,1,'parent'),(3,10,'parent'),(10,3,'child'),
(7,8,'parent'),(8,7,'child');
CREATE OR REPLACE FUNCTION get_graph(node_id INTEGER)
@Entity
@Table(name = "nodes", schema = "playground", catalog = "postgres")
public class NodesEntity {
@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Basic
@Column(name = "node_name", nullable = false, length = 10)
CREATE SCHEMA IF NOT EXISTS playground;
DROP TABLE IF EXISTS playground.edges;
DROP TABLE IF EXISTS playground.nodes;
CREATE TABLE playground.nodes
(
id SERIAL PRIMARY KEY,
node_name VARCHAR(10) NOT NULL
);
version: "3.1"
services:
db:
image: postgres
command: ["postgres", "-c", "logging_collector=on", "-c", "log_directory=/var/log/postgresql", "-c", "log_filename=postgresql.log", "-c", "log_statement=all"]
restart: always
environment:
POSTGRES_PASSWORD: postgres
volumes:
internal sealed class PhoneBook {
private String m_pathname; // path name of file containing the address book
// Other methods go here.
public String GetPhoneNumber(String name) {
String phone;
FileStream fs = null;
try {
fs = new FileStream(m_pathname, FileMode.Open);
// Code to read from fs until name is found goes here