Month: December 2023 (page 1 of 1)

Differences Between Postgres & SQL Server

A couple of weeks ago, one of my coworkers sent our group an article from Brent Ozar that they found interesting. The article is “Two Important Differences Between SQL Server and PostgreSQL”, which is a short and sweet discussion about two differences between the database engines that might trick or confuse a developer who’s learning PostgreSQL after working with SQL Server (someone exactly like me). After I read his post, I went down a rabbit hole of researching all of the little things left unsaid in the article that I hadn’t thought about yet since I’m only beginning my transition into Postgres. I recommend you go read that article, but here are the takeaways I learned from it as well as the extra information I learned from my additional googling.

CTEs are very similar but also different in Postgres vs. SQL Server

As Brent Ozar says in his post, CTEs are fairly similar between Microsoft SQL Server (MSSQL) and Postgres, but the main difference is in how the queries are processed by the engines which can affect how you should format your queries and their filters. If you don’t change your CTE writing style when switching to Postgres, you may end up with performance issues. This scares me a little simply because I’m sure this isn’t the only “gotcha” I’ll encounter while transitioning our systems to Postgres. However, I will always look at the whole project and each new challenge as a learning experience so that I don’t get overwhelmed by the mistakes I’m bound to make.

The one good thing I learned in my research about Postgres CTEs after reading Brent’s article was that I can continue to use Window Functions just the same with them, which is usually why I use CTEs with my current work anyway. I use the ROW_NUMBER function with CTEs quite often, so I was happy to see that the function also exists in Postgres.

I have taken simple query formatting for granted my entire career

After reading Brent’s post and the other subsequent articles to learn more about Postgres DO blocks, I came to the realization that I’ve really taken for granted the nice and simple query formatting, for both ad hoc and stored queries, that SQL Server and T-SQL provide. Even if I’m running a one-time query for myself to get data needed to develop a stored procedure, I use IF statements quite frequently and that is so simple to do with SQL Server. I only need to write “IF… BEGIN… END” and I have a conditional code block that I can run immediately to get the data I need.

Doing that in Postgres is a little more complicated, especially if I’m wanting these conditional statements to give me a result set to review since that isn’t possible with DO blocks in Postgres. In order to run conditional statements with Postgres, you need to use a DO block to let the query engine know that you’re about to run conditional logic. That itself is simple enough to adapt to, but there was one aspect of these code blocks that confused me for too long, which was the “dollar quote” symbols often seen in Postgres code examples. These were confusing because I wasn’t sure why they were needed or when to use them and everyone used to working with Postgres took that information for granted and never explained it with their examples.

In Postgres, when you define a function or DO block, the code within that block must be encapsulated by single quotes, which is different from SQL Server which doesn’t require any kind of quote encapsulation for the code contained in an IF statement. Most developers use the dollar quote styling method instead of single quotes because doing so prevents you from having to escape every single special character you may be using in your code block, such as single quotes or backslashes.

DO $$
<code>
END $$;

Additionally, it is possible to specify tags between the beginning and ending double dollar signs (ex: DO $tag$ <code> $tag$) which can help you organize your code to make it easier to read. As far as I can tell, the tags do not provide any function besides styling code for easier reading.

Once I understood these dollar quotes better, I felt more confident about being able to write Postgres SQL code. But there were still a few other catches with the code that I wasn’t expecting, coming from MSSQL.

Postgres is very specific about when you can and can’t return data from your query/code

Of all the Postgres info that I have learned so far, I think this one is going to get me the most due to how I write queries on a day-to-day basis. As I said above, I write a lot of ad hoc queries throughout the day to view and validate data for many purposes. Postgres’ rules about what you can return data from are much different than MSSQL. In SQL Server, it seems like you can return/select data from anywhere your heart desires, including functions, procedures, conditional statements, and ad hoc queries. Postgres only allows you to return data from two of those options, functions and ad hoc queries (as long as it doesn’t include conditional logic).

