Last active
October 26, 2017 18:46
-
-
Save samuelhorwitz/0bc77a517238914512fc8cdf50d217cd to your computer and use it in GitHub Desktop.
JSON building/stored procedure/view indirection segmentation fault affecting Postgres 9 and 10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ALTER TABLE base_table ADD COLUMN new_column boolean NOT NULL DEFAULT false; | |
CREATE OR REPLACE VIEW base_table_view AS SELECT base_table.* | |
FROM base_table; | |
CREATE OR REPLACE FUNCTION get_base_table_json(el anyelement) RETURNS json | |
LANGUAGE plpgsql | |
AS $$ BEGIN | |
RETURN json_build_object( | |
'id', el.id, | |
'newVal', el.new_column, | |
'value', el.some_json | |
); | |
END; $$; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE VIEW base_table_json AS | |
SELECT base_table_view.id, | |
get_base_table_json(base_table_view.*) AS json | |
FROM base_table_view; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- PostgreSQL database dump | |
-- | |
-- Dumped from database version 9.6.5 | |
-- Dumped by pg_dump version 9.6.3 | |
SET statement_timeout = 0; | |
SET lock_timeout = 0; | |
SET idle_in_transaction_session_timeout = 0; | |
SET client_encoding = 'UTF8'; | |
SET standard_conforming_strings = on; | |
SET check_function_bodies = false; | |
SET client_min_messages = warning; | |
SET row_security = off; | |
CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog; | |
COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language'; | |
SET search_path = public, pg_catalog; | |
CREATE FUNCTION get_base_table_json(el anyelement) RETURNS json | |
LANGUAGE plpgsql | |
AS $$ BEGIN | |
RETURN json_build_object( | |
'id', el.id, | |
'value', el.some_json | |
); | |
END; $$; | |
SET default_tablespace = ''; | |
SET default_with_oids = false; | |
CREATE TABLE base_table ( | |
id bigint NOT NULL, | |
some_json jsonb | |
); | |
CREATE SEQUENCE base_table_id_seq | |
START WITH 1 | |
INCREMENT BY 1 | |
NO MINVALUE | |
NO MAXVALUE | |
CACHE 1; | |
ALTER SEQUENCE base_table_id_seq OWNED BY base_table.id; | |
CREATE VIEW base_table_view AS | |
SELECT base_table.id, | |
base_table.some_json | |
FROM base_table; | |
CREATE VIEW base_table_json AS | |
SELECT base_table_view.id, | |
get_base_table_json(base_table_view.*) AS json | |
FROM base_table_view; | |
ALTER TABLE ONLY base_table ALTER COLUMN id SET DEFAULT nextval('base_table_id_seq'::regclass); | |
INSERT INTO base_table VALUES (24885, '{"foo": "bar"}'); | |
SELECT pg_catalog.setval('base_table_id_seq', 1, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to reproduce
initial.sql
SELECT id, json FROM base_table_json
and observe the result being correctly returned.break-it.sql
SELECT id, json FROM base_table_json
and observe the segmentation fault occur.fix-it.sql
SELECT id, json FROM base_table_json
and observe everything working again.Note that
fix-it.sql
does nothing except recreates the view exactly as it was before (but likely flushing out some cache or something behind the scenes in doing so).Sample log of expected failure: