postgresql

Query from a variable table in postgres

萝らか妹 提交于 2021-01-28 03:02:33
问题 In my postgres DB, I Have some tables which their names vary based on the current date. To be more specific their names are like these: STATSDATA15_8_2019 STATSDATA14_8_2019 STATSDATA13_8_2019 ... I need an automated query that considering the current date, grabs data from the related table. For example: If the date is August 15th, I need: select * from STATSDATA15_8_2019 And if it is August 14th, I need: select * from STATSDATA14_8_2019 In fact i need an automated, dynamic query with

How to disable all optimizations of PostgreSQL

白昼怎懂夜的黑 提交于 2021-01-28 03:00:40
问题 I'm studying query optimization and want to know how much each kind of optimizations help the query. Last time, I got an answer but when in my experiments, disable all optimization in the link has time complicity of O(n^1.8) enable all of them has O(n^0.5). there is not so much difference, if disable all of them, is there still other optimizations? how can I really have only one main optimizations each time? 回答1: You can't. PostgreSQL's query planner has no "turn off optimisation" flag. It'd

How to access a HSTORE column using PostgreSQL C library (libpq)?

廉价感情. 提交于 2021-01-28 02:56:42
问题 I cannot find any documentation regarding HSTORE data access using the C library. Currently I'm considering to just convert the HSTORE columns into arrays in my queries but is there a way to avoid such conversions? 回答1: libpqtypes appears to have some support for hstore . Another option is to avoid directly interacting with hstore in your code. You can still benefit from it in the database without dealing with its text representation on the client side. Say you want to fetch a hstore field;

How to modify field with jsonPath in PostgreSQL?

若如初见. 提交于 2021-01-28 02:54:46
问题 How to modify a field using jsonPath in PostgreSQL like SQL Server JSON_MODIFY (https://docs.microsoft.com/en-us/sql/t-sql/functions/json-modify-transact-sql?view=sql-server-ver15)? Thanks! 回答1: There are currently no ways to update JSON properties using JsonPath. The only way is with jsonb query jsonb_set 来源: https://stackoverflow.com/questions/62006261/how-to-modify-field-with-jsonpath-in-postgresql

Perform alembic upgrade in multiple schemas

隐身守侯 提交于 2021-01-28 02:51:48
问题 I am using flask + sqlalchemy + alembic + postgresql, and am trying to find a simple architecture to solve the following problem. I have a simple database structure, lets say two tables : -- Users -- UserItems My users are in several different domains. I would like to have several of this database structure. For that I have database schemas in SQL. I created the matching class structure using sqlalchemy decalrative_base, and end up with a MetaData object that isn't tied to a specific schema.

How to replace an array of characters in a column of text in PostgreSQL?

大憨熊 提交于 2021-01-28 02:10:14
问题 I have 2 text columns where I need to replace (on update) chars from array 1 ('q','x','y','z') to their index-equivalent value in array 2 ('a','b','c','d'). The closest I've come to atm is to nest the replace calls within each other like so UPDATE mytable SET col1=replace( replace( replace( replace( col1,'q','a' ),'x','b' ),'y','c' ),'z','d' ), col2=replace( replace( replace( replace( col2,'q','a' ),'x','b' ),'y','c' ),'z','d' ) but surely there must be a better way to do this? In my live

PostgreSQL: Delete key/value pair from array with json objects

孤街醉人 提交于 2021-01-28 02:07:04
问题 I have a table: CREATE TABLE movies( id text, data jsonb ); INSERT INTO movies(id, data) VALUES ( '1', { "actors": [ { "name": "actor1", "email": "actor1@somemail.com" }, { "name": "actor2", "email": "actor2@somemail.com" } ] } ); What I want is to delete the email field (key + value) from each json object of the actors array. I've tried the following solution and although it does execute, it doesn't have any effect on the array at all: update movies set data = jsonb_set(data, '{actors}',

Isn't PostgreSQL single query execution atomic? [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-28 02:02:55
问题 This question already has answers here : Insert, on duplicate update in PostgreSQL? (16 answers) Closed 5 years ago . In my postgresql DB, I have a table "my_table" with primary key on columns (a, b). I wrote the query below for inserts into this table which ensures that the primary key constraint is never violated. INSERT INTO my_table(a, b, c, d, e, f) (SELECT 'a1', 'b1', 'c1', 1442849052013, 1, FALSE FROM (SELECT 'a1'::VARCHAR(100) AS a, 'b1'::VARCHAR(50) AS b) AS new_fields LEFT OUTER

I'm storing code snippets in my database. How can I enable revisions/versions of them?

主宰稳场 提交于 2021-01-28 01:52:58
问题 I have a pretty primitive table schema now ( in Postgres ): CREATE TABLE "public"."sandbox_demo" ( "id" int4 DEFAULT nextval('sandbox_demo_id_seq'::regclass) NOT NULL, "source" text DEFAULT NULL NOT NULL, "created" timestamptz(6) DEFAULT NULL NOT NULL, "modified" timestamptz(6) DEFAULT NULL NOT NULL ) However, this table only supports plain entries and there's no parent child relationship or a mapping table so I could have revisions of the initial code snippet. What would be an ideal way of

Remove objects from query if None or Null

谁说胖子不能爱 提交于 2021-01-28 00:48:46
问题 I am trying to build a query that takes form data and removes any None or " " submissions but I'm not sure how to approach the logic. Here is the code; @app.route('/filterassets', methods=['GET', 'POST']) def searchassets(): form = FilterAssetsForm() results = None if request.method == "POST": if form.validate_on_submit(): try: horsepower = form.horsepower_search.data voltage = form.voltage_search.data rpm = form.rpm_search.data results = Motor.query.filter_by(horsepower=horsepower, voltage