WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), deletees AS (
                 SELECT *
                 FROM UNNEST($2::integer[]) as deletees (id)
             ), old_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_128ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, deletees
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = deletees.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM deletees
                 WHERE feature_collection_id = $1
                 AND features.id = deletees.id
                 AND is_alive
             )
             SELECT id, geography, properties, is_alive
             FROM old_features
            ;
WITH existing_feature AS (
                 SELECT created_from, geographies.geography, properties, feature_type
                 FROM features, geographies
                 WHERE features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND features.id = $2
                 AND is_alive
                 AND features.geography_id = geographies.id
             ), new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), new_geo AS (
                 INSERT INTO geographies (feature_collection_id, geography)
                 SELECT $1, ST_Buffer(geography, $3)
                 FROM existing_feature
                 RETURNING id, geography, geographies.geography_simplified_64ud AS simplified_geography
             ), new_feature AS (
                 INSERT INTO features (feature_collection_id, geography_id, extent, undo_stack_index, properties)
                 VALUES (
                     $1,
                     (SELECT id FROM new_geo),
                     (SELECT Box2D(geography::geometry)::geometry FROM new_geo),
                     (SELECT undo_stack_height FROM new_undo_stack_height),
                     json_build_object(
                         'zindex', (SELECT COALESCE(max(z_index) + 1, 0) FROM features WHERE feature_collection_id = $1),
                         'featureType', 'BufferZone',
                         'createdFrom', (SELECT format('%s (from %s)', COALESCE(properties ->> 'title', feature_type::text), created_from) FROM existing_feature),
                         'title', $4::text,
                         'description', $5::text
                     )
                 )
                 RETURNING id, properties, is_alive
             )
             SELECT new_feature.id, new_feature.properties, new_feature.is_alive, ST_AsGeoJSON(new_geo.simplified_geography)::jsonb AS geography
             FROM new_feature, new_geo
            ;
WITH preundo_undo_stack_height AS (
                 SELECT undo_stack_height
                 FROM feature_collections
                 WHERE id = $1
                 AND is_alive
                 AND undo_stack_height > min_undo_stack_height
             ), features_to_be_undone AS (
                 SELECT id, geography_id, properties, is_alive
                 FROM features
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM preundo_undo_stack_height)
             ), newest_historic_versions AS (
                 SELECT id, max(upper(sys_period)) as upper_sys_period
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index < (SELECT undo_stack_height FROM preundo_undo_stack_height)
                 AND id IN (SELECT id FROM features_to_be_undone)
                 GROUP BY id
             ), historic_features AS (
                 SELECT fh.*
                 FROM features_history AS fh, newest_historic_versions AS nhv
                 WHERE feature_collection_id = $1
                 AND fh.id = nhv.id
                 AND upper(fh.sys_period) = nhv.upper_sys_period
             ), uncreate_features AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     -- Imagine that all possible features exists at undo_stack_height = 0 with `is_alive = FALSE`.
                     undo_stack_index = 0
                 WHERE feature_collection_id = $1
                 AND is_alive
                 AND id IN (SELECT id FROM features_to_be_undone)
                 AND id NOT IN (SELECT id FROM historic_features)
                 RETURNING id
             ), set_feature_collection_data AS (
                 UPDATE feature_collections
                 SET undo_stack_height = (SELECT undo_stack_height FROM preundo_undo_stack_height) - 1,
                     num_possible_redos = num_possible_redos + 1
                 WHERE id = $1
                 AND is_alive
                 AND undo_stack_height > min_undo_stack_height
             ), update_features AS (
                 UPDATE features
                 SET geography_id = historic_features.geography_id,
                     extent = historic_features.extent,
                     properties = historic_features.properties,
                     undo_stack_index = historic_features.undo_stack_index,
                     is_alive = historic_features.is_alive
                 FROM historic_features
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 RETURNING features.id, features.geography_id, features.properties, features.is_alive
             )
             SELECT 'post' AS typ, update_features.id, ST_AsGeoJSON(geographies.geography_simplified_512ud)::jsonb AS geography, properties, is_alive
             FROM update_features, geographies
             WHERE update_features.geography_id = geographies.id
             AND geographies.feature_collection_id = $1
             UNION SELECT 'pre' AS typ, ftbu.id, ST_AsGeoJSON(geographies.geography_simplified_512ud)::jsonb AS geography, ftbu.properties, ftbu.is_alive
             FROM features_to_be_undone AS ftbu, geographies
             WHERE ftbu.geography_id = geographies.id
             AND geographies.feature_collection_id = $1
             UNION SELECT 'post' AS typ, id, '{}'::jsonb as geography, '{}'::jsonb as properties, FALSE as is_alive
             FROM uncreate_features
            ;
WITH postredo_undo_stack_height AS (
                 SELECT undo_stack_height + 1 as undo_stack_height
                 FROM feature_collections
                 WHERE id = $1
                 AND is_alive
                 AND num_possible_redos > 0
             ), update_data AS (
                 SELECT max(upper(sys_period)) AS update_time
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM postredo_undo_stack_height)
             ), historic_features AS (
                 SELECT *
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM postredo_undo_stack_height)
                 AND upper(sys_period) = (SELECT update_time FROM update_data)
             ), set_feature_collection_data AS (
                 UPDATE feature_collections
                 SET undo_stack_height = (SELECT undo_stack_height FROM postredo_undo_stack_height),
                     num_possible_redos = num_possible_redos - 1
                 WHERE id = $1
                 AND is_alive
                 AND num_possible_redos > 0
             ), previous_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_512ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM features, historic_features, geographies
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND geographies.id = features.geography_id
             ), update_features AS (
                 UPDATE features
                 SET geography_id = historic_features.geography_id,
                     extent = historic_features.extent,
                     properties = historic_features.properties,
                     undo_stack_index = historic_features.undo_stack_index,
                     is_alive = historic_features.is_alive
                 FROM historic_features, geographies
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND geographies.id = historic_features.geography_id
                 RETURNING features.id, ST_AsGeoJSON(geographies.geography_simplified_512ud)::jsonb AS geography, features.properties, features.is_alive
             )
             SELECT 'post' AS typ, id, geography, properties, is_alive
             FROM update_features
             UNION SELECT 'pre' AS typ, id, geography, properties, is_alive
             FROM previous_features
            ;
SELECT feature_type, COUNT(*) AS count
             FROM features
             WHERE features.feature_collection_id = $1
             AND features.is_alive
             AND features.feature_type <> $2
             GROUP BY feature_type
            ;
SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_16ud)::jsonb AS geography, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
             AND (
                 $2::float[] IS NULL
                 OR ST_Intersects(extent, ST_MakeEnvelope($2[1], $2[2], $2[3], $2[4], 4326))
             )
             AND features.feature_type <> $3
             AND (
                 $4::int[] IS NULL
                 OR features.id = ANY($4)
             )
             AND (
                 $5::int[] IS NULL
                 OR features.id <> ALL($5)
             )
             AND (
                 $6::text[] IS NULL
                 OR features.feature_type = ANY($6)
             )
             AND (
                 $7::text[] IS NULL
                 OR features.feature_type <> ALL($7)
             )
             ORDER BY z_index
            ;
SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_64ud)::jsonb AS geography, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.id = $2
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), deletees AS (
                 SELECT *
                 FROM UNNEST($2::integer[]) as deletees (id)
             ), old_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_512ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, deletees
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = deletees.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM deletees
                 WHERE feature_collection_id = $1
                 AND features.id = deletees.id
                 AND is_alive
             )
             SELECT id, geography, properties, is_alive
             FROM old_features
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), old AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_64ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = $2
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             )
             UPDATE features
             SET geography_id = COALESCE($3, features.geography_id),
                 extent = COALESCE(ST_GeomFromEWKB($4::bytea), features.extent),
                 properties = COALESCE($5::jsonb , features.properties),
                 undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
             FROM old
             WHERE features.id = $2
             AND features.feature_collection_id = $1
             AND features.is_alive
             RETURNING old.id, old.geography, old.properties, old.is_alive
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0,
                     min_undo_stack_height = CASE WHEN $14 THEN undo_stack_height + 1 ELSE min_undo_stack_height END
                 WHERE id = $1
                 AND is_alive
                 RETURNING undo_stack_height
             ), the_creation AS (
                 INSERT INTO features (feature_collection_id, geography_id, extent, properties, undo_stack_index)
                 SELECT
                     $1,
                     geography_id,
                     ST_GeomFromEWKB(extent),
                     properties,
                     (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM UNNEST($2::integer[], $3::bytea[], $4::jsonb[]) AS nf (geography_id, extent, properties)
                 RETURNING id, is_alive
             ), feature_update_params AS (
                 SELECT *
                 FROM UNNEST($5::integer[], $6::bool[], $7::integer[], $8::bytea[], $9::bool[], $10::jsonb[], $11::bool[], $12::integer[]) AS nv
                     (id, should_update_geography, geography_id, extent, should_update_properties, properties, should_update_z_index, z_index)
             ), pre_update_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, feature_update_params AS nv
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = nv.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             ), the_update_and_reorder AS (
                 UPDATE features
                 SET geography_id = CASE WHEN nv.should_update_geography THEN nv.geography_id ELSE features.geography_id END,
                     extent = CASE WHEN nv.should_update_geography THEN ST_GeomFromEWKB(nv.extent) ELSE features.extent END,
                     -- We construct the new properties by first
                     -- considering if request.update wants to change them
                     -- and then applying request.reorder if needed. `||`
                     -- merges two JSON objects.
                     properties = CASE WHEN nv.should_update_properties THEN nv.properties ELSE features.properties END
                               || CASE WHEN nv.should_update_z_index THEN jsonb_build_object('zindex', nv.z_index) ELSE '{}'::jsonb END,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM feature_update_params AS nv
                 WHERE features.id = nv.id
                 AND feature_collection_id = $1
                 AND is_alive
                 AND (nv.should_update_geography OR nv.should_update_properties OR nv.should_update_z_index)
             ), deletees AS (
                 SELECT *
                 FROM UNNEST($13::integer[]) as deletees (id)
             ), pre_deletion_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, deletees
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = deletees.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM deletees
                 WHERE feature_collection_id = $1
                 AND features.id = deletees.id
                 AND is_alive
             ), pre_update_collection_properties AS (
                 SELECT properties -> 'collection_properties' as properties, is_alive
                 FROM features
                 WHERE $15::jsonb IS NOT NULL
                 AND feature_collection_id = $1
                 AND is_alive
                 AND feature_type = $16
             ), the_collection_properties_update AS (
                 UPDATE features
                 SET properties = jsonb_set(properties, '{collection_properties}', $15),
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 WHERE $15::jsonb IS NOT NULL
                 AND feature_collection_id = $1
                 AND is_alive
                 AND feature_type = $16
             )
             SELECT 'created' AS typ, id, NULL AS geography, NULL AS properties, is_alive, (row_number() OVER ())::integer AS row_idx
             FROM the_creation
             UNION SELECT 'updated' AS typ, id, geography, properties, is_alive, -1 AS row_idx
             FROM pre_update_features
             UNION SELECT 'deleted' AS typ, id, geography, properties, is_alive, -2 AS row_idx
             FROM pre_deletion_features
             UNION SELECT 'collection_properties' AS typ, NULL AS id, NULL AS geography, properties, is_alive, -3 AS row_idx
             FROM pre_update_collection_properties
             ORDER BY row_idx
            ;
SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_16ud)::jsonb AS geography, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.id = $2
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), old AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_128ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = $2
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             )
             UPDATE features
             SET geography_id = COALESCE($3, features.geography_id),
                 extent = COALESCE(ST_GeomFromEWKB($4::bytea), features.extent),
                 properties = COALESCE($5::jsonb , features.properties),
                 undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
             FROM old
             WHERE features.id = $2
             AND features.feature_collection_id = $1
             AND features.is_alive
             RETURNING old.id, old.geography, old.properties, old.is_alive
            ;
SELECT EXISTS (
             SELECT 1
             FROM feature_collections, features
             WHERE feature_collections.id = $1
             AND features.id = $2
             AND features.is_alive
             AND feature_collections.is_alive
         ) as is_alive;
WITH existing_feature AS (
                 SELECT created_from, geographies.geography, properties, feature_type
                 FROM features, geographies
                 WHERE features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND features.id = $2
                 AND is_alive
                 AND features.geography_id = geographies.id
             ), new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), new_geo AS (
                 INSERT INTO geographies (feature_collection_id, geography)
                 SELECT $1, ST_Buffer(geography, $3)
                 FROM existing_feature
                 RETURNING id, geography, geographies.geography_simplified_8ud AS simplified_geography
             ), new_feature AS (
                 INSERT INTO features (feature_collection_id, geography_id, extent, undo_stack_index, properties)
                 VALUES (
                     $1,
                     (SELECT id FROM new_geo),
                     (SELECT Box2D(geography::geometry)::geometry FROM new_geo),
                     (SELECT undo_stack_height FROM new_undo_stack_height),
                     json_build_object(
                         'zindex', (SELECT COALESCE(max(z_index) + 1, 0) FROM features WHERE feature_collection_id = $1),
                         'featureType', 'BufferZone',
                         'createdFrom', (SELECT format('%s (from %s)', COALESCE(properties ->> 'title', feature_type::text), created_from) FROM existing_feature),
                         'title', $4::text,
                         'description', $5::text
                     )
                 )
                 RETURNING id, properties, is_alive
             )
             SELECT new_feature.id, new_feature.properties, new_feature.is_alive, ST_AsGeoJSON(new_geo.simplified_geography)::jsonb AS geography
             FROM new_feature, new_geo
            ;
