postgresql

Optimizing a postgres similarity query (pg_trgm + gin index)

喜夏-厌秋 提交于 2021-02-17 22:52:16
问题 I have defined the following index: CREATE INDEX users_search_idx ON auth_user USING gin( username gin_trgm_ops, first_name gin_trgm_ops, last_name gin_trgm_ops ); I am performing the following query: PREPARE user_search (TEXT, INT) AS SELECT username, email, first_name, last_name, ( -- would probably do per-field weightings here s_username + s_first_name + s_last_name ) rank FROM auth_user, similarity(username, $1) s_username, similarity(first_name, $1) s_first_name, similarity(last_name, $1

Rails - only find records where has_many associated records are present

核能气质少年 提交于 2021-02-17 19:21:26
问题 I have a model with a has_many relation with another model like so: class Parent < ActiveRecord::Base has_many :children end class Child < ActiveRecord::Base belongs_to :parent end Since there are some parents that may not have children, I'd like to do a query that returns only the parents that DO have children. How would I go about something like this? Since I'm doing this on Rails 3, it would be helpful if this query were to not use the where.not. syntax. 回答1: Rails 4 Parent.includes(:child

How to serialize a queryset from an unrelated model as a nested serializer?

旧城冷巷雨未停 提交于 2021-02-17 09:41:30
问题 I'm trying to add a nested serializer to an existing serializer based on some criteria of the parent model, not a Foreign key. The use case is to return a 'Research' object with an array of 'ResearchTemplate' objects that are identified by filtering on a Postgres ArrayField. Models class Research(TimeStampedModel): category = models.CharField(max_length=100, choices=RESEARCH_TEMPLATE_CATEGORIES, default='quote') body = models.CharField(max_length=1000, blank=True, default='') #The body of

Prevent SQL Injection In This PHP Code

不问归期 提交于 2021-02-17 07:17:06
问题 I have the following function that writes into a PostgreSQL database. I need to make it safe from SQL injection however I am not sure how to do that. The part of the query assembled from pg_query_params is safe from injection (or so I have been told) however the other part of the assembled query via PHP's string concatenation . is apparently vulnerable to injection. private function setItem($table, $id, $field, $itemId, $fieldValue){ $_1 = $itemId; $_2 = $fieldValue; $_3 = $field; $_4 =

How to remove trailing spaces and change encoding in char(n) columns in dynamic ExecuteQuery

流过昼夜 提交于 2021-02-17 07:07:21
问题 ASP.NET MVC Core 5 application uses Npgsql to get data from Postgres database. Columns in database are defined as CHAR(n) type, like: create table prpalk ( sfirmanimi char(100) ); Column types cannot be changed to varchar. Postgres database encoding is unicode. However some columns are in custom encoding which needs to be converted to unicode to be used in .NET. Those column names in database start always with word Custom. I tried Dapper to get data but data contains trailing spaces and

How to remove trailing spaces and change encoding in char(n) columns in dynamic ExecuteQuery

a 夏天 提交于 2021-02-17 07:06:15
问题 ASP.NET MVC Core 5 application uses Npgsql to get data from Postgres database. Columns in database are defined as CHAR(n) type, like: create table prpalk ( sfirmanimi char(100) ); Column types cannot be changed to varchar. Postgres database encoding is unicode. However some columns are in custom encoding which needs to be converted to unicode to be used in .NET. Those column names in database start always with word Custom. I tried Dapper to get data but data contains trailing spaces and

Postgres db design Normalize tables or Use Array Columns

亡梦爱人 提交于 2021-02-17 07:03:25
问题 Newbie trying to figure out the best way to design a Postgres db for the following use case scenario. There is an Account table for the business customers and there is a contacts table with a column relationship. account.pk_id, …. contacts.pk_id, contacts.fk_accountid … Thousands of different businesses in the Accounts table will be storing millions of contacts each in the Contacts table. Each contact record will over time belong to between 1 and 100 different categories, lists and products.

Equivalent or alternative method for RAISE EXCEPTION statement for function in LANGUAGE sql?

我们两清 提交于 2021-02-17 06:59:41
问题 Is there an equivalent (or workaround) for the RAISE EXCEPTION statement for the function written below in LANGUAGE sql ? CREATE OR REPLACE FUNCTION fn_interpolation (p_yearinteger integer, p_admin_id integer, p_crop_id integer, p_cropparameter integer) RETURNS TABLE (value double precision, remark text) AS $$ WITH yearvalues AS (SELECT yearinteger, value FROM cropvalues WHERE crops_id = p_crop_id AND admin_id = p_admin_id AND parameter_id = p_cropparameter), I need the function to abort and

PostgreSQL: How escape '?

半腔热情 提交于 2021-02-17 06:53:05
问题 My original sql: INSERT INTO clients (name, phone) VALUES ('Vs'emandon', '333026660'); I read about E'' and tried this: INSERT INTO clients (name) VALUES ('VsE'''emandon); But this not working. http://www.sqlfiddle.com/#!15/f717e/2 回答1: Postgres follows the SQL standard. Quote from the manual: To include a single-quote character within a string constant, write two adjacent single quotes, e.g., 'Dianne''s horse' . Note that this is not the same as a double-quote character ( " ). INSERT INTO

PostgreSQL免费公开课第18期-控制文件管理与恢复

江枫思渺然 提交于 2021-02-17 06:52:42
十八、PostgreSQL控制文件管理与恢复 1、控制文件的结构 2、如何维护控制文件 3、有备份控制文件损坏恢复 4、无备份控制文件损坏恢复 PG的控制文件与Oracle的控制文件一样重要,如果缺少直接影响数据库的启动与备份恢复,本次计算沙龙对PG控制文件的内容和结构进行详细的阐述,并介绍如何如何在没有备份控制文件的环境下恢复控制文件,是对数据库进行恢复的利器。 PG控制文件卡 通通告诉你方法 主讲:CUUG陈卫星老师 时间:2020-07-18 20:00-21:00 地址:腾讯课堂搜索‘cuug’ 来源: oschina 链接: https://my.oschina.net/u/3902946/blog/4403666