postgresql-9.1

Declaring the tuple structure of a record in PL/pgSQL

◇◆丶佛笑我妖孽 提交于 2019-11-27 06:31:31
问题 I can't find anything in the PostgreSQL documentation that shows how to declare a record, or row, while declaring the tuple structure at the same time. If you don't define you tuple structure you get the error "The tuple structure of a not-yet-assigned record is indeterminate". This is what I'm doing now, which works fine, but there must be a better way to do it. CREATE OR REPLACE FUNCTION my_func() RETURNS TABLE ( "a" integer, "b" varchar ) AS $$ DECLARE r record; BEGIN CREATE TEMP TABLE tmp

PostgreSQL ERROR: canceling statement due to conflict with recovery

一个人想着一个人 提交于 2019-11-27 04:59:03
问题 I'm getting the following error when running a query on a PostgreSQL db in standby mode. The query that causes the error works fine for 1 month but when you query for more than 1 month an error results. ERROR: canceling statement due to conflict with recovery Detail: User query might have needed to see row versions that must be removed Any suggestions on how to resolve? Thanks 回答1: Running queries on hot-standby server is somewhat tricky — it can fail, because during querying some needed rows

PSQLException: ResultSet not positioned properly, perhaps you need to call next

这一生的挚爱 提交于 2019-11-27 03:35:30
问题 public UserBean authenticate(String username,String password){ PostGresDAO pg=new PostGresDAO(); //creates new connection Connection conn=pg.getConnecion(); //return connection object PreparedStatement ps; ResultSet rs; String query="select password,name from scg_users where username=?"; UserBean ub=null; boolean authenticated=false; try{ ps=conn.prepareStatement(query); ps.setString(1, username); rs=ps.executeQuery(); if(rs!=null){ authenticated=password.equals(rs.getString(1)); //exception

How to restore a single table from a .sql postgresql backup?

混江龙づ霸主 提交于 2019-11-27 03:20:09
问题 A table's rows were mistakenly deleted from the database. We have a db backup which results in a sql file that can restored like so: psql -h localhost -d proddump -f /Users/U/Desktop/prod_db_backup/PostgreSQL/site_prod.sql This ends up doing a full restore locally. But what we need is to restore a single table's rows to production. Any tips on how to make this work with PostgreSQL 9.1? Thanks 回答1: Don't do SQL backups if you need single table restore, etc. Use pg_dump's -Fc option - the

Postgres query optimization (forcing an index scan)

落爺英雄遲暮 提交于 2019-11-27 02:38:04
问题 Below is my query. I am trying to get it to use an index scan, but it will only seq scan. By the way the metric_data table has 130 million rows. The metrics table has about 2000 rows. metric_data table columns: metric_id integer , t timestamp , d double precision , PRIMARY KEY (metric_id, t) How can I get this query to use my PRIMARY KEY index? SELECT S.metric, D.t, D.d FROM metric_data D INNER JOIN metrics S ON S.id = D.metric_id WHERE S.NAME = ANY (ARRAY ['cpu', 'mem']) AND D.t BETWEEN

If PostgreSQL count(*) is always slow how to paginate complex queries?

匆匆过客 提交于 2019-11-27 01:17:33
问题 If PostgreSQL's count(*) is always slow how to paginate complex queries? Making triggers doesn't seem to be a good solution as long as in this case we have a lot of pages (for example different categories, filters, etc). What to do if VACUUM/VACUUM ANALYZE/ANALYZE/VACUUM FULL doesn't help? What are the best practices to use count(*) with postgresql? 回答1: Did you read the heading on that article? Note that the following article only applies to versions of PostgreSQL prior to 9.2. Index-only

Postgresql insert trigger to set value

久未见 提交于 2019-11-27 00:41:32
问题 Assume in Postgresql, I have a table T and one of its column is C1 . I want to trigger a function when a new record is adding to the table T . The function should check the value of column C1 in the new record and if it is null/empty then set its value to 'X' . Is this possible? 回答1: You are correct that you need a trigger, because setting a default value for the column won't work for you - default values only work for null values and don't help you in preventing blank values. In postgres

How to pass custom type array to Postgres function

╄→гoц情女王★ 提交于 2019-11-26 23:36:53
问题 I have a custom type CREATE TYPE mytype as (id uuid, amount numeric(13,4)); I want to pass it to a function with the following signature: CREATE FUNCTION myschema.myfunction(id uuid, mytypes mytype[]) RETURNS BOOLEAN AS... How can I call this in postgres query and inevitably from PHP? 回答1: You can use the alternative syntax with a string literal instead of the array constructor, which is a Postgres function-like construct and may cause trouble when you need to pass values - like in a prepared

Check if NULL exists in Postgres array

时光总嘲笑我的痴心妄想 提交于 2019-11-26 22:25:26
问题 Similar to this question, how can I find if a NULL value exists in an array? Here are some attempts. SELECT num, ar, expected, ar @> ARRAY[NULL]::int[] AS test1, NULL = ANY (ar) AS test2, array_to_string(ar, ', ') <> array_to_string(ar, ', ', '(null)') AS test3 FROM ( SELECT 1 AS num, '{1,2,NULL}'::int[] AS ar, true AS expected UNION SELECT 2, '{1,2,3}'::int[], false ) td ORDER BY num; num | ar | expected | test1 | test2 | test3 -----+------------+----------+-------+-------+------- 1 | {1,2

Test for null in function with varying parameters

情到浓时终转凉″ 提交于 2019-11-26 22:22:31
问题 I have a Postgres function: create function myfunction(integer, text, text, text, text, text, text) RETURNS table(id int, match text, score int, nr int, nr_extra character varying, info character varying, postcode character varying, street character varying, place character varying, country character varying, the_geom geometry) AS $$ BEGIN return query (select a.id, 'address' as match, 1 as score, a.ad_nr, a.ad_nr_extra,a.ad_info,a.ad_postcode, s.name as street, p.name place , c.name country,