SELECT features.id, ST_AsEWKB(geographies.geography_simplified_16ud::geometry) AS geography_ewkb, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
             AND (
                 $2::float[] IS NULL
                 OR ST_Intersects(extent, ST_MakeEnvelope($2[1], $2[2], $2[3], $2[4], 4326))
             )
             AND features.feature_type <> $3
             AND (
                 $4::int[] IS NULL
                 OR features.id = ANY($4)
             )
             AND (
                 $5::int[] IS NULL
                 OR features.id <> ALL($5)
             )
             AND (
                 $6::text[] IS NULL
                 OR features.feature_type = ANY($6)
             )
             AND (
                 $7::text[] IS NULL
                 OR features.feature_type <> ALL($7)
             )
             ORDER BY z_index
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), deletees AS (
                 SELECT *
                 FROM UNNEST($2::integer[]) as deletees (id)
             ), old_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_32ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, deletees
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = deletees.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM deletees
                 WHERE feature_collection_id = $1
                 AND features.id = deletees.id
                 AND is_alive
             )
             SELECT id, geography, properties, is_alive
             FROM old_features
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), nv AS (
                 SELECT *
                 FROM UNNEST($2::integer[], $3::bool[], $4::integer[], $5::bytea[], $6::bool[], $7::jsonb[]) AS nv
                     (id, should_update_geography, geography_id, extent, should_update_properties, properties)
             ), old_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_8ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, nv
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = nv.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             ), the_update AS (
                 UPDATE features
                 SET geography_id = CASE WHEN nv.should_update_geography THEN nv.geography_id ELSE features.geography_id END,
                     extent = CASE WHEN nv.should_update_geography THEN ST_GeomFromEWKB(nv.extent) ELSE features.extent END,
                     properties = CASE WHEN nv.should_update_properties THEN nv.properties ELSE features.properties END,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM nv
                 WHERE features.id = nv.id
                 AND feature_collection_id = $1
                 AND is_alive
                 AND (nv.should_update_geography OR nv.should_update_properties)
             )
             SELECT id, geography, properties, is_alive
             FROM old_features
            ;
SELECT id, created_at, last_modified FROM feature_collections WHERE is_alive;
WITH new_undo_stack_height AS (
             UPDATE feature_collections
             SET undo_stack_height = undo_stack_height + 1,
                 num_possible_redos = 0
             WHERE id = $1 AND is_alive
             RETURNING undo_stack_height
         ), old AS (
             SELECT properties -> 'collection_properties' as properties
             FROM features
             WHERE feature_collection_id = $1
             AND is_alive
             AND feature_type = $3
         ), the_update AS (
             UPDATE features
             SET properties = jsonb_set(properties, '{collection_properties}', $2),
                 undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
             WHERE feature_collection_id = $1
             AND is_alive
             AND feature_type = $3
         )
         SELECT properties
         FROM old
        ;
WITH postredo_undo_stack_height AS (
                 SELECT undo_stack_height + 1 as undo_stack_height
                 FROM feature_collections
                 WHERE id = $1
                 AND is_alive
                 AND num_possible_redos > 0
             ), update_data AS (
                 SELECT max(upper(sys_period)) AS update_time
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM postredo_undo_stack_height)
             ), historic_features AS (
                 SELECT *
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM postredo_undo_stack_height)
                 AND upper(sys_period) = (SELECT update_time FROM update_data)
             ), set_feature_collection_data AS (
                 UPDATE feature_collections
                 SET undo_stack_height = (SELECT undo_stack_height FROM postredo_undo_stack_height),
                     num_possible_redos = num_possible_redos - 1
                 WHERE id = $1
                 AND is_alive
                 AND num_possible_redos > 0
             ), previous_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_128ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM features, historic_features, geographies
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND geographies.id = features.geography_id
             ), update_features AS (
                 UPDATE features
                 SET geography_id = historic_features.geography_id,
                     extent = historic_features.extent,
                     properties = historic_features.properties,
                     undo_stack_index = historic_features.undo_stack_index,
                     is_alive = historic_features.is_alive
                 FROM historic_features, geographies
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND geographies.id = historic_features.geography_id
                 RETURNING features.id, ST_AsGeoJSON(geographies.geography_simplified_128ud)::jsonb AS geography, features.properties, features.is_alive
             )
             SELECT 'post' AS typ, id, geography, properties, is_alive
             FROM update_features
             UNION SELECT 'pre' AS typ, id, geography, properties, is_alive
             FROM previous_features
            ;
SELECT features.id, ST_AsEWKB(geographies.geography_simplified_128ud::geometry) AS geography_ewkb, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
             AND (
                 $2::float[] IS NULL
                 OR ST_Intersects(extent, ST_MakeEnvelope($2[1], $2[2], $2[3], $2[4], 4326))
             )
             AND features.feature_type <> $3
             AND (
                 $4::int[] IS NULL
                 OR features.id = ANY($4)
             )
             AND (
                 $5::int[] IS NULL
                 OR features.id <> ALL($5)
             )
             AND (
                 $6::text[] IS NULL
                 OR features.feature_type = ANY($6)
             )
             AND (
                 $7::text[] IS NULL
                 OR features.feature_type <> ALL($7)
             )
             ORDER BY z_index
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), nv AS (
                 SELECT *
                 FROM UNNEST($2::integer[], $3::bool[], $4::integer[], $5::bytea[], $6::bool[], $7::jsonb[]) AS nv
                     (id, should_update_geography, geography_id, extent, should_update_properties, properties)
             ), old_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_32ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, nv
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = nv.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             ), the_update AS (
                 UPDATE features
                 SET geography_id = CASE WHEN nv.should_update_geography THEN nv.geography_id ELSE features.geography_id END,
                     extent = CASE WHEN nv.should_update_geography THEN ST_GeomFromEWKB(nv.extent) ELSE features.extent END,
                     properties = CASE WHEN nv.should_update_properties THEN nv.properties ELSE features.properties END,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM nv
                 WHERE features.id = nv.id
                 AND feature_collection_id = $1
                 AND is_alive
                 AND (nv.should_update_geography OR nv.should_update_properties)
             )
             SELECT id, geography, properties, is_alive
             FROM old_features
            ;
WITH preundo_undo_stack_height AS (
                 SELECT undo_stack_height
                 FROM feature_collections
                 WHERE id = $1
                 AND is_alive
                 AND undo_stack_height > min_undo_stack_height
             ), features_to_be_undone AS (
                 SELECT id, geography_id, properties, is_alive
                 FROM features
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM preundo_undo_stack_height)
             ), newest_historic_versions AS (
                 SELECT id, max(upper(sys_period)) as upper_sys_period
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index < (SELECT undo_stack_height FROM preundo_undo_stack_height)
                 AND id IN (SELECT id FROM features_to_be_undone)
                 GROUP BY id
             ), historic_features AS (
                 SELECT fh.*
                 FROM features_history AS fh, newest_historic_versions AS nhv
                 WHERE feature_collection_id = $1
                 AND fh.id = nhv.id
                 AND upper(fh.sys_period) = nhv.upper_sys_period
             ), uncreate_features AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     -- Imagine that all possible features exists at undo_stack_height = 0 with `is_alive = FALSE`.
                     undo_stack_index = 0
                 WHERE feature_collection_id = $1
                 AND is_alive
                 AND id IN (SELECT id FROM features_to_be_undone)
                 AND id NOT IN (SELECT id FROM historic_features)
                 RETURNING id
             ), set_feature_collection_data AS (
                 UPDATE feature_collections
                 SET undo_stack_height = (SELECT undo_stack_height FROM preundo_undo_stack_height) - 1,
                     num_possible_redos = num_possible_redos + 1
                 WHERE id = $1
                 AND is_alive
                 AND undo_stack_height > min_undo_stack_height
             ), update_features AS (
                 UPDATE features
                 SET geography_id = historic_features.geography_id,
                     extent = historic_features.extent,
                     properties = historic_features.properties,
                     undo_stack_index = historic_features.undo_stack_index,
                     is_alive = historic_features.is_alive
                 FROM historic_features
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 RETURNING features.id, features.geography_id, features.properties, features.is_alive
             )
             SELECT 'post' AS typ, update_features.id, ST_AsGeoJSON(geographies.geography_simplified_8ud)::jsonb AS geography, properties, is_alive
             FROM update_features, geographies
             WHERE update_features.geography_id = geographies.id
             AND geographies.feature_collection_id = $1
             UNION SELECT 'pre' AS typ, ftbu.id, ST_AsGeoJSON(geographies.geography_simplified_8ud)::jsonb AS geography, ftbu.properties, ftbu.is_alive
             FROM features_to_be_undone AS ftbu, geographies
             WHERE ftbu.geography_id = geographies.id
             AND geographies.feature_collection_id = $1
             UNION SELECT 'post' AS typ, id, '{}'::jsonb as geography, '{}'::jsonb as properties, FALSE as is_alive
             FROM uncreate_features
            ;
WITH preundo_undo_stack_height AS (
                 SELECT undo_stack_height
                 FROM feature_collections
                 WHERE id = $1
                 AND is_alive
                 AND undo_stack_height > min_undo_stack_height
             ), features_to_be_undone AS (
                 SELECT id, geography_id, properties, is_alive
                 FROM features
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM preundo_undo_stack_height)
             ), newest_historic_versions AS (
                 SELECT id, max(upper(sys_period)) as upper_sys_period
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index < (SELECT undo_stack_height FROM preundo_undo_stack_height)
                 AND id IN (SELECT id FROM features_to_be_undone)
                 GROUP BY id
             ), historic_features AS (
                 SELECT fh.*
                 FROM features_history AS fh, newest_historic_versions AS nhv
                 WHERE feature_collection_id = $1
                 AND fh.id = nhv.id
                 AND upper(fh.sys_period) = nhv.upper_sys_period
             ), uncreate_features AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     -- Imagine that all possible features exists at undo_stack_height = 0 with `is_alive = FALSE`.
                     undo_stack_index = 0
                 WHERE feature_collection_id = $1
                 AND is_alive
                 AND id IN (SELECT id FROM features_to_be_undone)
                 AND id NOT IN (SELECT id FROM historic_features)
                 RETURNING id
             ), set_feature_collection_data AS (
                 UPDATE feature_collections
                 SET undo_stack_height = (SELECT undo_stack_height FROM preundo_undo_stack_height) - 1,
                     num_possible_redos = num_possible_redos + 1
                 WHERE id = $1
                 AND is_alive
                 AND undo_stack_height > min_undo_stack_height
             ), update_features AS (
                 UPDATE features
                 SET geography_id = historic_features.geography_id,
                     extent = historic_features.extent,
                     properties = historic_features.properties,
                     undo_stack_index = historic_features.undo_stack_index,
                     is_alive = historic_features.is_alive
                 FROM historic_features
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 RETURNING features.id, features.geography_id, features.properties, features.is_alive
             )
             SELECT 'post' AS typ, update_features.id, ST_AsGeoJSON(geographies.geography_simplified_64ud)::jsonb AS geography, properties, is_alive
             FROM update_features, geographies
             WHERE update_features.geography_id = geographies.id
             AND geographies.feature_collection_id = $1
             UNION SELECT 'pre' AS typ, ftbu.id, ST_AsGeoJSON(geographies.geography_simplified_64ud)::jsonb AS geography, ftbu.properties, ftbu.is_alive
             FROM features_to_be_undone AS ftbu, geographies
             WHERE ftbu.geography_id = geographies.id
             AND geographies.feature_collection_id = $1
             UNION SELECT 'post' AS typ, id, '{}'::jsonb as geography, '{}'::jsonb as properties, FALSE as is_alive
             FROM uncreate_features
            ;
WITH existing_feature AS (
                 SELECT created_from, geographies.geography, properties, feature_type
                 FROM features, geographies
                 WHERE features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND features.id = $2
                 AND is_alive
                 AND features.geography_id = geographies.id
             ), new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), new_geo AS (
                 INSERT INTO geographies (feature_collection_id, geography)
                 SELECT $1, ST_Buffer(geography, $3)
                 FROM existing_feature
                 RETURNING id, geography, geographies.geography_simplified_128ud AS simplified_geography
             ), new_feature AS (
                 INSERT INTO features (feature_collection_id, geography_id, extent, undo_stack_index, properties)
                 VALUES (
                     $1,
                     (SELECT id FROM new_geo),
                     (SELECT Box2D(geography::geometry)::geometry FROM new_geo),
                     (SELECT undo_stack_height FROM new_undo_stack_height),
                     json_build_object(
                         'zindex', (SELECT COALESCE(max(z_index) + 1, 0) FROM features WHERE feature_collection_id = $1),
                         'featureType', 'BufferZone',
                         'createdFrom', (SELECT format('%s (from %s)', COALESCE(properties ->> 'title', feature_type::text), created_from) FROM existing_feature),
                         'title', $4::text,
                         'description', $5::text
                     )
                 )
                 RETURNING id, properties, is_alive
             )
             SELECT new_feature.id, new_feature.properties, new_feature.is_alive, ST_AsGeoJSON(new_geo.simplified_geography)::jsonb AS geography
             FROM new_feature, new_geo
            ;
WITH preundo_undo_stack_height AS (
                 SELECT undo_stack_height
                 FROM feature_collections
                 WHERE id = $1
                 AND is_alive
                 AND undo_stack_height > min_undo_stack_height
             ), features_to_be_undone AS (
                 SELECT id, geography_id, properties, is_alive
                 FROM features
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM preundo_undo_stack_height)
             ), newest_historic_versions AS (
                 SELECT id, max(upper(sys_period)) as upper_sys_period
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index < (SELECT undo_stack_height FROM preundo_undo_stack_height)
                 AND id IN (SELECT id FROM features_to_be_undone)
                 GROUP BY id
             ), historic_features AS (
                 SELECT fh.*
                 FROM features_history AS fh, newest_historic_versions AS nhv
                 WHERE feature_collection_id = $1
                 AND fh.id = nhv.id
                 AND upper(fh.sys_period) = nhv.upper_sys_period
             ), uncreate_features AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     -- Imagine that all possible features exists at undo_stack_height = 0 with `is_alive = FALSE`.
                     undo_stack_index = 0
                 WHERE feature_collection_id = $1
                 AND is_alive
                 AND id IN (SELECT id FROM features_to_be_undone)
                 AND id NOT IN (SELECT id FROM historic_features)
                 RETURNING id
             ), set_feature_collection_data AS (
                 UPDATE feature_collections
                 SET undo_stack_height = (SELECT undo_stack_height FROM preundo_undo_stack_height) - 1,
                     num_possible_redos = num_possible_redos + 1
                 WHERE id = $1
                 AND is_alive
                 AND undo_stack_height > min_undo_stack_height
             ), update_features AS (
                 UPDATE features
                 SET geography_id = historic_features.geography_id,
                     extent = historic_features.extent,
                     properties = historic_features.properties,
                     undo_stack_index = historic_features.undo_stack_index,
                     is_alive = historic_features.is_alive
                 FROM historic_features
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 RETURNING features.id, features.geography_id, features.properties, features.is_alive
             )
             SELECT 'post' AS typ, update_features.id, ST_AsGeoJSON(geographies.geography)::jsonb AS geography, properties, is_alive
             FROM update_features, geographies
             WHERE update_features.geography_id = geographies.id
             AND geographies.feature_collection_id = $1
             UNION SELECT 'pre' AS typ, ftbu.id, ST_AsGeoJSON(geographies.geography)::jsonb AS geography, ftbu.properties, ftbu.is_alive
             FROM features_to_be_undone AS ftbu, geographies
             WHERE ftbu.geography_id = geographies.id
             AND geographies.feature_collection_id = $1
             UNION SELECT 'post' AS typ, id, '{}'::jsonb as geography, '{}'::jsonb as properties, FALSE as is_alive
             FROM uncreate_features
            ;
SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_256ud)::jsonb AS geography, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
             AND (
                 $2::float[] IS NULL
                 OR ST_Intersects(extent, ST_MakeEnvelope($2[1], $2[2], $2[3], $2[4], 4326))
             )
             AND features.feature_type <> $3
             AND (
                 $4::int[] IS NULL
                 OR features.id = ANY($4)
             )
             AND (
                 $5::int[] IS NULL
                 OR features.id <> ALL($5)
             )
             AND (
                 $6::text[] IS NULL
                 OR features.feature_type = ANY($6)
             )
             AND (
                 $7::text[] IS NULL
                 OR features.feature_type <> ALL($7)
             )
             ORDER BY z_index
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), deletees AS (
                 SELECT *
                 FROM UNNEST($2::integer[]) as deletees (id)
             ), old_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, deletees
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = deletees.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM deletees
                 WHERE feature_collection_id = $1
                 AND features.id = deletees.id
                 AND is_alive
             )
             SELECT id, geography, properties, is_alive
             FROM old_features
            ;
SELECT features.id, features.properties, features.is_alive
             FROM features
             WHERE features.feature_collection_id = $1
             AND features.is_alive
             AND features.feature_type <> $2
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0,
                     min_undo_stack_height = CASE WHEN $14 THEN undo_stack_height + 1 ELSE min_undo_stack_height END
                 WHERE id = $1
                 AND is_alive
                 RETURNING undo_stack_height
             ), the_creation AS (
                 INSERT INTO features (feature_collection_id, geography_id, extent, properties, undo_stack_index)
                 SELECT
                     $1,
                     geography_id,
                     ST_GeomFromEWKB(extent),
                     properties,
                     (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM UNNEST($2::integer[], $3::bytea[], $4::jsonb[]) AS nf (geography_id, extent, properties)
                 RETURNING id, is_alive
             ), feature_update_params AS (
                 SELECT *
                 FROM UNNEST($5::integer[], $6::bool[], $7::integer[], $8::bytea[], $9::bool[], $10::jsonb[], $11::bool[], $12::integer[]) AS nv
                     (id, should_update_geography, geography_id, extent, should_update_properties, properties, should_update_z_index, z_index)
             ), pre_update_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_512ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, feature_update_params AS nv
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = nv.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             ), the_update_and_reorder AS (
                 UPDATE features
                 SET geography_id = CASE WHEN nv.should_update_geography THEN nv.geography_id ELSE features.geography_id END,
                     extent = CASE WHEN nv.should_update_geography THEN ST_GeomFromEWKB(nv.extent) ELSE features.extent END,
                     -- We construct the new properties by first
                     -- considering if request.update wants to change them
                     -- and then applying request.reorder if needed. `||`
                     -- merges two JSON objects.
                     properties = CASE WHEN nv.should_update_properties THEN nv.properties ELSE features.properties END
                               || CASE WHEN nv.should_update_z_index THEN jsonb_build_object('zindex', nv.z_index) ELSE '{}'::jsonb END,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM feature_update_params AS nv
                 WHERE features.id = nv.id
                 AND feature_collection_id = $1
                 AND is_alive
                 AND (nv.should_update_geography OR nv.should_update_properties OR nv.should_update_z_index)
             ), deletees AS (
                 SELECT *
                 FROM UNNEST($13::integer[]) as deletees (id)
             ), pre_deletion_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_512ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, deletees
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = deletees.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM deletees
                 WHERE feature_collection_id = $1
                 AND features.id = deletees.id
                 AND is_alive
             ), pre_update_collection_properties AS (
                 SELECT properties -> 'collection_properties' as properties, is_alive
                 FROM features
                 WHERE $15::jsonb IS NOT NULL
                 AND feature_collection_id = $1
                 AND is_alive
                 AND feature_type = $16
             ), the_collection_properties_update AS (
                 UPDATE features
                 SET properties = jsonb_set(properties, '{collection_properties}', $15),
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 WHERE $15::jsonb IS NOT NULL
                 AND feature_collection_id = $1
                 AND is_alive
                 AND feature_type = $16
             )
             SELECT 'created' AS typ, id, NULL AS geography, NULL AS properties, is_alive, (row_number() OVER ())::integer AS row_idx
             FROM the_creation
             UNION SELECT 'updated' AS typ, id, geography, properties, is_alive, -1 AS row_idx
             FROM pre_update_features
             UNION SELECT 'deleted' AS typ, id, geography, properties, is_alive, -2 AS row_idx
             FROM pre_deletion_features
             UNION SELECT 'collection_properties' AS typ, NULL AS id, NULL AS geography, properties, is_alive, -3 AS row_idx
             FROM pre_update_collection_properties
             ORDER BY row_idx
            ;
WITH preundo_undo_stack_height AS (
                 SELECT undo_stack_height
                 FROM feature_collections
                 WHERE id = $1
                 AND is_alive
                 AND undo_stack_height > min_undo_stack_height
             ), features_to_be_undone AS (
                 SELECT id, geography_id, properties, is_alive
                 FROM features
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM preundo_undo_stack_height)
             ), newest_historic_versions AS (
                 SELECT id, max(upper(sys_period)) as upper_sys_period
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index < (SELECT undo_stack_height FROM preundo_undo_stack_height)
                 AND id IN (SELECT id FROM features_to_be_undone)
                 GROUP BY id
             ), historic_features AS (
                 SELECT fh.*
                 FROM features_history AS fh, newest_historic_versions AS nhv
                 WHERE feature_collection_id = $1
                 AND fh.id = nhv.id
                 AND upper(fh.sys_period) = nhv.upper_sys_period
             ), uncreate_features AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     -- Imagine that all possible features exists at undo_stack_height = 0 with `is_alive = FALSE`.
                     undo_stack_index = 0
                 WHERE feature_collection_id = $1
                 AND is_alive
                 AND id IN (SELECT id FROM features_to_be_undone)
                 AND id NOT IN (SELECT id FROM historic_features)
                 RETURNING id
             ), set_feature_collection_data AS (
                 UPDATE feature_collections
                 SET undo_stack_height = (SELECT undo_stack_height FROM preundo_undo_stack_height) - 1,
                     num_possible_redos = num_possible_redos + 1
                 WHERE id = $1
                 AND is_alive
                 AND undo_stack_height > min_undo_stack_height
             ), update_features AS (
                 UPDATE features
                 SET geography_id = historic_features.geography_id,
                     extent = historic_features.extent,
                     properties = historic_features.properties,
                     undo_stack_index = historic_features.undo_stack_index,
                     is_alive = historic_features.is_alive
                 FROM historic_features
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 RETURNING features.id, features.geography_id, features.properties, features.is_alive
             )
             SELECT 'post' AS typ, update_features.id, ST_AsGeoJSON(geographies.geography_simplified_16ud)::jsonb AS geography, properties, is_alive
             FROM update_features, geographies
             WHERE update_features.geography_id = geographies.id
             AND geographies.feature_collection_id = $1
             UNION SELECT 'pre' AS typ, ftbu.id, ST_AsGeoJSON(geographies.geography_simplified_16ud)::jsonb AS geography, ftbu.properties, ftbu.is_alive
             FROM features_to_be_undone AS ftbu, geographies
             WHERE ftbu.geography_id = geographies.id
             AND geographies.feature_collection_id = $1
             UNION SELECT 'post' AS typ, id, '{}'::jsonb as geography, '{}'::jsonb as properties, FALSE as is_alive
             FROM uncreate_features
            ;
SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_512ud)::jsonb AS geography, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.id = $2
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
            ;
SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_8ud)::jsonb AS geography, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
             AND (
                 $2::float[] IS NULL
                 OR ST_Intersects(extent, ST_MakeEnvelope($2[1], $2[2], $2[3], $2[4], 4326))
             )
             AND features.feature_type <> $3
             AND (
                 $4::int[] IS NULL
                 OR features.id = ANY($4)
             )
             AND (
                 $5::int[] IS NULL
                 OR features.id <> ALL($5)
             )
             AND (
                 $6::text[] IS NULL
                 OR features.feature_type = ANY($6)
             )
             AND (
                 $7::text[] IS NULL
                 OR features.feature_type <> ALL($7)
             )
             ORDER BY z_index
            ;
SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_512ud)::jsonb AS geography, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
             AND (
                 $2::float[] IS NULL
                 OR ST_Intersects(extent, ST_MakeEnvelope($2[1], $2[2], $2[3], $2[4], 4326))
             )
             AND features.feature_type <> $3
             AND (
                 $4::int[] IS NULL
                 OR features.id = ANY($4)
             )
             AND (
                 $5::int[] IS NULL
                 OR features.id <> ALL($5)
             )
             AND (
                 $6::text[] IS NULL
                 OR features.feature_type = ANY($6)
             )
             AND (
                 $7::text[] IS NULL
                 OR features.feature_type <> ALL($7)
             )
             ORDER BY z_index
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0,
                     min_undo_stack_height = CASE WHEN $14 THEN undo_stack_height + 1 ELSE min_undo_stack_height END
                 WHERE id = $1
                 AND is_alive
                 RETURNING undo_stack_height
             ), the_creation AS (
                 INSERT INTO features (feature_collection_id, geography_id, extent, properties, undo_stack_index)
                 SELECT
                     $1,
                     geography_id,
                     ST_GeomFromEWKB(extent),
                     properties,
                     (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM UNNEST($2::integer[], $3::bytea[], $4::jsonb[]) AS nf (geography_id, extent, properties)
                 RETURNING id, is_alive
             ), feature_update_params AS (
                 SELECT *
                 FROM UNNEST($5::integer[], $6::bool[], $7::integer[], $8::bytea[], $9::bool[], $10::jsonb[], $11::bool[], $12::integer[]) AS nv
                     (id, should_update_geography, geography_id, extent, should_update_properties, properties, should_update_z_index, z_index)
             ), pre_update_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_16ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, feature_update_params AS nv
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = nv.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             ), the_update_and_reorder AS (
                 UPDATE features
                 SET geography_id = CASE WHEN nv.should_update_geography THEN nv.geography_id ELSE features.geography_id END,
                     extent = CASE WHEN nv.should_update_geography THEN ST_GeomFromEWKB(nv.extent) ELSE features.extent END,
                     -- We construct the new properties by first
                     -- considering if request.update wants to change them
                     -- and then applying request.reorder if needed. `||`
                     -- merges two JSON objects.
                     properties = CASE WHEN nv.should_update_properties THEN nv.properties ELSE features.properties END
                               || CASE WHEN nv.should_update_z_index THEN jsonb_build_object('zindex', nv.z_index) ELSE '{}'::jsonb END,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM feature_update_params AS nv
                 WHERE features.id = nv.id
                 AND feature_collection_id = $1
                 AND is_alive
                 AND (nv.should_update_geography OR nv.should_update_properties OR nv.should_update_z_index)
             ), deletees AS (
                 SELECT *
                 FROM UNNEST($13::integer[]) as deletees (id)
             ), pre_deletion_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_16ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, deletees
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = deletees.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM deletees
                 WHERE feature_collection_id = $1
                 AND features.id = deletees.id
                 AND is_alive
             ), pre_update_collection_properties AS (
                 SELECT properties -> 'collection_properties' as properties, is_alive
                 FROM features
                 WHERE $15::jsonb IS NOT NULL
                 AND feature_collection_id = $1
                 AND is_alive
                 AND feature_type = $16
             ), the_collection_properties_update AS (
                 UPDATE features
                 SET properties = jsonb_set(properties, '{collection_properties}', $15),
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 WHERE $15::jsonb IS NOT NULL
                 AND feature_collection_id = $1
                 AND is_alive
                 AND feature_type = $16
             )
             SELECT 'created' AS typ, id, NULL AS geography, NULL AS properties, is_alive, (row_number() OVER ())::integer AS row_idx
             FROM the_creation
             UNION SELECT 'updated' AS typ, id, geography, properties, is_alive, -1 AS row_idx
             FROM pre_update_features
             UNION SELECT 'deleted' AS typ, id, geography, properties, is_alive, -2 AS row_idx
             FROM pre_deletion_features
             UNION SELECT 'collection_properties' AS typ, NULL AS id, NULL AS geography, properties, is_alive, -3 AS row_idx
             FROM pre_update_collection_properties
             ORDER BY row_idx
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), nv AS (
                 SELECT *
                 FROM UNNEST($2::integer[], $3::bool[], $4::integer[], $5::bytea[], $6::bool[], $7::jsonb[]) AS nv
                     (id, should_update_geography, geography_id, extent, should_update_properties, properties)
             ), old_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_16ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, nv
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = nv.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             ), the_update AS (
                 UPDATE features
                 SET geography_id = CASE WHEN nv.should_update_geography THEN nv.geography_id ELSE features.geography_id END,
                     extent = CASE WHEN nv.should_update_geography THEN ST_GeomFromEWKB(nv.extent) ELSE features.extent END,
                     properties = CASE WHEN nv.should_update_properties THEN nv.properties ELSE features.properties END,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM nv
                 WHERE features.id = nv.id
                 AND feature_collection_id = $1
                 AND is_alive
                 AND (nv.should_update_geography OR nv.should_update_properties)
             )
             SELECT id, geography, properties, is_alive
             FROM old_features
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1
                 AND is_alive
                 RETURNING undo_stack_height
             ), old AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = $2
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 WHERE id = $2
                 AND feature_collection_id = $1
                 AND is_alive
             )
             SELECT old.id, old.geography, old.properties, old.is_alive
             FROM old
            ;
