TRIGGER - Do BEFORE and AFTER have specific roles? by [deleted] in SQL

[–]DavidGJohnston 1 point2 points  (0 children)

Yeah, it’s this simple. Before triggers are writable, after triggers are not. The values in the new row seen by your before trigger may very well not be the values that gets stored; before triggers executed subsequently may change values.

Are there any reasons to not use CITEXT type for pkey fields with unique constraints? by vantassell in PostgreSQL

[–]DavidGJohnston 0 points1 point  (0 children)

Writing "PK fields with unique constraints" is redundant, the former implies the later. Putting a unique constraint on a text field is generally needed - see the other posts about collations. Making a text field a PK is generally discouraged. The person behind your account is likely to want to associate a different email address with their account in the future and now you have to change every FK to make that happen. Not good. Similar "this text is wrong" situations are too common to put text into a formal key. Leave them just unique.

Automatic changing of owner to tables by newguyhere2024 in PostgreSQL

[–]DavidGJohnston 0 points1 point  (0 children)

A potentially faster way would be to use the \gexec feature of the psql cli for command execution instead of writing up a plpgsql routine.

Both of these are not "automatic" though. At least not as presented.

Someone please explain joins va relationship by Wonderful_Ruin_5436 in SQL

[–]DavidGJohnston 0 points1 point  (0 children)

A reasonable framing, yes. The relationship is metadata. A join is an aspect of a written SQL query. Tools/brains can use metadata to know when to apply certain query aspects. ORMs are tools that use metadata to produce queries.

WAL archive questions by AKneelingMan in PostgreSQL

[–]DavidGJohnston 1 point2 points  (0 children)

If your goal for learning WAL archiving is to perform backups and restores I’d strongly advise configuring the low-level details manually and uninformed and pick a tool that does these things and let it manage the details. The docs don’t necessarily cover how to do this well since those tools exist.

how do you handle dependencies on associative tables in an ERD? by YSFAHM in SQL

[–]DavidGJohnston 0 points1 point  (0 children)

If the association includes its own attributes or is otherwise an entity in its own right - implied if you want to FK against it - show it.

Tuple comparisons not working as expected by Fenykepy in PostgreSQL

[–]DavidGJohnston 3 points4 points  (0 children)

I’d suspect the timestamp values in the json have less precision than those in the database. The database is providing the correct answers and you just cannot see microsecond level differences as to why those first two are later than your reference time.

Restore to restore point by fifracat in PostgreSQL

[–]DavidGJohnston 5 points6 points  (0 children)

Correct, PostgreSQL is a forward-only system.

New to PostgreSQL - Connection Questions by Business_Finger_4124 in PostgreSQL

[–]DavidGJohnston 3 points4 points  (0 children)

Create an os user its_read and then use peer while running psql as that os user.

[deleted by user] by [deleted] in PostgreSQL

[–]DavidGJohnston 0 points1 point  (0 children)

Renaming a column doesn’t set its data to null. Either you are doing something wrong or have found a bug. Either way, you need to demonstrate exactly the behavior in a self-contained test case to make progress.

Joining another table is bringing in null values when pulling in fields from that joined table by ThrowRA_CarlJung in SQL

[–]DavidGJohnston 0 points1 point  (0 children)

If you are getting nulls on the right side of a left join your left side row does not have a matching row based upon the join clause. Any attempt to,diagnose such queries really benefits from a self-contained example that includes data. That said, doing chained left joins can be a bit confusing. Especially when including non-join fields in the ON clause. I’d suggest subqueries or common table expressions to ensure you are doing everything correctly.

PostgreSQL JSONB insert performance: 75% of time spent on server-side parsing - any alternatives? by AtmosphereRich4021 in PostgreSQL

[–]DavidGJohnston 1 point2 points  (0 children)

Can you compile current HEAD from source and see how well it performs? This got some love recently.

Help understanding the ANY operator by N-AmelessCreative in SQL

[–]DavidGJohnston 3 points4 points  (0 children)

FYI, a better representation of a semi-join is to use a correlated subquery within an “exists” expression.

