Random temporary table name

This commit is contained in:
Juan Ignacio Sánchez Lara 2018-06-22 12:35:29 +02:00
parent bbbf70f3ac
commit 91d93bef79
2 changed files with 22 additions and 12 deletions

View File

@ -1997,6 +1997,8 @@ DECLARE
batches_n integer; batches_n integer;
BATCHES_SIZE CONSTANT numeric := 100; BATCHES_SIZE CONSTANT numeric := 100;
current_row_count integer ; current_row_count integer ;
temp_table_name text;
BEGIN BEGIN
EXECUTE format('SELECT COUNT(1) from (%s) _x', query) INTO query_row_count; EXECUTE format('SELECT COUNT(1) from (%s) _x', query) INTO query_row_count;
@ -2011,8 +2013,11 @@ BEGIN
RAISE DEBUG 'batches_n: %', batches_n; RAISE DEBUG 'batches_n: %', batches_n;
CREATE TEMPORARY TABLE bulk_geocode_street_point temp_table_name := 'bulk_geocode_street_' || md5(random()::text);
(cartodb_id integer, the_geom geometry(Multipolygon,4326), metadata jsonb);
EXECUTE format('CREATE TEMPORARY TABLE %s ' ||
'(cartodb_id integer, the_geom geometry(Multipolygon,4326), metadata jsonb)',
temp_table_name);
FOR cartodb_id_batch in 0..(batches_n - 1) FOR cartodb_id_batch in 0..(batches_n - 1)
LOOP LOOP
@ -2024,9 +2029,9 @@ BEGIN
' floor((cartodb_id-1)::float/$1) as batch' || ' floor((cartodb_id-1)::float/$1) as batch' ||
' FROM (%s) _x' || ' FROM (%s) _x' ||
') ' || ') ' ||
'INSERT INTO bulk_geocode_street_point SELECT (cdb_dataservices_client._cdb_bulk_geocode_street_point(jsonb_agg(data))).* ' || 'INSERT INTO %s SELECT (cdb_dataservices_client._cdb_bulk_geocode_street_point(jsonb_agg(data))).* ' ||
'FROM geocoding_data ' || 'FROM geocoding_data ' ||
'WHERE batch = $2', street_column, city_column, state_column, country_column, query) 'WHERE batch = $2', street_column, city_column, state_column, country_column, query, temp_table_name)
USING BATCHES_SIZE, cartodb_id_batch; USING BATCHES_SIZE, cartodb_id_batch;
GET DIAGNOSTICS current_row_count = ROW_COUNT; GET DIAGNOSTICS current_row_count = ROW_COUNT;
@ -2034,7 +2039,7 @@ BEGIN
END LOOP; END LOOP;
RETURN QUERY SELECT * FROM bulk_geocode_street_point; RETURN QUERY EXECUTE 'SELECT * FROM ' || quote_ident(temp_table_name);
END; END;
$$ LANGUAGE 'plpgsql' SECURITY DEFINER VOLATILE PARALLEL UNSAFE; $$ LANGUAGE 'plpgsql' SECURITY DEFINER VOLATILE PARALLEL UNSAFE;
-- --

View File

@ -9,6 +9,8 @@ DECLARE
batches_n integer; batches_n integer;
BATCHES_SIZE CONSTANT numeric := 100; BATCHES_SIZE CONSTANT numeric := 100;
current_row_count integer ; current_row_count integer ;
temp_table_name text;
BEGIN BEGIN
EXECUTE format('SELECT COUNT(1) from (%s) _x', query) INTO query_row_count; EXECUTE format('SELECT COUNT(1) from (%s) _x', query) INTO query_row_count;
@ -23,8 +25,11 @@ BEGIN
RAISE DEBUG 'batches_n: %', batches_n; RAISE DEBUG 'batches_n: %', batches_n;
CREATE TEMPORARY TABLE bulk_geocode_street_point temp_table_name := 'bulk_geocode_street_' || md5(random()::text);
(cartodb_id integer, the_geom geometry(Multipolygon,4326), metadata jsonb);
EXECUTE format('CREATE TEMPORARY TABLE %s ' ||
'(cartodb_id integer, the_geom geometry(Multipolygon,4326), metadata jsonb)',
temp_table_name);
FOR cartodb_id_batch in 0..(batches_n - 1) FOR cartodb_id_batch in 0..(batches_n - 1)
LOOP LOOP
@ -36,9 +41,9 @@ BEGIN
' floor((cartodb_id-1)::float/$1) as batch' || ' floor((cartodb_id-1)::float/$1) as batch' ||
' FROM (%s) _x' || ' FROM (%s) _x' ||
') ' || ') ' ||
'INSERT INTO bulk_geocode_street_point SELECT (cdb_dataservices_client._cdb_bulk_geocode_street_point(jsonb_agg(data))).* ' || 'INSERT INTO %s SELECT (cdb_dataservices_client._cdb_bulk_geocode_street_point(jsonb_agg(data))).* ' ||
'FROM geocoding_data ' || 'FROM geocoding_data ' ||
'WHERE batch = $2', street_column, city_column, state_column, country_column, query) 'WHERE batch = $2', street_column, city_column, state_column, country_column, query, temp_table_name)
USING BATCHES_SIZE, cartodb_id_batch; USING BATCHES_SIZE, cartodb_id_batch;
GET DIAGNOSTICS current_row_count = ROW_COUNT; GET DIAGNOSTICS current_row_count = ROW_COUNT;
@ -46,6 +51,6 @@ BEGIN
END LOOP; END LOOP;
RETURN QUERY SELECT * FROM bulk_geocode_street_point; RETURN QUERY EXECUTE 'SELECT * FROM ' || quote_ident(temp_table_name);
END; END;
$$ LANGUAGE 'plpgsql' SECURITY DEFINER VOLATILE PARALLEL UNSAFE; $$ LANGUAGE 'plpgsql' SECURITY DEFINER VOLATILE PARALLEL UNSAFE;