WITH postredo_undo_stack_height AS (
                 SELECT undo_stack_height + 1 as undo_stack_height
                 FROM feature_collections
                 WHERE id = $1
                 AND is_alive
                 AND num_possible_redos > 0
             ), update_data AS (
                 SELECT max(upper(sys_period)) AS update_time
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM postredo_undo_stack_height)
             ), historic_features AS (
                 SELECT *
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM postredo_undo_stack_height)
                 AND upper(sys_period) = (SELECT update_time FROM update_data)
             ), set_feature_collection_data AS (
                 UPDATE feature_collections
                 SET undo_stack_height = (SELECT undo_stack_height FROM postredo_undo_stack_height),
                     num_possible_redos = num_possible_redos - 1
                 WHERE id = $1
                 AND is_alive
                 AND num_possible_redos > 0
             ), previous_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_16ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM features, historic_features, geographies
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND geographies.id = features.geography_id
             ), update_features AS (
                 UPDATE features
                 SET geography_id = historic_features.geography_id,
                     extent = historic_features.extent,
                     properties = historic_features.properties,
                     undo_stack_index = historic_features.undo_stack_index,
                     is_alive = historic_features.is_alive
                 FROM historic_features, geographies
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND geographies.id = historic_features.geography_id
                 RETURNING features.id, ST_AsGeoJSON(geographies.geography_simplified_16ud)::jsonb AS geography, features.properties, features.is_alive
             )
             SELECT 'post' AS typ, id, geography, properties, is_alive
             FROM update_features
             UNION SELECT 'pre' AS typ, id, geography, properties, is_alive
             FROM previous_features
            ;
SELECT features.id, ST_AsEWKB(geographies.geography::geometry) AS geography_ewkb, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
             AND (
                 $2::float[] IS NULL
                 OR ST_Intersects(extent, ST_MakeEnvelope($2[1], $2[2], $2[3], $2[4], 4326))
             )
             AND features.feature_type <> $3
             AND (
                 $4::int[] IS NULL
                 OR features.id = ANY($4)
             )
             AND (
                 $5::int[] IS NULL
                 OR features.id <> ALL($5)
             )
             AND (
                 $6::text[] IS NULL
                 OR features.feature_type = ANY($6)
             )
             AND (
                 $7::text[] IS NULL
                 OR features.feature_type <> ALL($7)
             )
             ORDER BY z_index
            ;
SELECT features.id, ST_AsEWKB(geographies.geography_simplified_64ud::geometry) AS geography_ewkb, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
             AND (
                 $2::float[] IS NULL
                 OR ST_Intersects(extent, ST_MakeEnvelope($2[1], $2[2], $2[3], $2[4], 4326))
             )
             AND features.feature_type <> $3
             AND (
                 $4::int[] IS NULL
                 OR features.id = ANY($4)
             )
             AND (
                 $5::int[] IS NULL
                 OR features.id <> ALL($5)
             )
             AND (
                 $6::text[] IS NULL
                 OR features.feature_type = ANY($6)
             )
             AND (
                 $7::text[] IS NULL
                 OR features.feature_type <> ALL($7)
             )
             ORDER BY z_index
            ;
WITH postredo_undo_stack_height AS (
                 SELECT undo_stack_height + 1 as undo_stack_height
                 FROM feature_collections
                 WHERE id = $1
                 AND is_alive
                 AND num_possible_redos > 0
             ), update_data AS (
                 SELECT max(upper(sys_period)) AS update_time
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM postredo_undo_stack_height)
             ), historic_features AS (
                 SELECT *
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM postredo_undo_stack_height)
                 AND upper(sys_period) = (SELECT update_time FROM update_data)
             ), set_feature_collection_data AS (
                 UPDATE feature_collections
                 SET undo_stack_height = (SELECT undo_stack_height FROM postredo_undo_stack_height),
                     num_possible_redos = num_possible_redos - 1
                 WHERE id = $1
                 AND is_alive
                 AND num_possible_redos > 0
             ), previous_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_64ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM features, historic_features, geographies
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND geographies.id = features.geography_id
             ), update_features AS (
                 UPDATE features
                 SET geography_id = historic_features.geography_id,
                     extent = historic_features.extent,
                     properties = historic_features.properties,
                     undo_stack_index = historic_features.undo_stack_index,
                     is_alive = historic_features.is_alive
                 FROM historic_features, geographies
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND geographies.id = historic_features.geography_id
                 RETURNING features.id, ST_AsGeoJSON(geographies.geography_simplified_64ud)::jsonb AS geography, features.properties, features.is_alive
             )
             SELECT 'post' AS typ, id, geography, properties, is_alive
             FROM update_features
             UNION SELECT 'pre' AS typ, id, geography, properties, is_alive
             FROM previous_features
            ;
WITH preundo_undo_stack_height AS (
                 SELECT undo_stack_height
                 FROM feature_collections
                 WHERE id = $1
                 AND is_alive
                 AND undo_stack_height > min_undo_stack_height
             ), features_to_be_undone AS (
                 SELECT id, geography_id, properties, is_alive
                 FROM features
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM preundo_undo_stack_height)
             ), newest_historic_versions AS (
                 SELECT id, max(upper(sys_period)) as upper_sys_period
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index < (SELECT undo_stack_height FROM preundo_undo_stack_height)
                 AND id IN (SELECT id FROM features_to_be_undone)
                 GROUP BY id
             ), historic_features AS (
                 SELECT fh.*
                 FROM features_history AS fh, newest_historic_versions AS nhv
                 WHERE feature_collection_id = $1
                 AND fh.id = nhv.id
                 AND upper(fh.sys_period) = nhv.upper_sys_period
             ), uncreate_features AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     -- Imagine that all possible features exists at undo_stack_height = 0 with `is_alive = FALSE`.
                     undo_stack_index = 0
                 WHERE feature_collection_id = $1
                 AND is_alive
                 AND id IN (SELECT id FROM features_to_be_undone)
                 AND id NOT IN (SELECT id FROM historic_features)
                 RETURNING id
             ), set_feature_collection_data AS (
                 UPDATE feature_collections
                 SET undo_stack_height = (SELECT undo_stack_height FROM preundo_undo_stack_height) - 1,
                     num_possible_redos = num_possible_redos + 1
                 WHERE id = $1
                 AND is_alive
                 AND undo_stack_height > min_undo_stack_height
             ), update_features AS (
                 UPDATE features
                 SET geography_id = historic_features.geography_id,
                     extent = historic_features.extent,
                     properties = historic_features.properties,
                     undo_stack_index = historic_features.undo_stack_index,
                     is_alive = historic_features.is_alive
                 FROM historic_features
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 RETURNING features.id, features.geography_id, features.properties, features.is_alive
             )
             SELECT 'post' AS typ, update_features.id, ST_AsGeoJSON(geographies.geography_simplified_256ud)::jsonb AS geography, properties, is_alive
             FROM update_features, geographies
             WHERE update_features.geography_id = geographies.id
             AND geographies.feature_collection_id = $1
             UNION SELECT 'pre' AS typ, ftbu.id, ST_AsGeoJSON(geographies.geography_simplified_256ud)::jsonb AS geography, ftbu.properties, ftbu.is_alive
             FROM features_to_be_undone AS ftbu, geographies
             WHERE ftbu.geography_id = geographies.id
             AND geographies.feature_collection_id = $1
             UNION SELECT 'post' AS typ, id, '{}'::jsonb as geography, '{}'::jsonb as properties, FALSE as is_alive
             FROM uncreate_features
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), old AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = $2
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             )
             UPDATE features
             SET geography_id = COALESCE($3, features.geography_id),
                 extent = COALESCE(ST_GeomFromEWKB($4::bytea), features.extent),
                 properties = COALESCE($5::jsonb , features.properties),
                 undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
             FROM old
             WHERE features.id = $2
             AND features.feature_collection_id = $1
             AND features.is_alive
             RETURNING old.id, old.geography, old.properties, old.is_alive
            ;
SELECT id, feature_collection_id, created_at, feature_type, properties, lower(sys_period) AS lower_sys_period FROM features_history;
WITH fc AS (
             INSERT INTO feature_collections
             DEFAULT VALUES
             RETURNING id as feature_collection_id
         ), dummy_geo AS (
             INSERT INTO geographies (feature_collection_id, geography)
             VALUES (
                 (SELECT feature_collection_id FROM fc),
                 ST_GeomFromText('POINT EMPTY', 4326)
             )
             RETURNING id
         ), dummy_feature AS (
             INSERT INTO features (feature_collection_id, geography_id, extent, properties, undo_stack_index)
             VALUES (
                 (SELECT feature_collection_id FROM fc),
                 (SELECT id FROM dummy_geo),
                 ST_GeomFromText('POINT EMPTY', 4326),
                 json_build_object(
                     'featureType', $1::text,
                     'createdFrom', 'N/A',
                     'zindex', 0,
                     'collection_properties', $2::jsonb
                 )::jsonb,
                 0
             )
         )
         SELECT feature_collection_id FROM fc
        ;
WITH new_undo_stack_height AS (
             UPDATE feature_collections
             SET undo_stack_height = undo_stack_height + 1,
                 num_possible_redos = 0
             WHERE id = $1 AND is_alive
             RETURNING undo_stack_height
         )
         INSERT INTO features (feature_collection_id, geography_id, extent, properties, undo_stack_index)
         VALUES (
             $1,
             $2,
             ST_GeomFromEWKB($3::bytea),
             $4::jsonb,
             (SELECT undo_stack_height FROM new_undo_stack_height)
         )
         RETURNING id;;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), deletees AS (
                 SELECT *
                 FROM UNNEST($2::integer[]) as deletees (id)
             ), old_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_8ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, deletees
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = deletees.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM deletees
                 WHERE feature_collection_id = $1
                 AND features.id = deletees.id
                 AND is_alive
             )
             SELECT id, geography, properties, is_alive
             FROM old_features
            ;
WITH postredo_undo_stack_height AS (
                 SELECT undo_stack_height + 1 as undo_stack_height
                 FROM feature_collections
                 WHERE id = $1
                 AND is_alive
                 AND num_possible_redos > 0
             ), update_data AS (
                 SELECT max(upper(sys_period)) AS update_time
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM postredo_undo_stack_height)
             ), historic_features AS (
                 SELECT *
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM postredo_undo_stack_height)
                 AND upper(sys_period) = (SELECT update_time FROM update_data)
             ), set_feature_collection_data AS (
                 UPDATE feature_collections
                 SET undo_stack_height = (SELECT undo_stack_height FROM postredo_undo_stack_height),
                     num_possible_redos = num_possible_redos - 1
                 WHERE id = $1
                 AND is_alive
                 AND num_possible_redos > 0
             ), previous_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_32ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM features, historic_features, geographies
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND geographies.id = features.geography_id
             ), update_features AS (
                 UPDATE features
                 SET geography_id = historic_features.geography_id,
                     extent = historic_features.extent,
                     properties = historic_features.properties,
                     undo_stack_index = historic_features.undo_stack_index,
                     is_alive = historic_features.is_alive
                 FROM historic_features, geographies
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND geographies.id = historic_features.geography_id
                 RETURNING features.id, ST_AsGeoJSON(geographies.geography_simplified_32ud)::jsonb AS geography, features.properties, features.is_alive
             )
             SELECT 'post' AS typ, id, geography, properties, is_alive
             FROM update_features
             UNION SELECT 'pre' AS typ, id, geography, properties, is_alive
             FROM previous_features
            ;
