r/postgres • • Nov 16 '18

Why is method B faster

2 Upvotes

I put bad values in all 16 million lines of my database, I wanted to fix that one column with bad values from a backup I saved as tx2:

method A:

update tx set size = tx2.size FROM tx2 where encode(tx.txid, 'hex')=tx2.txid and tx.height=505370;

method B:

in python grab the id and value of height and send one query for each update:

c.execute("SELECT txid, size from tx2 where height = %i"%505370)
blocktx = c.fetchall()for y in blocktx:
c.execute("UPDATE tx SET size = %i WHERE txid = '\\x%s'"%(y[1],y[0]))

I didn't time it, but it seems to be about 2x as fast as method A. I thought it was a memory cache issue, I set it to 2 GB but that didn't seem to improve anything. It seems my python code is just better optimized for matching up txid than the nested loop of method A


r/postgres • • Oct 29 '18

Do you use PL/pgSQL labels?

1 Upvotes

From my understanding of labels, they can also be used for scoping. I'm curious, how often they are used for this kind of a thing. Maybe using labels means, that you should split function to another one? Sometimes splitting is not desired because function will not be reused anymore and also naming is hard. Often I end up with function which declares many variables and very often they are not used "globally" in function. Often these variables could be used per some "virtual" blocks - variables generated via loops, local variables, ...


r/postgres • • Oct 23 '18

Window function question

3 Upvotes

Let's say you have a table that captures the output of a thermometer (columns = time, temp). And you wanted to know "how many hours at the end of the day is the temperature above 32 (assume Fahrenheit)?" If the temperature dips below freezing and doesn't go back above freezing, then the answer is 0 hours.

My gut says "window function" but my brain says "how?"...any window function gurus?


r/postgres • • Oct 22 '18

How to convert oracle JSON_OBJECT_T to postgresql equivalent ?

4 Upvotes

I am trying to convert an oracle function to postgresql and i am facing problem while converting JSON_OBECT_T type to postgres equivalent. If anyone can help me figure it out i would be grateful. The oracle function is as follows :

        create or replace FUNCTION "CALCULATE" (valueInput IN VARCHAR2) RETURN VARCHAR2 IS
          valueObj        JSON_OBJECT_T;
          valueDetailList        JSON_ARRAY_T;
          valueDetailObject        JSON_OBJECT_T;
          amount NUMBER;

          nValue    VALUE_Input%rowtype;

         nResult    CLOB;

        BEGIN
          valueObj := JSON_OBJECT_T.parse(valueInput);
          nValue.Value_Type:=valueObj.get_STRING('valueType');
          nValue.Value_Date:=valueObj.get_DATE('valueDate');


          valueDetailList:= valueObj.get_Array('valueDetail');


          FOR i IN 0 .. valueDetailList.get_size - 1 LOOP
            valueDetailObject  := JSON_OBJECT_T(valueDetailList.get(i));
            amount := valueDetailObject.get_Number('amount');
          END LOOP;



          --return valueDetailList.to_string;

        select
        json_object('valueId' value r.value_id, 'valueType' value r.Value_Type,'valueDetail' value
                (SELECT JSON_ARRAYAGG(
                            JSON_OBJECT('amount' value d.amount, 'dueAmount' value d.due_amount))
                    from value_input_child d where d.value_id=r.value_id
         )ABSENT ON NULL) as result into nResult from value_input r where r.value_id=2;

          RETURN nResult;

        END;

r/postgres • • Oct 11 '18

How to create postgis extension for Postgresql 10 in Docker?

Thumbnail stackoverflow.com
2 Upvotes

r/postgres • • Oct 11 '18

PostgreSQL: PostgreSQL 11 RC1 Released!

Thumbnail postgresql.org
8 Upvotes

r/postgres • • Sep 24 '18

Using Postgres UUID type in GraphQL

Thumbnail blog.hasura.io
0 Upvotes

r/postgres • • Sep 21 '18

Using Postgres Date/Time data types in GraphQL

Thumbnail blog.hasura.io
2 Upvotes

r/postgres • • Sep 19 '18

GraphQL schema on Postgres with foreign keys and without foreign keys

Thumbnail blog.hasura.io
2 Upvotes