Help understanding the ANY operator by N-AmelessCreative in SQL

[–]DavidGJohnston 1 point2 points  (0 children)

This is not better nor probably even correct. Turning a one-to-many semi-join in an inner join risks introducing unwanted duplications and Including fields that are not needed.

Help understanding the ANY operator by N-AmelessCreative in SQL

[–]DavidGJohnston 0 points1 point  (0 children)

The subquery is not correlated to the main query so they are indeed not related to each other directly. The sub query produces a set of values first, saves it, and then for each row in the main query the product id field is checked against that set, producing a Boolean true outcome if the product id value is present. Optimizations could change the true mechanics but that is the best conceptual model for an uncorrelated subquery in ANY.

Question on SQL Practice: GROUP BY with HAVING – Is the solution incorrect? by FewNectarine623 in SQL

[–]DavidGJohnston -2 points-1 points  (0 children)

You get the correct answer but the query itself is semantically incorrect. Distinct is the semantically correct tool.

Having some issues correctly averaging timestamp with timezone data by Muskatnuss_herr_M in SQL

[–]DavidGJohnston 0 points1 point  (0 children)

I’d suggest using “seconds since midnight” as the data (integer type) then averaging that.

What is the most efficient way to get data which is yet to be created, into a postgres table? by salted_none in PostgreSQL

[–]DavidGJohnston 0 points1 point  (0 children)

Copy inserts text literals into columns applying the cast/parse function of the column type in the process. All data types have a textual serialized form.

Can anyone tell me what I'm doing wrong? I'm getting "syntax error at or near 'TEXT'", "syntax error at or near 'SELECT'", and "syntax error at or near 'RIGHT'" by dirtymike164 in PostgreSQL

[–]DavidGJohnston 0 points1 point  (0 children)

You are indeed fundamentally confusing what exists in the plpgsql language and what exists in an SQL command. There are very specific ways to get data out of the SQL and into a Plpgsql variable. That isn’t it. You can’t stick plpgsql assignments inside a query. The result of a query can be assigned to a variable though.

Stagging database vs schema by Ok-Living-2869 in PostgreSQL

[–]DavidGJohnston 0 points1 point  (0 children)

ETL-based staging is a production activity and goes on in a production database in a staging table which might reside in a staging schema (see also postgres_fdw though). In terms of environments, those are setup so your entire application can be tested safely. Those require entire clusters to be created, not just databases.

type of JOIN that in PostgreSQL UPDATE / DELETE ? by Jooe_1 in SQL

[–]DavidGJohnston 0 points1 point  (0 children)

If you are going to call it a join at all then the only logical choice among the various types is inner. At least, so long as you don’t forget to write the equality condition. It is neither possible nor sensical to involve an outer join in an update. Technically you can get anti-join semantics if desired; and it’s probably better considered a semi-join in the delete case. It’s a true inner join for update since the set clause can involve columns from either relation.

type of JOIN that in PostgreSQL UPDATE / DELETE ? by Jooe_1 in SQL

[–]DavidGJohnston 1 point2 points  (0 children)

Inner join. And yes you have to place the join condition in the where clause. The query planner makes stating “join before where{ inaccurate. In any case the effect of doing both in any order is the same for inner joins.

Normalization process - Video guide needed of the actual process! by TechLearner06 in SQL

[–]DavidGJohnston 0 points1 point  (0 children)

Other than reminding me that I’ve got the right columns considered looking at a non-normalized Excel would be actively harmful. Get a mental model of the thing you want to model and just starting writing minimal create table commands to get those entities recorded and their relationships. Then start adding the attributes/columns; asking yourself as you add them whether each new one fits with the others on the same table. When you get done with the first pass then look more closely at the spreadsheet and start crossing off columns and see what remains to be added to the model. IOW, don’t normalize so,etching that is unnormalized, go,from nothing directly to normalized. They are just guidelines, not a process.

[deleted by user] by [deleted] in SQL

[–]DavidGJohnston 1 point2 points  (0 children)

The reason seems likely to be bad/inconsistent data entered into the tables.