SELECT feature_collections.id, undo_stack_height, min_undo_stack_height, num_possible_redos, last_modified, features.properties -> 'collection_properties' as properties
         FROM feature_collections LEFT OUTER JOIN features
         ON features.feature_collection_id = feature_collections.id
         AND features.feature_type = $2
         WHERE feature_collections.id = $1
         AND feature_collections.is_alive
        ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0,
                     min_undo_stack_height = CASE WHEN $14 THEN undo_stack_height + 1 ELSE min_undo_stack_height END
                 WHERE id = $1
                 AND is_alive
                 RETURNING undo_stack_height
             ), the_creation AS (
                 INSERT INTO features (feature_collection_id, geography_id, extent, properties, undo_stack_index)
                 SELECT
                     $1,
                     geography_id,
                     ST_GeomFromEWKB(extent),
                     properties,
                     (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM UNNEST($2::integer[], $3::bytea[], $4::jsonb[]) AS nf (geography_id, extent, properties)
                 RETURNING id, is_alive
             ), feature_update_params AS (
                 SELECT *
                 FROM UNNEST($5::integer[], $6::bool[], $7::integer[], $8::bytea[], $9::bool[], $10::jsonb[], $11::bool[], $12::integer[]) AS nv
                     (id, should_update_geography, geography_id, extent, should_update_properties, properties, should_update_z_index, z_index)
             ), pre_update_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_32ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, feature_update_params AS nv
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = nv.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             ), the_update_and_reorder AS (
                 UPDATE features
                 SET geography_id = CASE WHEN nv.should_update_geography THEN nv.geography_id ELSE features.geography_id END,
                     extent = CASE WHEN nv.should_update_geography THEN ST_GeomFromEWKB(nv.extent) ELSE features.extent END,
                     -- We construct the new properties by first
                     -- considering if request.update wants to change them
                     -- and then applying request.reorder if needed. `||`
                     -- merges two JSON objects.
                     properties = CASE WHEN nv.should_update_properties THEN nv.properties ELSE features.properties END
                               || CASE WHEN nv.should_update_z_index THEN jsonb_build_object('zindex', nv.z_index) ELSE '{}'::jsonb END,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM feature_update_params AS nv
                 WHERE features.id = nv.id
                 AND feature_collection_id = $1
                 AND is_alive
                 AND (nv.should_update_geography OR nv.should_update_properties OR nv.should_update_z_index)
             ), deletees AS (
                 SELECT *
                 FROM UNNEST($13::integer[]) as deletees (id)
             ), pre_deletion_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_32ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, deletees
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = deletees.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM deletees
                 WHERE feature_collection_id = $1
                 AND features.id = deletees.id
                 AND is_alive
             ), pre_update_collection_properties AS (
                 SELECT properties -> 'collection_properties' as properties, is_alive
                 FROM features
                 WHERE $15::jsonb IS NOT NULL
                 AND feature_collection_id = $1
                 AND is_alive
                 AND feature_type = $16
             ), the_collection_properties_update AS (
                 UPDATE features
                 SET properties = jsonb_set(properties, '{collection_properties}', $15),
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 WHERE $15::jsonb IS NOT NULL
                 AND feature_collection_id = $1
                 AND is_alive
                 AND feature_type = $16
             )
             SELECT 'created' AS typ, id, NULL AS geography, NULL AS properties, is_alive, (row_number() OVER ())::integer AS row_idx
             FROM the_creation
             UNION SELECT 'updated' AS typ, id, geography, properties, is_alive, -1 AS row_idx
             FROM pre_update_features
             UNION SELECT 'deleted' AS typ, id, geography, properties, is_alive, -2 AS row_idx
             FROM pre_deletion_features
             UNION SELECT 'collection_properties' AS typ, NULL AS id, NULL AS geography, properties, is_alive, -3 AS row_idx
             FROM pre_update_collection_properties
             ORDER BY row_idx
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0,
                     min_undo_stack_height = CASE WHEN $14 THEN undo_stack_height + 1 ELSE min_undo_stack_height END
                 WHERE id = $1
                 AND is_alive
                 RETURNING undo_stack_height
             ), the_creation AS (
                 INSERT INTO features (feature_collection_id, geography_id, extent, properties, undo_stack_index)
                 SELECT
                     $1,
                     geography_id,
                     ST_GeomFromEWKB(extent),
                     properties,
                     (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM UNNEST($2::integer[], $3::bytea[], $4::jsonb[]) AS nf (geography_id, extent, properties)
                 RETURNING id, is_alive
             ), feature_update_params AS (
                 SELECT *
                 FROM UNNEST($5::integer[], $6::bool[], $7::integer[], $8::bytea[], $9::bool[], $10::jsonb[], $11::bool[], $12::integer[]) AS nv
                     (id, should_update_geography, geography_id, extent, should_update_properties, properties, should_update_z_index, z_index)
             ), pre_update_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_64ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, feature_update_params AS nv
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = nv.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             ), the_update_and_reorder AS (
                 UPDATE features
                 SET geography_id = CASE WHEN nv.should_update_geography THEN nv.geography_id ELSE features.geography_id END,
                     extent = CASE WHEN nv.should_update_geography THEN ST_GeomFromEWKB(nv.extent) ELSE features.extent END,
                     -- We construct the new properties by first
                     -- considering if request.update wants to change them
                     -- and then applying request.reorder if needed. `||`
                     -- merges two JSON objects.
                     properties = CASE WHEN nv.should_update_properties THEN nv.properties ELSE features.properties END
                               || CASE WHEN nv.should_update_z_index THEN jsonb_build_object('zindex', nv.z_index) ELSE '{}'::jsonb END,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM feature_update_params AS nv
                 WHERE features.id = nv.id
                 AND feature_collection_id = $1
                 AND is_alive
                 AND (nv.should_update_geography OR nv.should_update_properties OR nv.should_update_z_index)
             ), deletees AS (
                 SELECT *
                 FROM UNNEST($13::integer[]) as deletees (id)
             ), pre_deletion_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_64ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, deletees
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = deletees.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM deletees
                 WHERE feature_collection_id = $1
                 AND features.id = deletees.id
                 AND is_alive
             ), pre_update_collection_properties AS (
                 SELECT properties -> 'collection_properties' as properties, is_alive
                 FROM features
                 WHERE $15::jsonb IS NOT NULL
                 AND feature_collection_id = $1
                 AND is_alive
                 AND feature_type = $16
             ), the_collection_properties_update AS (
                 UPDATE features
                 SET properties = jsonb_set(properties, '{collection_properties}', $15),
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 WHERE $15::jsonb IS NOT NULL
                 AND feature_collection_id = $1
                 AND is_alive
                 AND feature_type = $16
             )
             SELECT 'created' AS typ, id, NULL AS geography, NULL AS properties, is_alive, (row_number() OVER ())::integer AS row_idx
             FROM the_creation
             UNION SELECT 'updated' AS typ, id, geography, properties, is_alive, -1 AS row_idx
             FROM pre_update_features
             UNION SELECT 'deleted' AS typ, id, geography, properties, is_alive, -2 AS row_idx
             FROM pre_deletion_features
             UNION SELECT 'collection_properties' AS typ, NULL AS id, NULL AS geography, properties, is_alive, -3 AS row_idx
             FROM pre_update_collection_properties
             ORDER BY row_idx
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1
                 AND is_alive
                 RETURNING undo_stack_height
             ), old AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_128ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = $2
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 WHERE id = $2
                 AND feature_collection_id = $1
                 AND is_alive
             )
             SELECT old.id, old.geography, old.properties, old.is_alive
             FROM old
            ;
WITH existing_feature AS (
                 SELECT created_from, geographies.geography, properties, feature_type
                 FROM features, geographies
                 WHERE features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND features.id = $2
                 AND is_alive
                 AND features.geography_id = geographies.id
             ), new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), new_geo AS (
                 INSERT INTO geographies (feature_collection_id, geography)
                 SELECT $1, ST_Buffer(geography, $3)
                 FROM existing_feature
                 RETURNING id, geography, geographies.geography_simplified_16ud AS simplified_geography
             ), new_feature AS (
                 INSERT INTO features (feature_collection_id, geography_id, extent, undo_stack_index, properties)
                 VALUES (
                     $1,
                     (SELECT id FROM new_geo),
                     (SELECT Box2D(geography::geometry)::geometry FROM new_geo),
                     (SELECT undo_stack_height FROM new_undo_stack_height),
                     json_build_object(
                         'zindex', (SELECT COALESCE(max(z_index) + 1, 0) FROM features WHERE feature_collection_id = $1),
                         'featureType', 'BufferZone',
                         'createdFrom', (SELECT format('%s (from %s)', COALESCE(properties ->> 'title', feature_type::text), created_from) FROM existing_feature),
                         'title', $4::text,
                         'description', $5::text
                     )
                 )
                 RETURNING id, properties, is_alive
             )
             SELECT new_feature.id, new_feature.properties, new_feature.is_alive, ST_AsGeoJSON(new_geo.simplified_geography)::jsonb AS geography
             FROM new_feature, new_geo
            ;
SELECT id, feature_collection_id, created_at, feature_type, properties, lower(sys_period) AS lower_sys_period FROM features;
WITH preundo_undo_stack_height AS (
                 SELECT undo_stack_height
                 FROM feature_collections
                 WHERE id = $1
                 AND is_alive
                 AND undo_stack_height > min_undo_stack_height
             ), features_to_be_undone AS (
                 SELECT id, geography_id, properties, is_alive
                 FROM features
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM preundo_undo_stack_height)
             ), newest_historic_versions AS (
                 SELECT id, max(upper(sys_period)) as upper_sys_period
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index < (SELECT undo_stack_height FROM preundo_undo_stack_height)
                 AND id IN (SELECT id FROM features_to_be_undone)
                 GROUP BY id
             ), historic_features AS (
                 SELECT fh.*
                 FROM features_history AS fh, newest_historic_versions AS nhv
                 WHERE feature_collection_id = $1
                 AND fh.id = nhv.id
                 AND upper(fh.sys_period) = nhv.upper_sys_period
             ), uncreate_features AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     -- Imagine that all possible features exists at undo_stack_height = 0 with `is_alive = FALSE`.
                     undo_stack_index = 0
                 WHERE feature_collection_id = $1
                 AND is_alive
                 AND id IN (SELECT id FROM features_to_be_undone)
                 AND id NOT IN (SELECT id FROM historic_features)
                 RETURNING id
             ), set_feature_collection_data AS (
                 UPDATE feature_collections
                 SET undo_stack_height = (SELECT undo_stack_height FROM preundo_undo_stack_height) - 1,
                     num_possible_redos = num_possible_redos + 1
                 WHERE id = $1
                 AND is_alive
                 AND undo_stack_height > min_undo_stack_height
             ), update_features AS (
                 UPDATE features
                 SET geography_id = historic_features.geography_id,
                     extent = historic_features.extent,
                     properties = historic_features.properties,
                     undo_stack_index = historic_features.undo_stack_index,
                     is_alive = historic_features.is_alive
                 FROM historic_features
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 RETURNING features.id, features.geography_id, features.properties, features.is_alive
             )
             SELECT 'post' AS typ, update_features.id, ST_AsGeoJSON(geographies.geography_simplified_32ud)::jsonb AS geography, properties, is_alive
             FROM update_features, geographies
             WHERE update_features.geography_id = geographies.id
             AND geographies.feature_collection_id = $1
             UNION SELECT 'pre' AS typ, ftbu.id, ST_AsGeoJSON(geographies.geography_simplified_32ud)::jsonb AS geography, ftbu.properties, ftbu.is_alive
             FROM features_to_be_undone AS ftbu, geographies
             WHERE ftbu.geography_id = geographies.id
             AND geographies.feature_collection_id = $1
             UNION SELECT 'post' AS typ, id, '{}'::jsonb as geography, '{}'::jsonb as properties, FALSE as is_alive
             FROM uncreate_features
            ;
SELECT 'pong
' AS a;
SELECT features.id, ST_AsEWKB(geographies.geography_simplified_8ud::geometry) AS geography_ewkb, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
             AND (
                 $2::float[] IS NULL
                 OR ST_Intersects(extent, ST_MakeEnvelope($2[1], $2[2], $2[3], $2[4], 4326))
             )
             AND features.feature_type <> $3
             AND (
                 $4::int[] IS NULL
                 OR features.id = ANY($4)
             )
             AND (
                 $5::int[] IS NULL
                 OR features.id <> ALL($5)
             )
             AND (
                 $6::text[] IS NULL
                 OR features.feature_type = ANY($6)
             )
             AND (
                 $7::text[] IS NULL
                 OR features.feature_type <> ALL($7)
             )
             ORDER BY z_index
            ;
SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_64ud)::jsonb AS geography, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
             AND (
                 $2::float[] IS NULL
                 OR ST_Intersects(extent, ST_MakeEnvelope($2[1], $2[2], $2[3], $2[4], 4326))
             )
             AND features.feature_type <> $3
             AND (
                 $4::int[] IS NULL
                 OR features.id = ANY($4)
             )
             AND (
                 $5::int[] IS NULL
                 OR features.id <> ALL($5)
             )
             AND (
                 $6::text[] IS NULL
                 OR features.feature_type = ANY($6)
             )
             AND (
                 $7::text[] IS NULL
                 OR features.feature_type <> ALL($7)
             )
             ORDER BY z_index
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1
                 AND is_alive
                 RETURNING undo_stack_height
             ), old AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_64ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = $2
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 WHERE id = $2
                 AND feature_collection_id = $1
                 AND is_alive
             )
             SELECT old.id, old.geography, old.properties, old.is_alive
             FROM old
            ;
SELECT PostGIS_Full_Version() as postgis_version;
WITH new_fc AS (
             INSERT INTO feature_collections (undo_stack_height, min_undo_stack_height)
             VALUES (1, 1)
             RETURNING id as feature_collection_id
         ), old_features AS (
             SELECT id, properties, geography_id, extent, ROW_NUMBER() OVER (ORDER BY id) AS row_idx
             FROM features
             WHERE feature_collection_id = $1
             AND is_alive
         ), old_geos AS (
             SELECT geographies.id AS geo_id, old_features.id AS feature_id, geography
             FROM geographies, old_features
             WHERE old_features.geography_id = geographies.id
             AND geographies.feature_collection_id = $1
         ), new_geos AS (
             INSERT INTO geographies (feature_collection_id, geography)
             SELECT new_fc.feature_collection_id, old_geos.geography
             FROM new_fc, old_geos
             ORDER BY old_geos.feature_id
             RETURNING id AS geo_id
         ), enumerate_new_geos AS (
             SELECT geo_id, ROW_NUMBER() OVER (ORDER BY geo_id) row_idx
             FROM new_geos
         ), clone_features AS (
             INSERT INTO features (feature_collection_id, properties, extent, undo_stack_index, geography_id)
             SELECT new_fc.feature_collection_id, properties, extent, 1, enumerate_new_geos.geo_id
             FROM new_fc, old_features, enumerate_new_geos
             WHERE old_features.row_idx = enumerate_new_geos.row_idx
         )
         SELECT feature_collection_id FROM new_fc
        ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), deletees AS (
                 SELECT *
                 FROM UNNEST($2::integer[]) as deletees (id)
             ), old_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_16ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, deletees
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = deletees.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM deletees
                 WHERE feature_collection_id = $1
                 AND features.id = deletees.id
                 AND is_alive
             )
             SELECT id, geography, properties, is_alive
             FROM old_features
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1
                 AND is_alive
                 RETURNING undo_stack_height
             ), old AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_8ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = $2
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 WHERE id = $2
                 AND feature_collection_id = $1
                 AND is_alive
             )
             SELECT old.id, old.geography, old.properties, old.is_alive
             FROM old
            ;
SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_32ud)::jsonb AS geography, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.id = $2
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1
                 AND is_alive
                 RETURNING undo_stack_height
             ), old AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_32ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = $2
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 WHERE id = $2
                 AND feature_collection_id = $1
                 AND is_alive
             )
             SELECT old.id, old.geography, old.properties, old.is_alive
             FROM old
            ;
INSERT INTO geographies (feature_collection_id, geography)
         VALUES ($1, ST_GeomFromGeoJSON($2::jsonb))
         RETURNING id, ST_AsEWKB(Box2D(ST_GeomFromGeoJSON($2::jsonb))::geometry) AS extent;
        ;