If you have a chunk of code that you want to save to run again later in Postgres, and that code returns data with a SELECT statement or a RETURN statement, you must use a function and not a stored procedure. Stored procedures in Postgres are only meant to perform calculations or to do anything else besides return data. If you put a SELECT statement in a procedure in Postgres, it will NOT display that data like you would expect or like you are used to with SQL Server. This will affect my organization greatly when we move to Postgres because we have a lot of stored procedures whose sole purpose is to return specified data with a SELECT statement. All of those procedures will need to be converted to Postgres functions (which isn’t that big a deal, but it definitely needs to be kept in mind).

You also are unable to return data from a DO block with conditional statements as Brent Ozar mentioned in his post. If you want to have conditional logic that returns data for you to review, that will need to be done with a function. This one is a little crazy to me since that is the opposite of what’s possible with SQL Server. I can run this statement without issue as an ad hoc query in SQL Server, but it wouldn’t work in Postgres DO block:

DECLARE @Today VARCHAR(10) = 'Friday';

IF (@Today = 'Friday')
BEGIN
	SELECT 1 AS TodayIsFriday
END
ELSE
BEGIN
	SELECT 0 AS TodayIsFriday
END

This will take getting used to.

Postgres has the ability to understand other programming, scripting, and query languages

This is the most interesting new thing I learned about Postgres with this research. It is fascinating to me that this database engine allows you to easily write and execute code written in other languages and that it will work just as easily as normal SQL code. When you write a function in Postgres, you are able to specify what language you are writing the code in, which can be many different languages. If you don’t specify any, it will default to standard SQL.

Postgres comes with four different language modules installed, which are PL/pgSQL, PL/Tcl, PL/Perl, and PL/Python. The “PL” in those module names stands for “Procedural Language”. Tcl and Perl aren’t as interesting to me personally since I’ve never heard of the first and have never used the second, but the other two built-in language options intrigue me.

PL/pgSQL interests me because that’s the standard Postgres SQL language module available which gives you a lot of coding features that standard SQL doesn’t provide, such as custom functions, procedures, complex computations, etc. The types of functionality that I assumed were normal for database engines since they’re built-in to T-SQL/SQL Server.

PL/Python also interests me since I’m learning Python at the moment, in addition to learning Postgres, and it seems like the most useful scripting language I’ve ever used. If you can integrate Python with the database engine so easily, I can see getting a lot of use out of that to pull and analyze data. But I haven’t yet used this functionality with Postgres so I can’t say for sure.

Conclusion

Overall, I am excited to learn these new things about Postgres because I am starting to feel more ready for our migration from SQL Server to PostgreSQL. As I’ve written in posts before, I am a natural procrastinator and had been procrastinating starting to learn Postgres until my coworker sent me the Brent Ozar article. I am super thankful that he sent that, because it led me down this wonderful rabbit hole of learning many differences between Postgres and SQL Server that I’m sure will benefit me going forward.

The below list is all of the resources I used while learning the above information. I thought the EnterpriseDB link was the best overall summary of different topics while the rest really get into the weeds of different Postgres features.

  • https://www.postgresql.org/docs/current/sql-createprocedure.html
  • https://www.enterprisedb.com/postgres-tutorials/everything-you-need-know-about-postgres-stored-procedures-and-functions
  • https://www.postgresql.org/docs/current/sql-do.html
  • https://stackoverflow.com/questions/12144284/what-are-used-for-in-pl-pgsql
  • https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING
  • https://www.geeksforgeeks.org/postgresql-dollar-quoted-string-constants/#
  • https://www.postgresql.org/docs/current/tutorial-window.html
  • https://www.geeksforgeeks.org/postgresql-row_number-function/
  • https://www.postgresql.org/docs/8.1/xplang.html
  • https://www.postgresql.org/docs/current/plpgsql-overview.html
  • https://www.postgresql.org/docs/current/sql-createfunction.html