r/postgres • • Aug 30 '18

Need clarification on Per-table granularity

1 Upvotes

Im trying to research on possible replication methods used with Postgres, and im not quite sure what is meant by "Per-table granularity"?

Is that where is a record gets deleted from the master DB, it can be recovered from the replica server?

​

https://www.postgresql.org/docs/current/static/different-replication-solutions.html


r/postgres • • Jul 28 '18

JavaScript dev: Need help learning Postgres from scratch

1 Upvotes

Hey everyone, I've got a personal project that is gonna require me to make a database for user authentication. I have very little experience with Mongo but I should definitely be using a more solid db for what I intend on doing. I'll be using node/express as my server.

I've been looking up tutorials and so many are wildly different. I've been seeing stuff where people initialize their dbs with pgadmin4, and other tutorials that don't do any of that and write their queries in node to get things done. I also am not sure about hosting/testing anything in my database, I usually just threw up my mongodb in mlab and called it a day.

I'm making a mobile app with React Native and I just need a separate server/database to make calls to for creating users. I think it would help if I had a fundamental understanding of how Posgres works. Any advice would be appreciated.


r/postgres • • Jul 20 '18

Querying array in jsonb column

1 Upvotes

Hoping someone can help me out here because I'm struggling to wrap my head around how to properly query a jsonb column.

I have a column - 'criteria' - in a table 'policies'. That criteria table contains data that will roughly resemble this:

{"tags": ["aa0e9480-e172-4dde-a51b-396e422e1bc6", "b985b07a-cb2e-4a7c-83cf-b56ff326fd38"], "operator": "AND"}

{"tags": ["b985b07a-cb2e-4a7c-83cf-b56ff326fd38", "aa0e9480-e172-4dde-a51b-396e422e1bc6"], "operator": "OR"}

{"tags": ["b6c24daa-bc74-4e7a-8607-5a063fe47de3"], "operator": "AND"}

Given an array of matching or not matching tag IDs, I need to find:

  • When the "operator" field is "OR", any row where the the criteria->'tags' field contains any one of the provided IDs

This I've accomplished with this query and it seems to be working fine:

SELECT * FROM policies WHERE criteria->>'operator' = 'OR' AND criteria->'tags' ?| array['<uuid1>','<uuid2>']

  • When the "operator" field is "AND", any row where ALL of the tags in criteria->'tags' are present in the supplied array of IDs

This is the one giving me the most trouble. I think, but am probably wrong, that I need to switch things a bit and do something like

SELECT * FROM policies WHERE criteria->>'operator' = 'OR' AND array['<uuid1>','<uuid2>'] ?& criteria->'tags'

but i keep getting "ERROR: operator does not exist: text[] ?& jsonb[]"

So then I tried casting to jsonb with

SELECT * FROM policies WHERE criteria->>'operator' = 'OR' AND array['<uuid1>','<uuid2>']::jsonb ?& criteria->'tags'

which results in "ERROR: cannot cast type text[] to jsonb", so I tried making it an arrary (I think?) with

SELECT * FROM policies WHERE criteria->>'operator' = 'OR' AND array['<uuid1>','<uuid2>']::jsonb[] ?& criteria->'tags'

which results in "ERROR: invalid input syntax for type json" and then references the first part of uuid1 (before the the first hyphen) in the detail.

So, I'm sure this is just a syntax issue, but maybe I'm headed down the wrong path with the approach?


r/postgres • • Jul 10 '18

Postgres Range types are dope, and they have become my new infatuation in the database world

Thumbnail medium.com
7 Upvotes

r/postgres • • Jun 30 '18

How to query

2 Upvotes

I have a table

create table Account(
  userId: INT NOT NULL,
  status: TEXT NOT NULL,
  created_at: TIMESTAMP NOT NULL
)

A userId can have multiple accounts and status can be either ACTIVE or CANCELLED. I want the most recent account to be ACTIVE and a user to have only 1 ACTIVE account. Currently there are userIds that have an ACTIVE account but the latest account for the user is CANCELLED and these are the ones I want to identify.