INSERT INTO geographies (feature_collection_id, geography)
         SELECT
             $1,
             ST_GeomFromGeoJSON(ng.geography)
         FROM UNNEST($2::jsonb[]) as ng (geography)
         RETURNING id, ST_AsEWKB(Box2D(geography::geometry)::geometry) AS extent;
        ;
WITH new_undo_stack_height AS (
             UPDATE feature_collections
             SET undo_stack_height = undo_stack_height + 1,
                 num_possible_redos = 0
             WHERE id = $3 AND is_alive
             RETURNING undo_stack_height
         )
         UPDATE features
         SET properties = properties || json_build_object('zindex', nv.z_index)::jsonb,
             undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
         FROM UNNEST($1::integer[], $2::integer[]) AS nv (id, z_index)
         WHERE features.id = nv.id
         AND features.feature_collection_id = $3
         AND features.is_alive
         AND features.z_index != nv.z_index
        ;
SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_32ud)::jsonb AS geography, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
             AND (
                 $2::float[] IS NULL
                 OR ST_Intersects(extent, ST_MakeEnvelope($2[1], $2[2], $2[3], $2[4], 4326))
             )
             AND features.feature_type <> $3
             AND (
                 $4::int[] IS NULL
                 OR features.id = ANY($4)
             )
             AND (
                 $5::int[] IS NULL
                 OR features.id <> ALL($5)
             )
             AND (
                 $6::text[] IS NULL
                 OR features.feature_type = ANY($6)
             )
             AND (
                 $7::text[] IS NULL
                 OR features.feature_type <> ALL($7)
             )
             ORDER BY z_index
            ;
SELECT features.id, ST_AsEWKB(geographies.geography_simplified_512ud::geometry) AS geography_ewkb, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
             AND (
                 $2::float[] IS NULL
                 OR ST_Intersects(extent, ST_MakeEnvelope($2[1], $2[2], $2[3], $2[4], 4326))
             )
             AND features.feature_type <> $3
             AND (
                 $4::int[] IS NULL
                 OR features.id = ANY($4)
             )
             AND (
                 $5::int[] IS NULL
                 OR features.id <> ALL($5)
             )
             AND (
                 $6::text[] IS NULL
                 OR features.feature_type = ANY($6)
             )
             AND (
                 $7::text[] IS NULL
                 OR features.feature_type <> ALL($7)
             )
             ORDER BY z_index
            ;
SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_8ud)::jsonb AS geography, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.id = $2
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
            ;
WITH preundo_undo_stack_height AS (
                 SELECT undo_stack_height
                 FROM feature_collections
                 WHERE id = $1
                 AND is_alive
                 AND undo_stack_height > min_undo_stack_height
             ), features_to_be_undone AS (
                 SELECT id, geography_id, properties, is_alive
                 FROM features
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM preundo_undo_stack_height)
             ), newest_historic_versions AS (
                 SELECT id, max(upper(sys_period)) as upper_sys_period
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index < (SELECT undo_stack_height FROM preundo_undo_stack_height)
                 AND id IN (SELECT id FROM features_to_be_undone)
                 GROUP BY id
             ), historic_features AS (
                 SELECT fh.*
                 FROM features_history AS fh, newest_historic_versions AS nhv
                 WHERE feature_collection_id = $1
                 AND fh.id = nhv.id
                 AND upper(fh.sys_period) = nhv.upper_sys_period
             ), uncreate_features AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     -- Imagine that all possible features exists at undo_stack_height = 0 with `is_alive = FALSE`.
                     undo_stack_index = 0
                 WHERE feature_collection_id = $1
                 AND is_alive
                 AND id IN (SELECT id FROM features_to_be_undone)
                 AND id NOT IN (SELECT id FROM historic_features)
                 RETURNING id
             ), set_feature_collection_data AS (
                 UPDATE feature_collections
                 SET undo_stack_height = (SELECT undo_stack_height FROM preundo_undo_stack_height) - 1,
                     num_possible_redos = num_possible_redos + 1
                 WHERE id = $1
                 AND is_alive
                 AND undo_stack_height > min_undo_stack_height
             ), update_features AS (
                 UPDATE features
                 SET geography_id = historic_features.geography_id,
                     extent = historic_features.extent,
                     properties = historic_features.properties,
                     undo_stack_index = historic_features.undo_stack_index,
                     is_alive = historic_features.is_alive
                 FROM historic_features
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 RETURNING features.id, features.geography_id, features.properties, features.is_alive
             )
             SELECT 'post' AS typ, update_features.id, ST_AsGeoJSON(geographies.geography_simplified_128ud)::jsonb AS geography, properties, is_alive
             FROM update_features, geographies
             WHERE update_features.geography_id = geographies.id
             AND geographies.feature_collection_id = $1
             UNION SELECT 'pre' AS typ, ftbu.id, ST_AsGeoJSON(geographies.geography_simplified_128ud)::jsonb AS geography, ftbu.properties, ftbu.is_alive
             FROM features_to_be_undone AS ftbu, geographies
             WHERE ftbu.geography_id = geographies.id
             AND geographies.feature_collection_id = $1
             UNION SELECT 'post' AS typ, id, '{}'::jsonb as geography, '{}'::jsonb as properties, FALSE as is_alive
             FROM uncreate_features
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), old AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_32ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = $2
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             )
             UPDATE features
             SET geography_id = COALESCE($3, features.geography_id),
                 extent = COALESCE(ST_GeomFromEWKB($4::bytea), features.extent),
                 properties = COALESCE($5::jsonb , features.properties),
                 undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
             FROM old
             WHERE features.id = $2
             AND features.feature_collection_id = $1
             AND features.is_alive
             RETURNING old.id, old.geography, old.properties, old.is_alive
            ;
WITH new_undo_stack_height AS (
             UPDATE feature_collections
             SET undo_stack_height = undo_stack_height + 1,
                 num_possible_redos = 0
             WHERE id = $1 AND is_alive
             RETURNING undo_stack_height
         )
         INSERT INTO features (feature_collection_id, geography_id, extent, properties, undo_stack_index)
         SELECT
             $1,
             geography_id,
             ST_GeomFromEWKB(extent),
             properties,
             (SELECT undo_stack_height FROM new_undo_stack_height)
         FROM UNNEST($2::integer[], $3::bytea[], $4::jsonb[]) AS nf (geography_id, extent, properties)
         RETURNING id;
        ;
WITH postredo_undo_stack_height AS (
                 SELECT undo_stack_height + 1 as undo_stack_height
                 FROM feature_collections
                 WHERE id = $1
                 AND is_alive
                 AND num_possible_redos > 0
             ), update_data AS (
                 SELECT max(upper(sys_period)) AS update_time
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM postredo_undo_stack_height)
             ), historic_features AS (
                 SELECT *
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM postredo_undo_stack_height)
                 AND upper(sys_period) = (SELECT update_time FROM update_data)
             ), set_feature_collection_data AS (
                 UPDATE feature_collections
                 SET undo_stack_height = (SELECT undo_stack_height FROM postredo_undo_stack_height),
                     num_possible_redos = num_possible_redos - 1
                 WHERE id = $1
                 AND is_alive
                 AND num_possible_redos > 0
             ), previous_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography)::jsonb AS geography, features.properties, features.is_alive
                 FROM features, historic_features, geographies
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND geographies.id = features.geography_id
             ), update_features AS (
                 UPDATE features
                 SET geography_id = historic_features.geography_id,
                     extent = historic_features.extent,
                     properties = historic_features.properties,
                     undo_stack_index = historic_features.undo_stack_index,
                     is_alive = historic_features.is_alive
                 FROM historic_features, geographies
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND geographies.id = historic_features.geography_id
                 RETURNING features.id, ST_AsGeoJSON(geographies.geography)::jsonb AS geography, features.properties, features.is_alive
             )
             SELECT 'post' AS typ, id, geography, properties, is_alive
             FROM update_features
             UNION SELECT 'pre' AS typ, id, geography, properties, is_alive
             FROM previous_features
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0,
                     min_undo_stack_height = CASE WHEN $14 THEN undo_stack_height + 1 ELSE min_undo_stack_height END
                 WHERE id = $1
                 AND is_alive
                 RETURNING undo_stack_height
             ), the_creation AS (
                 INSERT INTO features (feature_collection_id, geography_id, extent, properties, undo_stack_index)
                 SELECT
                     $1,
                     geography_id,
                     ST_GeomFromEWKB(extent),
                     properties,
                     (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM UNNEST($2::integer[], $3::bytea[], $4::jsonb[]) AS nf (geography_id, extent, properties)
                 RETURNING id, is_alive
             ), feature_update_params AS (
                 SELECT *
                 FROM UNNEST($5::integer[], $6::bool[], $7::integer[], $8::bytea[], $9::bool[], $10::jsonb[], $11::bool[], $12::integer[]) AS nv
                     (id, should_update_geography, geography_id, extent, should_update_properties, properties, should_update_z_index, z_index)
             ), pre_update_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_8ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, feature_update_params AS nv
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = nv.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             ), the_update_and_reorder AS (
                 UPDATE features
                 SET geography_id = CASE WHEN nv.should_update_geography THEN nv.geography_id ELSE features.geography_id END,
                     extent = CASE WHEN nv.should_update_geography THEN ST_GeomFromEWKB(nv.extent) ELSE features.extent END,
                     -- We construct the new properties by first
                     -- considering if request.update wants to change them
                     -- and then applying request.reorder if needed. `||`
                     -- merges two JSON objects.
                     properties = CASE WHEN nv.should_update_properties THEN nv.properties ELSE features.properties END
                               || CASE WHEN nv.should_update_z_index THEN jsonb_build_object('zindex', nv.z_index) ELSE '{}'::jsonb END,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM feature_update_params AS nv
                 WHERE features.id = nv.id
                 AND feature_collection_id = $1
                 AND is_alive
                 AND (nv.should_update_geography OR nv.should_update_properties OR nv.should_update_z_index)
             ), deletees AS (
                 SELECT *
                 FROM UNNEST($13::integer[]) as deletees (id)
             ), pre_deletion_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_8ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, deletees
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = deletees.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM deletees
                 WHERE feature_collection_id = $1
                 AND features.id = deletees.id
                 AND is_alive
             ), pre_update_collection_properties AS (
                 SELECT properties -> 'collection_properties' as properties, is_alive
                 FROM features
                 WHERE $15::jsonb IS NOT NULL
                 AND feature_collection_id = $1
                 AND is_alive
                 AND feature_type = $16
             ), the_collection_properties_update AS (
                 UPDATE features
                 SET properties = jsonb_set(properties, '{collection_properties}', $15),
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 WHERE $15::jsonb IS NOT NULL
                 AND feature_collection_id = $1
                 AND is_alive
                 AND feature_type = $16
             )
             SELECT 'created' AS typ, id, NULL AS geography, NULL AS properties, is_alive, (row_number() OVER ())::integer AS row_idx
             FROM the_creation
             UNION SELECT 'updated' AS typ, id, geography, properties, is_alive, -1 AS row_idx
             FROM pre_update_features
             UNION SELECT 'deleted' AS typ, id, geography, properties, is_alive, -2 AS row_idx
             FROM pre_deletion_features
             UNION SELECT 'collection_properties' AS typ, NULL AS id, NULL AS geography, properties, is_alive, -3 AS row_idx
             FROM pre_update_collection_properties
             ORDER BY row_idx
            ;
SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_256ud)::jsonb AS geography, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.id = $2
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
            ;
INSERT INTO geographies (feature_collection_id, geography)
                 VALUES (
                    $1,
                    ST_GeomFromGeoJSON($2::jsonb)
                 )
                 RETURNING id, ST_AsEWKB(Box2D(geography::geometry)::geometry) AS extent
                ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), old AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_256ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = $2
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             )
             UPDATE features
             SET geography_id = COALESCE($3, features.geography_id),
                 extent = COALESCE(ST_GeomFromEWKB($4::bytea), features.extent),
                 properties = COALESCE($5::jsonb , features.properties),
                 undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
             FROM old
             WHERE features.id = $2
             AND features.feature_collection_id = $1
             AND features.is_alive
             RETURNING old.id, old.geography, old.properties, old.is_alive
            ;
WITH postredo_undo_stack_height AS (
                 SELECT undo_stack_height + 1 as undo_stack_height
                 FROM feature_collections
                 WHERE id = $1
                 AND is_alive
                 AND num_possible_redos > 0
             ), update_data AS (
                 SELECT max(upper(sys_period)) AS update_time
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM postredo_undo_stack_height)
             ), historic_features AS (
                 SELECT *
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM postredo_undo_stack_height)
                 AND upper(sys_period) = (SELECT update_time FROM update_data)
             ), set_feature_collection_data AS (
                 UPDATE feature_collections
                 SET undo_stack_height = (SELECT undo_stack_height FROM postredo_undo_stack_height),
                     num_possible_redos = num_possible_redos - 1
                 WHERE id = $1
                 AND is_alive
                 AND num_possible_redos > 0
             ), previous_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_8ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM features, historic_features, geographies
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND geographies.id = features.geography_id
             ), update_features AS (
                 UPDATE features
                 SET geography_id = historic_features.geography_id,
                     extent = historic_features.extent,
                     properties = historic_features.properties,
                     undo_stack_index = historic_features.undo_stack_index,
                     is_alive = historic_features.is_alive
                 FROM historic_features, geographies
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND geographies.id = historic_features.geography_id
                 RETURNING features.id, ST_AsGeoJSON(geographies.geography_simplified_8ud)::jsonb AS geography, features.properties, features.is_alive
             )
             SELECT 'post' AS typ, id, geography, properties, is_alive
             FROM update_features
             UNION SELECT 'pre' AS typ, id, geography, properties, is_alive
             FROM previous_features
            ;
SELECT name, json_schema
                 FROM json_schemas
                ;