How to Clean Up Old Local Branches With Git

If you use Git Bash or another form of Git on your local development machine for version control in your organization, have you ever looked at how many branches you have from old work? Sometimes I forget that Git is keeping every single branch I’ve ever made off of master in the past unless I manually go in and delete it. This means I end up with an insane number of local branches hanging out on my computer, some of them months old. It’s not necessarily bad that these local branches are still around, but I know that I will never need them again after the related ticket or software change has been deployed to Live. Any pertinent information that might be needed for reference for that branch is stored in our remote repo which means I don’t need it hanging around on my machine.

When I finally remember to check how many local branches I have on a repo (using the command “git branch”), I am shocked to see dozens upon dozens of branches like in the above screenshot (which is only about half of the old branches I have on that repo). Then I want to get rid of them but also don’t want to use “git branch -D <branch>” for every single individual branch to clean them up one by one since that would take me quite a while to complete.

The faster way to get rid of all local branches, taught me by a coworker, is the following: “git branch  | grep -v “master” | xargs git branch -D”. Note: use this with caution because it will delete everything and you don’t want to delete something that you still need. Also, there are some caveats with which this command won’t work, and you can read more about that on StackOverflow.

TL;DR: the above command will fetch the list of all branches available on the current directory/repo, will get all branches except the one you specify with “grep -v” (so that you can keep the local master branch), and will then delete all of those branches with a force delete.

Let’s break down each part of that command:

  • “Git branch”
    • This is the command that will list all local branches on the current repository
    • Using a pipe character (the vertical bar “|”) will tell the command to feed in the results of what’s on the left of the pipe into the command on the right of the pipe, which in this case means we are feeding the list of local branches into the command “grep -v “master””
  • “grep -v “master””
    • The grep command will print output matching a specified pattern
    • The option “-v” signifies that the inverse of the list matching a pattern should be output
    • In this scenario, the two above points mean that this full command is going to take the list of all local branches and print out all of them that aren’t the specified branch, which in this case is “master”. If your main branch isn’t called master, you can change that value to whatever branch you don’t want to delete with this command.
  • “xargs git branch -D”
    • I haven’t been able to definitively figure out what the xargs command is doing (if anyone has documentation on this, please send it my way!), but essentially it seems to be taking the list of branches created with the two previous commands and running that list through the normal “git branch -D” command which will perform a hard delete on those branches.
    • “git branch -D” is the command used to force a delete of a branch (the -D is short for using the two options “–delete –force”)

This isn’t the most necessary Git command you’ll ever use in your work, but it does come in handy to keep your work organized and decluttered if you’re someone like me who values that.

How to Stay Organized While Busy at Work, Part 2

Welcome back to part two of my discussion on ways to help manage stress and chaos at work. These posts are specifically aimed at database and application developers and IT personnel as a whole, although I think most of the topics would also apply to other office workers. Today’s post will cover the final three topics in the list below. If you would like to catch up on last week’s part one post, you can find it here.

Once again, I hope you find something valuable in this post, and I would love to hear your thoughts about how you’ve implemented these and had them work (or not!) in your own life. Thanks for reading!

  1. Your calendar is your best friend, use it to your advantage
  2. Only work on one thing at a time
  3. Every work request should be a ticket (mostly)
  4. Set times to catch up on email and messages throughout the day
  5. Do the hardest and most important work first thing in the morning
  6. Track what you spend your time on

Set times for yourself to catch up on emails and messages throughout the day

Going back to the advice to only work on one thing at a time, that should also include emails and messages. If you’ve never heard of the concept of context switching, I think you should read into it because it can be a huge time-sink if you don’t control it as much as possible. (Read about context switching from Atlassian here.)