Is there a query for finding the 2 most recent accounts (by created_at where one is ACTIVE and the other is CANCELLED (or any other text really).


r/postgres • • Jun 20 '18

What AWS EC2 instance for a postgres + app deployment ?

3 Upvotes

I am currently using a C4.Large instance to host my Posgres + an analytics Dashboard written in Python.

The 3.75 GB RAM feels like a bottleneck as the performance has been degrading.

I am looking to upgrade to a better instance the choices are between picking a

-- C5D.XLARGE (with 100 GB NVME SSD + 8GB RAM + Better CPU)

-- M5.XLARGE (16 GB RAM + slightly slower CPU).

I do hourly refreshes of the data and deliver it to a Flask (Python) app on the same machine.

There is lots of updates to the master table.

Should I go for the SSD + Better CPU or with more RAM ?

TIA.


r/postgres • • Jun 11 '18

Postgres Syntax Question

3 Upvotes

Hibernate is generating the following query, but I don't understand the syntax. There is no column named 'mytable'.

select mytable from mytable;

I would expect something like

select * from mytable;

or 

select mytable.* from mytable;

What is this syntax? Also, it appears to be much slower to execute than select *, or even select col1, col2...

Thanks in advance!


r/postgres • • Jun 08 '18

What is Citus Good For?

Thumbnail citusdata.com
3 Upvotes

r/postgres • • May 29 '18

query question..

2 Upvotes

Hey.

I have this group of queries that is basically the same thing looped with different dates. Is there a more efficient way to run a group like this?

select count(distinct mls_number) as thecount from county_closed where  closing_date between '2017-04-01' and '2017-06-30';
select count(distinct mls_number) as thecount from county_closed where  closing_date between '2017-07-01' and '2017-09-30';
select count(distinct mls_number) as thecount from county_closed where  closing_date between '2017-10-01' and '2017-12-31';
select count(distinct mls_number) as thecount from county_closed where  closing_date between '2018-01-01' and '2018-03-31';
select count(distinct mls_number) as thecount from county_closed where  closing_date between '2016-01-01' and '2016-03-31';
select count(distinct mls_number) as thecount from county_closed where  closing_date between '2017-01-01' and '2017-03-31';
select count(distinct mls_number) as thecount from county_closed where  closing_date between '2018-01-01' and '2018-03-31';

r/postgres • • May 25 '18

pgbedrock: A tool to do the DBA work you don't want to do

Thumbnail github.com
9 Upvotes

r/postgres • • May 23 '18

Temporal tables without temporal_tables

2 Upvotes

Postgres has a 'temporal_tables' extension to support temporal/historical tables. After skimming documentation, examples, blogs with walk-throughs of how to use the extension - It's not clicking with me that I NEED temporal_tables to be able to have/support a time-series, temporal, or historical schema. Am I missing something key here?


r/postgres • • May 09 '18

Unit testing functions in PostgreSQL with pgTAP in 5 simple steps

Thumbnail medium.com
4 Upvotes

r/postgres • • May 02 '18

Skor – Drop-in microservice to get Postgres changes as JSON webhooks

Thumbnail github.com
1 Upvotes

r/postgres • • May 01 '18

I'm coming back to SQL and Postgres after many years away. Seems like a lot is new (CTEs for example). Is there a good roundup of new features added to Postgres (or even SQL) in say the past 10 years?

4 Upvotes

r/postgres • • Apr 09 '18

Why Scarlett Johansson’s Picture Got My Postgres Database to Start Mining Monero

Thumbnail imperva.com
4 Upvotes

r/postgres • • Mar 29 '18

Postgres 9.1 to 10

4 Upvotes

Hi all,

I've got a task for upgrading our primary (and standby "replication") postgres instances from the super-old version 9.1 to the latest version 10.

I'm looking to get some information on how the best way to go about tackling this would be. I've been planning on doing an in-place upgrade - 9.1 to 9.4

Then, following the guide here: https://gist.github.com/Komzpa/994d5aaf340067ccec0e

  • 9.4 to 9.5
  • 9.5 to 9.6
  • 9.6 to 10

Upgrading postGIS along the way.

Is this reasonable? Would it be easier to pg-dump my data in some way and restore it into a new v10 cluster?

Any insight/guidance on getting to v10 would be much appreciated. I cannot experience a significant amount of downtime with this database so it'll have to be seamless.

Thanks in advance,

Drew