WITH existing_feature AS (
                 SELECT created_from, geographies.geography, properties, feature_type
                 FROM features, geographies
                 WHERE features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND features.id = $2
                 AND is_alive
                 AND features.geography_id = geographies.id
             ), new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), new_geo AS (
                 INSERT INTO geographies (feature_collection_id, geography)
                 SELECT $1, ST_Buffer(geography, $3)
                 FROM existing_feature
                 RETURNING id, geography, geographies.geography AS simplified_geography
             ), new_feature AS (
                 INSERT INTO features (feature_collection_id, geography_id, extent, undo_stack_index, properties)
                 VALUES (
                     $1,
                     (SELECT id FROM new_geo),
                     (SELECT Box2D(geography::geometry)::geometry FROM new_geo),
                     (SELECT undo_stack_height FROM new_undo_stack_height),
                     json_build_object(
                         'zindex', (SELECT COALESCE(max(z_index) + 1, 0) FROM features WHERE feature_collection_id = $1),
                         'featureType', 'BufferZone',
                         'createdFrom', (SELECT format('%s (from %s)', COALESCE(properties ->> 'title', feature_type::text), created_from) FROM existing_feature),
                         'title', $4::text,
                         'description', $5::text
                     )
                 )
                 RETURNING id, properties, is_alive
             )
             SELECT new_feature.id, new_feature.properties, new_feature.is_alive, ST_AsGeoJSON(new_geo.simplified_geography)::jsonb AS geography
             FROM new_feature, new_geo
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0,
                     min_undo_stack_height = CASE WHEN $14 THEN undo_stack_height + 1 ELSE min_undo_stack_height END
                 WHERE id = $1
                 AND is_alive
                 RETURNING undo_stack_height
             ), the_creation AS (
                 INSERT INTO features (feature_collection_id, geography_id, extent, properties, undo_stack_index)
                 SELECT
                     $1,
                     geography_id,
                     ST_GeomFromEWKB(extent),
                     properties,
                     (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM UNNEST($2::integer[], $3::bytea[], $4::jsonb[]) AS nf (geography_id, extent, properties)
                 RETURNING id, is_alive
             ), feature_update_params AS (
                 SELECT *
                 FROM UNNEST($5::integer[], $6::bool[], $7::integer[], $8::bytea[], $9::bool[], $10::jsonb[], $11::bool[], $12::integer[]) AS nv
                     (id, should_update_geography, geography_id, extent, should_update_properties, properties, should_update_z_index, z_index)
             ), pre_update_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_128ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, feature_update_params AS nv
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = nv.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             ), the_update_and_reorder AS (
                 UPDATE features
                 SET geography_id = CASE WHEN nv.should_update_geography THEN nv.geography_id ELSE features.geography_id END,
                     extent = CASE WHEN nv.should_update_geography THEN ST_GeomFromEWKB(nv.extent) ELSE features.extent END,
                     -- We construct the new properties by first
                     -- considering if request.update wants to change them
                     -- and then applying request.reorder if needed. `||`
                     -- merges two JSON objects.
                     properties = CASE WHEN nv.should_update_properties THEN nv.properties ELSE features.properties END
                               || CASE WHEN nv.should_update_z_index THEN jsonb_build_object('zindex', nv.z_index) ELSE '{}'::jsonb END,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM feature_update_params AS nv
                 WHERE features.id = nv.id
                 AND feature_collection_id = $1
                 AND is_alive
                 AND (nv.should_update_geography OR nv.should_update_properties OR nv.should_update_z_index)
             ), deletees AS (
                 SELECT *
                 FROM UNNEST($13::integer[]) as deletees (id)
             ), pre_deletion_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_128ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, deletees
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = deletees.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM deletees
                 WHERE feature_collection_id = $1
                 AND features.id = deletees.id
                 AND is_alive
             ), pre_update_collection_properties AS (
                 SELECT properties -> 'collection_properties' as properties, is_alive
                 FROM features
                 WHERE $15::jsonb IS NOT NULL
                 AND feature_collection_id = $1
                 AND is_alive
                 AND feature_type = $16
             ), the_collection_properties_update AS (
                 UPDATE features
                 SET properties = jsonb_set(properties, '{collection_properties}', $15),
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 WHERE $15::jsonb IS NOT NULL
                 AND feature_collection_id = $1
                 AND is_alive
                 AND feature_type = $16
             )
             SELECT 'created' AS typ, id, NULL AS geography, NULL AS properties, is_alive, (row_number() OVER ())::integer AS row_idx
             FROM the_creation
             UNION SELECT 'updated' AS typ, id, geography, properties, is_alive, -1 AS row_idx
             FROM pre_update_features
             UNION SELECT 'deleted' AS typ, id, geography, properties, is_alive, -2 AS row_idx
             FROM pre_deletion_features
             UNION SELECT 'collection_properties' AS typ, NULL AS id, NULL AS geography, properties, is_alive, -3 AS row_idx
             FROM pre_update_collection_properties
             ORDER BY row_idx
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), nv AS (
                 SELECT *
                 FROM UNNEST($2::integer[], $3::bool[], $4::integer[], $5::bytea[], $6::bool[], $7::jsonb[]) AS nv
                     (id, should_update_geography, geography_id, extent, should_update_properties, properties)
             ), old_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, nv
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = nv.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             ), the_update AS (
                 UPDATE features
                 SET geography_id = CASE WHEN nv.should_update_geography THEN nv.geography_id ELSE features.geography_id END,
                     extent = CASE WHEN nv.should_update_geography THEN ST_GeomFromEWKB(nv.extent) ELSE features.extent END,
                     properties = CASE WHEN nv.should_update_properties THEN nv.properties ELSE features.properties END,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM nv
                 WHERE features.id = nv.id
                 AND feature_collection_id = $1
                 AND is_alive
                 AND (nv.should_update_geography OR nv.should_update_properties)
             )
             SELECT id, geography, properties, is_alive
             FROM old_features
            ;
INSERT INTO json_schemas (name, json_schema)
         VALUES ($1, $2)
         ON CONFLICT (name) DO UPDATE
         SET json_schema = EXCLUDED.json_schema
        ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), deletees AS (
                 SELECT *
                 FROM UNNEST($2::integer[]) as deletees (id)
             ), old_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_64ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, deletees
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = deletees.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM deletees
                 WHERE feature_collection_id = $1
                 AND features.id = deletees.id
                 AND is_alive
             )
             SELECT id, geography, properties, is_alive
             FROM old_features
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), old AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_512ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = $2
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             )
             UPDATE features
             SET geography_id = COALESCE($3, features.geography_id),
                 extent = COALESCE(ST_GeomFromEWKB($4::bytea), features.extent),
                 properties = COALESCE($5::jsonb , features.properties),
                 undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
             FROM old
             WHERE features.id = $2
             AND features.feature_collection_id = $1
             AND features.is_alive
             RETURNING old.id, old.geography, old.properties, old.is_alive
            ;
WITH postredo_undo_stack_height AS (
                 SELECT undo_stack_height + 1 as undo_stack_height
                 FROM feature_collections
                 WHERE id = $1
                 AND is_alive
                 AND num_possible_redos > 0
             ), update_data AS (
                 SELECT max(upper(sys_period)) AS update_time
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM postredo_undo_stack_height)
             ), historic_features AS (
                 SELECT *
                 FROM features_history
                 WHERE feature_collection_id = $1
                 AND undo_stack_index = (SELECT undo_stack_height FROM postredo_undo_stack_height)
                 AND upper(sys_period) = (SELECT update_time FROM update_data)
             ), set_feature_collection_data AS (
                 UPDATE feature_collections
                 SET undo_stack_height = (SELECT undo_stack_height FROM postredo_undo_stack_height),
                     num_possible_redos = num_possible_redos - 1
                 WHERE id = $1
                 AND is_alive
                 AND num_possible_redos > 0
             ), previous_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_256ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM features, historic_features, geographies
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND geographies.id = features.geography_id
             ), update_features AS (
                 UPDATE features
                 SET geography_id = historic_features.geography_id,
                     extent = historic_features.extent,
                     properties = historic_features.properties,
                     undo_stack_index = historic_features.undo_stack_index,
                     is_alive = historic_features.is_alive
                 FROM historic_features, geographies
                 WHERE features.id = historic_features.id
                 AND features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND geographies.id = historic_features.geography_id
                 RETURNING features.id, ST_AsGeoJSON(geographies.geography_simplified_256ud)::jsonb AS geography, features.properties, features.is_alive
             )
             SELECT 'post' AS typ, id, geography, properties, is_alive
             FROM update_features
             UNION SELECT 'pre' AS typ, id, geography, properties, is_alive
             FROM previous_features
            ;
WITH existing_feature AS (
                 SELECT created_from, geographies.geography, properties, feature_type
                 FROM features, geographies
                 WHERE features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND features.id = $2
                 AND is_alive
                 AND features.geography_id = geographies.id
             ), new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), new_geo AS (
                 INSERT INTO geographies (feature_collection_id, geography)
                 SELECT $1, ST_Buffer(geography, $3)
                 FROM existing_feature
                 RETURNING id, geography, geographies.geography_simplified_32ud AS simplified_geography
             ), new_feature AS (
                 INSERT INTO features (feature_collection_id, geography_id, extent, undo_stack_index, properties)
                 VALUES (
                     $1,
                     (SELECT id FROM new_geo),
                     (SELECT Box2D(geography::geometry)::geometry FROM new_geo),
                     (SELECT undo_stack_height FROM new_undo_stack_height),
                     json_build_object(
                         'zindex', (SELECT COALESCE(max(z_index) + 1, 0) FROM features WHERE feature_collection_id = $1),
                         'featureType', 'BufferZone',
                         'createdFrom', (SELECT format('%s (from %s)', COALESCE(properties ->> 'title', feature_type::text), created_from) FROM existing_feature),
                         'title', $4::text,
                         'description', $5::text
                     )
                 )
                 RETURNING id, properties, is_alive
             )
             SELECT new_feature.id, new_feature.properties, new_feature.is_alive, ST_AsGeoJSON(new_geo.simplified_geography)::jsonb AS geography
             FROM new_feature, new_geo
            ;
SELECT undo_stack_height, min_undo_stack_height, num_possible_redos, last_modified
         FROM feature_collections
         WHERE feature_collections.id = $1
         AND feature_collections.is_alive
        ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0,
                     min_undo_stack_height = CASE WHEN $14 THEN undo_stack_height + 1 ELSE min_undo_stack_height END
                 WHERE id = $1
                 AND is_alive
                 RETURNING undo_stack_height
             ), the_creation AS (
                 INSERT INTO features (feature_collection_id, geography_id, extent, properties, undo_stack_index)
                 SELECT
                     $1,
                     geography_id,
                     ST_GeomFromEWKB(extent),
                     properties,
                     (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM UNNEST($2::integer[], $3::bytea[], $4::jsonb[]) AS nf (geography_id, extent, properties)
                 RETURNING id, is_alive
             ), feature_update_params AS (
                 SELECT *
                 FROM UNNEST($5::integer[], $6::bool[], $7::integer[], $8::bytea[], $9::bool[], $10::jsonb[], $11::bool[], $12::integer[]) AS nv
                     (id, should_update_geography, geography_id, extent, should_update_properties, properties, should_update_z_index, z_index)
             ), pre_update_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_256ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, feature_update_params AS nv
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = nv.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             ), the_update_and_reorder AS (
                 UPDATE features
                 SET geography_id = CASE WHEN nv.should_update_geography THEN nv.geography_id ELSE features.geography_id END,
                     extent = CASE WHEN nv.should_update_geography THEN ST_GeomFromEWKB(nv.extent) ELSE features.extent END,
                     -- We construct the new properties by first
                     -- considering if request.update wants to change them
                     -- and then applying request.reorder if needed. `||`
                     -- merges two JSON objects.
                     properties = CASE WHEN nv.should_update_properties THEN nv.properties ELSE features.properties END
                               || CASE WHEN nv.should_update_z_index THEN jsonb_build_object('zindex', nv.z_index) ELSE '{}'::jsonb END,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM feature_update_params AS nv
                 WHERE features.id = nv.id
                 AND feature_collection_id = $1
                 AND is_alive
                 AND (nv.should_update_geography OR nv.should_update_properties OR nv.should_update_z_index)
             ), deletees AS (
                 SELECT *
                 FROM UNNEST($13::integer[]) as deletees (id)
             ), pre_deletion_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_256ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, deletees
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = deletees.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM deletees
                 WHERE feature_collection_id = $1
                 AND features.id = deletees.id
                 AND is_alive
             ), pre_update_collection_properties AS (
                 SELECT properties -> 'collection_properties' as properties, is_alive
                 FROM features
                 WHERE $15::jsonb IS NOT NULL
                 AND feature_collection_id = $1
                 AND is_alive
                 AND feature_type = $16
             ), the_collection_properties_update AS (
                 UPDATE features
                 SET properties = jsonb_set(properties, '{collection_properties}', $15),
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 WHERE $15::jsonb IS NOT NULL
                 AND feature_collection_id = $1
                 AND is_alive
                 AND feature_type = $16
             )
             SELECT 'created' AS typ, id, NULL AS geography, NULL AS properties, is_alive, (row_number() OVER ())::integer AS row_idx
             FROM the_creation
             UNION SELECT 'updated' AS typ, id, geography, properties, is_alive, -1 AS row_idx
             FROM pre_update_features
             UNION SELECT 'deleted' AS typ, id, geography, properties, is_alive, -2 AS row_idx
             FROM pre_deletion_features
             UNION SELECT 'collection_properties' AS typ, NULL AS id, NULL AS geography, properties, is_alive, -3 AS row_idx
             FROM pre_update_collection_properties
             ORDER BY row_idx
            ;