While I am working, I find that if I am constantly being bombarded with messages and emails from other people, I get incrementally more stressed as the day goes on because it all starts to be overwhelming, especially when the group chats are going at 100 miles per hour some days. Plus, the context switching of always losing focus to read what new chats are coming in as they come in is tiring and prevents me from getting important things done. This is why I’ve set a rule for myself that I will use Focus Time like I said in part one, and will give myself dedicated time throughout the day to focus on working, but will also mentally set times throughout the day to stay connected with coworkers.

I try to work with a loose version of the Pomodoro Method of intensely focusing on a task for 25-50 minutes and then taking a 5-10 minute break from that task. I used an actual timer app in the past, but now I don’t use it and mentally keep track of how long I’ve been focused on one thing. Then when the break time comes, I will quickly catch up on and respond to any messages I’ve received while focusing. Emails I only check about 3 times a day, after my morning standup, before going to lunch, and before leaving for the day, since usually email is not as urgent as Teams messages. This method gives you the best of both worlds of still being able to help your teammates with their work while still being able to get your work done without too much context switching.

Do the hardest and most important work first thing in the morning

This concept is what many in the self-help world refer to as “eat the frog”, which I think is a pretty weird name, but the concept is a good one. I am a natural procrastinator. You would have thought I would have learned my lesson with many late nights in college trying to finish work the night or two before a deadline, but I guess not. My procrastination comes from a fear that I won’t know what to do when I get into the task I need to complete, and then it will be scary because I won’t know how to proceed. However, not once in my professional career so far have I ever been assigned a task that I knew 0% about what needed to be done, even if all I know about the task is that someone wrote a vague document about it 5 years ago or that it needs to use recursion to get the data.

With all of this in mind, in about the last 6 months or so, I’ve focused on starting my day out by working on the most challenging and important tasks first thing in the morning. I usually start work around 7 AM and have my first meeting each day a 9:15 AM, so I know I usually have about 2 hours to buckle down and focus on that one difficult task, so I have no excuse to not focus on the task. I still don’t like doing the challenging thing, but doing it first thing in the morning makes sure I have the mental energy to do it because I’m freshly awake and am sipping on my morning coffee. Starting early also means that when I inevitably make good progress on it before lunch, I can have a sense of accomplishment and a lighter load after lunch (when the onslaught of meetings normally begins). For the scenarios where I don’t make significant progress in the morning because the task is that challenging, at least I know I put my best effort of the day into it to make some amount of progress instead of procrastinating on it and becoming even more afraid of the task at hand.

Track what you spend your time on

This relates to my earlier advice about making every work request a ticket, but this advice to track what you spend your time on isn’t exclusive to ticket work. I also recommend that you track the time spent on other activities as well so you know what truly is taking a lot of time in your work day. If you are constantly helping others (which can be good) instead of doing your work, you may look back on the day or week and wonder where your time went and why you weren’t able to finish your tasks. I have been in that exact position, which is why I started keeping track of all the work I complete throughout the day, including small calls with teammates to help them with their work, meetings, and even presentation work time. I do this, including keeping a general summary of what I did or learned with each of my tickets, with a daily OneNote page, but you can track it in any way you would like.

In the past, I tried using Trello and other software to do this timekeeping for me, but it always ended up being too complicated so I would inevitably stop using the software and stop tracking what I was working on. This year, I decided to make a OneNote page for each day of work and made a default template to use for all new pages that would give me the list of things I want to track each day. Then at the end of the week, I compile and summarize the daily work into a page that I call “Weekly Summary”, and add any notable work into my list of accomplishments for the year. This method speeds up and eases the process of identifying accomplishments so that when the annual review time comes around, I will have plenty of items to pull from my list instead of trying to remember it all myself (which I don’t because I have a terrible memory for these things).

My daily summary page contains the following items:

  • Tickets worked on (I will write about the challenges I’ve faced with the ticket today as well as the progress made on it)
  • Tickets closed
  • Resources found/used (this is where I keep track of the random StackOverflow pages or blogs I’ve used to help myself with my work throughout the day)
  • Other work completed (this is where I list any calls, meetings, etc.)
  • Other notes