SELECT features.id, ST_AsEWKB(geographies.geography_simplified_32ud::geometry) AS geography_ewkb, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
             AND (
                 $2::float[] IS NULL
                 OR ST_Intersects(extent, ST_MakeEnvelope($2[1], $2[2], $2[3], $2[4], 4326))
             )
             AND features.feature_type <> $3
             AND (
                 $4::int[] IS NULL
                 OR features.id = ANY($4)
             )
             AND (
                 $5::int[] IS NULL
                 OR features.id <> ALL($5)
             )
             AND (
                 $6::text[] IS NULL
                 OR features.feature_type = ANY($6)
             )
             AND (
                 $7::text[] IS NULL
                 OR features.feature_type <> ALL($7)
             )
             ORDER BY z_index
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), nv AS (
                 SELECT *
                 FROM UNNEST($2::integer[], $3::bool[], $4::integer[], $5::bytea[], $6::bool[], $7::jsonb[]) AS nv
                     (id, should_update_geography, geography_id, extent, should_update_properties, properties)
             ), old_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_256ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, nv
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = nv.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             ), the_update AS (
                 UPDATE features
                 SET geography_id = CASE WHEN nv.should_update_geography THEN nv.geography_id ELSE features.geography_id END,
                     extent = CASE WHEN nv.should_update_geography THEN ST_GeomFromEWKB(nv.extent) ELSE features.extent END,
                     properties = CASE WHEN nv.should_update_properties THEN nv.properties ELSE features.properties END,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM nv
                 WHERE features.id = nv.id
                 AND feature_collection_id = $1
                 AND is_alive
                 AND (nv.should_update_geography OR nv.should_update_properties)
             )
             SELECT id, geography, properties, is_alive
             FROM old_features
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), deletees AS (
                 SELECT *
                 FROM UNNEST($2::integer[]) as deletees (id)
             ), old_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_256ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, deletees
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = deletees.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM deletees
                 WHERE feature_collection_id = $1
                 AND features.id = deletees.id
                 AND is_alive
             )
             SELECT id, geography, properties, is_alive
             FROM old_features
            ;
WITH existing_feature AS (
                 SELECT created_from, geographies.geography, properties, feature_type
                 FROM features, geographies
                 WHERE features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND features.id = $2
                 AND is_alive
                 AND features.geography_id = geographies.id
             ), new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), new_geo AS (
                 INSERT INTO geographies (feature_collection_id, geography)
                 SELECT $1, ST_Buffer(geography, $3)
                 FROM existing_feature
                 RETURNING id, geography, geographies.geography_simplified_512ud AS simplified_geography
             ), new_feature AS (
                 INSERT INTO features (feature_collection_id, geography_id, extent, undo_stack_index, properties)
                 VALUES (
                     $1,
                     (SELECT id FROM new_geo),
                     (SELECT Box2D(geography::geometry)::geometry FROM new_geo),
                     (SELECT undo_stack_height FROM new_undo_stack_height),
                     json_build_object(
                         'zindex', (SELECT COALESCE(max(z_index) + 1, 0) FROM features WHERE feature_collection_id = $1),
                         'featureType', 'BufferZone',
                         'createdFrom', (SELECT format('%s (from %s)', COALESCE(properties ->> 'title', feature_type::text), created_from) FROM existing_feature),
                         'title', $4::text,
                         'description', $5::text
                     )
                 )
                 RETURNING id, properties, is_alive
             )
             SELECT new_feature.id, new_feature.properties, new_feature.is_alive, ST_AsGeoJSON(new_geo.simplified_geography)::jsonb AS geography
             FROM new_feature, new_geo
            ;
SELECT features.id, ST_AsGeoJSON(geographies.geography)::jsonb AS geography, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.id = $2
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), old AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_16ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = $2
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             )
             UPDATE features
             SET geography_id = COALESCE($3, features.geography_id),
                 extent = COALESCE(ST_GeomFromEWKB($4::bytea), features.extent),
                 properties = COALESCE($5::jsonb , features.properties),
                 undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
             FROM old
             WHERE features.id = $2
             AND features.feature_collection_id = $1
             AND features.is_alive
             RETURNING old.id, old.geography, old.properties, old.is_alive
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), nv AS (
                 SELECT *
                 FROM UNNEST($2::integer[], $3::bool[], $4::integer[], $5::bytea[], $6::bool[], $7::jsonb[]) AS nv
                     (id, should_update_geography, geography_id, extent, should_update_properties, properties)
             ), old_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_128ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, nv
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = nv.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             ), the_update AS (
                 UPDATE features
                 SET geography_id = CASE WHEN nv.should_update_geography THEN nv.geography_id ELSE features.geography_id END,
                     extent = CASE WHEN nv.should_update_geography THEN ST_GeomFromEWKB(nv.extent) ELSE features.extent END,
                     properties = CASE WHEN nv.should_update_properties THEN nv.properties ELSE features.properties END,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM nv
                 WHERE features.id = nv.id
                 AND feature_collection_id = $1
                 AND is_alive
                 AND (nv.should_update_geography OR nv.should_update_properties)
             )
             SELECT id, geography, properties, is_alive
             FROM old_features
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), nv AS (
                 SELECT *
                 FROM UNNEST($2::integer[], $3::bool[], $4::integer[], $5::bytea[], $6::bool[], $7::jsonb[]) AS nv
                     (id, should_update_geography, geography_id, extent, should_update_properties, properties)
             ), old_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_512ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, nv
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = nv.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             ), the_update AS (
                 UPDATE features
                 SET geography_id = CASE WHEN nv.should_update_geography THEN nv.geography_id ELSE features.geography_id END,
                     extent = CASE WHEN nv.should_update_geography THEN ST_GeomFromEWKB(nv.extent) ELSE features.extent END,
                     properties = CASE WHEN nv.should_update_properties THEN nv.properties ELSE features.properties END,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM nv
                 WHERE features.id = nv.id
                 AND feature_collection_id = $1
                 AND is_alive
                 AND (nv.should_update_geography OR nv.should_update_properties)
             )
             SELECT id, geography, properties, is_alive
             FROM old_features
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), nv AS (
                 SELECT *
                 FROM UNNEST($2::integer[], $3::bool[], $4::integer[], $5::bytea[], $6::bool[], $7::jsonb[]) AS nv
                     (id, should_update_geography, geography_id, extent, should_update_properties, properties)
             ), old_features AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_64ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies, nv
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = nv.id
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             ), the_update AS (
                 UPDATE features
                 SET geography_id = CASE WHEN nv.should_update_geography THEN nv.geography_id ELSE features.geography_id END,
                     extent = CASE WHEN nv.should_update_geography THEN ST_GeomFromEWKB(nv.extent) ELSE features.extent END,
                     properties = CASE WHEN nv.should_update_properties THEN nv.properties ELSE features.properties END,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 FROM nv
                 WHERE features.id = nv.id
                 AND feature_collection_id = $1
                 AND is_alive
                 AND (nv.should_update_geography OR nv.should_update_properties)
             )
             SELECT id, geography, properties, is_alive
             FROM old_features
            ;
SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_128ud)::jsonb AS geography, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
             AND (
                 $2::float[] IS NULL
                 OR ST_Intersects(extent, ST_MakeEnvelope($2[1], $2[2], $2[3], $2[4], 4326))
             )
             AND features.feature_type <> $3
             AND (
                 $4::int[] IS NULL
                 OR features.id = ANY($4)
             )
             AND (
                 $5::int[] IS NULL
                 OR features.id <> ALL($5)
             )
             AND (
                 $6::text[] IS NULL
                 OR features.feature_type = ANY($6)
             )
             AND (
                 $7::text[] IS NULL
                 OR features.feature_type <> ALL($7)
             )
             ORDER BY z_index
            ;
SELECT features.id, ST_AsEWKB(geographies.geography_simplified_256ud::geometry) AS geography_ewkb, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
             AND (
                 $2::float[] IS NULL
                 OR ST_Intersects(extent, ST_MakeEnvelope($2[1], $2[2], $2[3], $2[4], 4326))
             )
             AND features.feature_type <> $3
             AND (
                 $4::int[] IS NULL
                 OR features.id = ANY($4)
             )
             AND (
                 $5::int[] IS NULL
                 OR features.id <> ALL($5)
             )
             AND (
                 $6::text[] IS NULL
                 OR features.feature_type = ANY($6)
             )
             AND (
                 $7::text[] IS NULL
                 OR features.feature_type <> ALL($7)
             )
             ORDER BY z_index
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1
                 AND is_alive
                 RETURNING undo_stack_height
             ), old AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_16ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = $2
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 WHERE id = $2
                 AND feature_collection_id = $1
                 AND is_alive
             )
             SELECT old.id, old.geography, old.properties, old.is_alive
             FROM old
            ;
WITH new_schemas AS (
             SELECT name, json_schema
             FROM UNNEST($1::text[], $2::text[]) AS new_schemas (name, json_schema)
         ), update_existing_schemas AS (
             UPDATE json_schemas
             SET json_schema = new_schemas.json_schema
             FROM new_schemas
             WHERE json_schemas.name = new_schemas.name
             AND json_schemas.json_schema <> new_schemas.json_schema
         ), delete_stale_schemas AS (
             DELETE FROM json_schemas
             WHERE json_schemas.name NOT IN (SELECT name FROM new_schemas)
         )
         INSERT INTO json_schemas (name, json_schema)
         SELECT name, json_schema
         FROM new_schemas
         WHERE new_schemas.name NOT IN (SELECT name FROM json_schemas)
        ;
INSERT INTO geographies (feature_collection_id, geography)
             SELECT $1, ST_GeomFromGeoJSON(ng.geography)
             FROM UNNEST($2::jsonb[]) as ng (geography)
             RETURNING id, ST_AsEWKB(Box2D(geography::geometry)::geometry) AS extent
            ;
WITH existing_feature AS (
                 SELECT created_from, geographies.geography, properties, feature_type
                 FROM features, geographies
                 WHERE features.feature_collection_id = $1
                 AND geographies.feature_collection_id = $1
                 AND features.id = $2
                 AND is_alive
                 AND features.geography_id = geographies.id
             ), new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), new_geo AS (
                 INSERT INTO geographies (feature_collection_id, geography)
                 SELECT $1, ST_Buffer(geography, $3)
                 FROM existing_feature
                 RETURNING id, geography, geographies.geography_simplified_256ud AS simplified_geography
             ), new_feature AS (
                 INSERT INTO features (feature_collection_id, geography_id, extent, undo_stack_index, properties)
                 VALUES (
                     $1,
                     (SELECT id FROM new_geo),
                     (SELECT Box2D(geography::geometry)::geometry FROM new_geo),
                     (SELECT undo_stack_height FROM new_undo_stack_height),
                     json_build_object(
                         'zindex', (SELECT COALESCE(max(z_index) + 1, 0) FROM features WHERE feature_collection_id = $1),
                         'featureType', 'BufferZone',
                         'createdFrom', (SELECT format('%s (from %s)', COALESCE(properties ->> 'title', feature_type::text), created_from) FROM existing_feature),
                         'title', $4::text,
                         'description', $5::text
                     )
                 )
                 RETURNING id, properties, is_alive
             )
             SELECT new_feature.id, new_feature.properties, new_feature.is_alive, ST_AsGeoJSON(new_geo.simplified_geography)::jsonb AS geography
             FROM new_feature, new_geo
            ;
WITH new_undo_stack_height AS (
             UPDATE feature_collections
             SET is_alive = FALSE,
                 undo_stack_height = undo_stack_height + 1
             WHERE id = $1
             RETURNING undo_stack_height
         )
         UPDATE features
         SET is_alive = FALSE,
             undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
         WHERE feature_collection_id = $1
         AND is_alive;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1
                 AND is_alive
                 RETURNING undo_stack_height
             ), old AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_512ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = $2
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 WHERE id = $2
                 AND feature_collection_id = $1
                 AND is_alive
             )
             SELECT old.id, old.geography, old.properties, old.is_alive
             FROM old
            ;
SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_128ud)::jsonb AS geography, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.id = $2
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
            ;
SELECT features.id, ST_AsGeoJSON(geographies.geography)::jsonb AS geography, features.properties, features.is_alive
             FROM feature_collections, features, geographies
             WHERE features.feature_collection_id = $1
             AND features.feature_collection_id = feature_collections.id
             AND geographies.feature_collection_id = feature_collections.id
             AND features.geography_id = geographies.id
             AND feature_collections.is_alive
             AND features.is_alive
             AND (
                 $2::float[] IS NULL
                 OR ST_Intersects(extent, ST_MakeEnvelope($2[1], $2[2], $2[3], $2[4], 4326))
             )
             AND features.feature_type <> $3
             AND (
                 $4::int[] IS NULL
                 OR features.id = ANY($4)
             )
             AND (
                 $5::int[] IS NULL
                 OR features.id <> ALL($5)
             )
             AND (
                 $6::text[] IS NULL
                 OR features.feature_type = ANY($6)
             )
             AND (
                 $7::text[] IS NULL
                 OR features.feature_type <> ALL($7)
             )
             ORDER BY z_index
            ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1 AND is_alive
                 RETURNING undo_stack_height
             ), old AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_8ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = $2
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
                 AND features.is_alive
             )
             UPDATE features
             SET geography_id = COALESCE($3, features.geography_id),
                 extent = COALESCE(ST_GeomFromEWKB($4::bytea), features.extent),
                 properties = COALESCE($5::jsonb , features.properties),
                 undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
             FROM old
             WHERE features.id = $2
             AND features.feature_collection_id = $1
             AND features.is_alive
             RETURNING old.id, old.geography, old.properties, old.is_alive
            ;
SELECT version() as version, current_user as user, current_database() as database,
             inet_server_addr()::text as server_addr, inet_server_port() as server_port, pg_postmaster_start_time()::text as server_start_time
        ;
WITH new_undo_stack_height AS (
                 UPDATE feature_collections
                 SET undo_stack_height = undo_stack_height + 1,
                     num_possible_redos = 0
                 WHERE id = $1
                 AND is_alive
                 RETURNING undo_stack_height
             ), old AS (
                 SELECT features.id, ST_AsGeoJSON(geographies.geography_simplified_256ud)::jsonb AS geography, features.properties, features.is_alive
                 FROM feature_collections, features, geographies
                 WHERE features.feature_collection_id = $1
                 AND features.feature_collection_id = feature_collections.id
                 AND geographies.feature_collection_id = feature_collections.id
                 AND features.id = $2
                 AND features.geography_id = geographies.id
                 AND feature_collections.is_alive
             ), the_deletion AS (
                 UPDATE features
                 SET is_alive = FALSE,
                     undo_stack_index = (SELECT undo_stack_height FROM new_undo_stack_height)
                 WHERE id = $2
                 AND feature_collection_id = $1
                 AND is_alive
             )
             SELECT old.id, old.geography, old.properties, old.is_alive
             FROM old
            ;