Once I made the OneNote page template with these items, it became much easier for me to stick with completing the daily summary since I never had to think or retype the main bullet points. This process has helped me define how much time I spend on various activities throughout the day and week which gives me perspective on how much I do (a lot).

Your work time is important, guard it ferociously

My goal for these posts was to give others ideas for how to tame their workload to make it feel more manageable and less overwhelming. I hope that some of these ideas spark something for you to implement in your work life. Overall, I would like to get the point across that your work time is important, and your mental health relating to work is important, so guard both of these things ferociously. Managing your time and not letting others make your life hectic can help prevent burnout, or at least slow it down. These strategies have helped immensely in my chaotic work life, so I hope they can also help with yours.

How to Stay Organized While Busy at Work, Part 1

Recently, I was suddenly given responsibility for all database development work for the application I support, plus many other work items not directly related to the application, when previously I had been splitting that work with another developer. I wasn’t expecting this change, and neither was anyone else, so I was immediately overwhelmed with the amount of work, questions, and requests for review that were coming my way. The first week of this new responsibility was chaos, and it made me realize I needed to tighten up my work organization strategies. I’ve always been someone who keeps my work organized and tried to keep myself to a set pattern of efficient behavior, but it wasn’t as regulated as I needed to keep on top of the flood of work that suddenly came my way. I quickly developed a set of organization and work strategies to make sure I stay on top of everything I’m responsible for while also not being extremely stressed by the workload. Plus, I love helping other people and didn’t want to stop helping other developers because of the new workload, so I made sure my new strategy allowed time to continue with that.

If you’re feeling overwhelmed, stressed, and disorganized with your work and would like some ideas for getting organized to reduce those problems, keep reading. I ended up having a lot more to say on this topic than I originally thought, so I’ve split this topic into two posts. This post will cover the first three ideas on the list below, and the final three will be covered in next week’s post.

  1. Your calendar is your best friend, use it to your advantage
  2. Only work on one thing at a time
  3. Every work request should be a ticket
  4. Set times for yourself to catch up on email and messages throughout the day
  5. Do the hardest and most important work first thing in the morning
  6. Track what you spend your time on

That may seem like a lot of items to add to your already stressful work life but trust me, getting and keeping things organized takes a huge load of stress off of your shoulders. You can’t control what craziness gets thrown at you by others, but you can control how you react to the craziness and how you structure your day to handle that craziness. Plus, if you pick just one or two to start with, ones that would benefit you the most with the least amount of effort, it won’t feel like a burden to use these strategies.

Your calendar is your best friend, use it to your advantage

Seriously, if you’re not using your work calendar already to manage meetings and schedule yourself time to work on what you need to get done, you should start doing it immediately. Whether your calendar is shared with others or not, blocking times for yourself to work on specific tasks can allow you to prepare for that work time mentally and then fully concentrate on that work when the time comes. Plus, it keeps you accountable for getting work done. Here are my 3 quick tips on how you can utilize your calendar to its fullest:

  1. If you use Outlook and your organization has the feature enabled, use the Microsoft Viva plugin to schedule Focus Time for yourself every day. Focus Time is a feature that will automatically schedule blocks of time on your calendar, two weeks at a time, for you to focus on what you need to. While in Focus Time, your status on Teams will be changed to Do Not Disturb so that you won’t get all the pesky notifications of chats, posts, updates, etc. to reduce distractions. I have loved this feature since it got added because it gives me a dedicated and justifiable time for not responding to the constant chats I get throughout the day. Most people in my organization understand that Focus time is sacred and that I won’t be responding to their chats until after the allotted time has ended.
  2. Schedule work time for the most important items you need to complete. Put it on your calendar and have it set your status as Busy for that time. The most important effect of doing this is it will let others know not to schedule meetings over this time (unless they don’t respect calendars in general, which is a separate problem). I usually schedule myself non-Focus Time work times when there is an important meeting coming up and I need to prepare for it. I will set aside 30 minutes to an hour before the meeting when I know I will work on that one task. And when the time arrives for you to work on whatever it was that was scheduled, don’t respond to emails or chats. Setting this time aside for yourself will help keep you on top of what needs to get done when it needs to get done so you’re never unprepared for a meeting or other task again.
  3. On the same note as #2 above, at the beginning of each week, block out time on your calendar every day for lunch and breaks, if you take them. I have found that I am the most productive and feel the best personally when I take a midmorning break, an hour lunch break (to go the gym), and a midafternoon break. I have started blocking out those times on my calendar which prevents people from scheduling over my breaks and also gives me reminders when it is time to take a break. If I didn’t have the reminder pop up, half the time I would forget I need to take one and would then feel burned out at the end of the day.

Only work on one thing at a time

This piece of advice is easier said than done, especially if you work in an organization that often suffers from poor planning or conflicting priorities, but I would like to say it is possible for everyone. It’s at least physically the only way to work (unless you’ve developed a way to code on two different programs at once). Since you can only physically work on one project at a time, and have your focus directed to a single thing at once, that is the best starting point to fight for yourself to get buy-off from management or project managers for only working on one thing at a time.

For me, when things got crazy at work, I realized that I could no longer handle the stress of trying to accomplish all of the business goals within the same time frame they were originally scheduled for. I have worked extremely stressful jobs in the past and had vowed I would never put up with that again, so I had to set boundaries for myself in my current work to reduce the stress. I began pushing back on the analysts and program managers who decided on work priority to give them the burden of making the difficult prioritization decisions that I felt I was facing, given there was now only one DB dev doing the project work.

As an example, during the craziest week of my life at my current job, I was already assigned two tickets, one of them high-priority. Then I went on vacation and came back and was assigned another high-priority ticket that had a deadline in less than two weeks and it was something I had never done before. As soon as I saw that mess, I went to our SA and asked him which of those 3 “high priority” tickets was the most important, stating that I could only work on one task at a time and there were only so many hours in the day, so they needed to tell me the order in which I should work them, according to the business needs. Within a few hours, I had an ordered list provided to me as to what I should be focusing on that day.

But after you push back and get a truly prioritized list of work items, you then need to keep your boundaries in place, no matter what else tries to happen. If others are coming up to you and asking you for a lot of help with whatever they’re working on, tell them that you are currently unable to help but would love to help later after you’ve finished your current task (within reason, if you work in a collaborative environment like I do, you can’t blow your coworkers off all the time). Or if someone else is trying to assign you more tickets that need to be done “right now”, push back on them and your manager and make everyone else work together to figure out how the new task fits in your current list, and switch if needed.

Every work request should be a ticket

This piece of advice is one I’ve used on myself for both of my development jobs so far in my career, and I think it’s one of the easiest to enforce. If you are being asked to do work that will take 30 minutes or more, no matter what it is, create a ticket for it or have the requester make the ticket. I do this to cover myself because I never want to be the person who’s eternally busy but with no metrics to show where their time goes. Even if others don’t admit it, they may be wondering what you’re doing all day if you have no hours logged on tickets. If you set a standard that everything gets a ticket, you won’t ever have to worry about that. Also, having everything as a ticket can help with the prioritization of all work on your plate and helps keep all your current and future work documented and organized. Plus, I like to keep everything in tickets for the satisfaction at the end of the year of seeing how many work items I completed, along with the number of hours total I spent on everything I did (I love data, even on myself).

Conclusion

The three ideas above are only half of the story of what I have been using recently to keep my chaotic work life more organized. I won’t promise that it will make everything sunshine and rainbows, but it at least keeps the chaos reigned in a bit and brings it down to a manageable level. If you’re interested in reading more about the final three methods I’m using for organization, that post will be going up next week. I hope these suggestions are as helpful to you as they have been for me!